<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Expert extends Model
{
    use HasFactory;
    use SoftDeletes;

    protected $guarded = ['id', 'created_at', 'updated_at'];

    protected $appends = ['display_name'];

    protected $casts = [
        'education_certificates' => 'array',
        'skill_certificates' => 'array',
        'work_experience' => 'array',
        'business_license' => 'array',
        'products' => 'array',
        'reviewed_at' => 'datetime',
        'work_latitude' => 'decimal:8',
        'work_longitude' => 'decimal:8',
    ];

    public const STATUS_ACTIVE = 'approved';

    public const STATUS_APPROVED = 'approved';

    public const STATUS_PENDING = 'pending';

    public const STATUS_REJECTED = 'rejected';

    public const STATUS_NEEDS_EDIT = 'needs_edit';

    public const STATUS_BLOCKED = 'blocked';

    public static function statusOptions(): array
    {
        return [
            self::STATUS_PENDING => 'در انتظار',
            self::STATUS_APPROVED => 'تایید شده',
            self::STATUS_REJECTED => 'رد شده',
            self::STATUS_NEEDS_EDIT => 'نیاز به ویرایش',
            self::STATUS_BLOCKED => 'مسدود',
        ];
    }

    protected static function boot(): void
    {
        parent::boot();
        static::addGlobalScope('order', function (Builder $builder) {
            $builder->orderByDesc('created_at');
        });
    }

    public function createdBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function updatedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'updated_by');
    }

    public function reviewedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'reviewed_by');
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function province(): BelongsTo
    {
        return $this->belongsTo(\Mortezaa97\Regions\Models\Province::class, 'work_province_id');
    }

    public function county(): BelongsTo
    {
        return $this->belongsTo(\Mortezaa97\Regions\Models\County::class, 'work_county_id');
    }

    public function educationItems(): HasMany
    {
        return $this->hasMany(ExpertEducation::class, 'expert_id')->orderBy('sort_order');
    }

    public function certificates(): HasMany
    {
        return $this->hasMany(ExpertCertificate::class, 'expert_id')->orderBy('sort_order');
    }

    public function consultationRequests(): HasMany
    {
        return $this->hasMany(ConsultationRequest::class, 'expert_id');
    }

    public function getDisplayNameAttribute(): string
    {
        if ($this->brand_name) {
            return $this->brand_name;
        }
        if ($this->user) {
            return $this->user->getFilamentName();
        }
        return 'خدمات‌دهنده #' . $this->id;
    }
}
