<?php

namespace App\Models;

use App\Enums\RecordStatus;
use App\Models\Concerns\Auditable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class TelecomCompany extends Model
{
    use Auditable, HasFactory, SoftDeletes;

    protected $fillable = [
        'name', 'code', 'phone', 'email', 'contact_person', 'address', 'status', 'notes',
    ];

    protected $casts = [
        'status' => RecordStatus::class,
    ];

    public function plans(): HasMany
    {
        return $this->hasMany(Plan::class);
    }

    public function lines(): HasMany
    {
        return $this->hasMany(Line::class);
    }

    public function mainAccounts(): HasMany
    {
        return $this->hasMany(MainAccount::class);
    }

    public function scopeSearch($query, ?string $term)
    {
        if (! $term) {
            return $query;
        }

        return $query->where(function ($q) use ($term) {
            $q->where('name', 'like', "%{$term}%")
                ->orWhere('code', 'like', "%{$term}%")
                ->orWhere('phone', 'like', "%{$term}%");
        });
    }
}
