<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

class Contractor extends Model
{
    use LogsActivity;

    protected $fillable = [
        'type', 'first_name', 'last_name', 'company_name', 'mobile', 'phone',
        'national_code', 'national_id', 'registration_number', 'economic_code',
        'email', 'province', 'city', 'postal_code', 'address',
        'specialty', 'description', 'is_active',
    ];

    protected function casts(): array
    {
        return [
            'is_active' => 'boolean',
        ];
    }

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()->logFillable()->logOnlyDirty()->dontSubmitEmptyLogs();
    }

    /** نام نمایشی پیمانکار (حقیقی یا حقوقی) */
    public function getDisplayNameAttribute(): string
    {
        if ($this->type === 'legal') {
            return $this->company_name ?: trim("{$this->first_name} {$this->last_name}");
        }

        return trim("{$this->first_name} {$this->last_name}") ?: ($this->company_name ?? '—');
    }

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