<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class FreightCompany extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'company_name',
        'phone',
        'entity_type',
        'vehicle_categories',
        'province',
        'city',
        'postal_code',
        'landline',
        'address',
        'company_description',
        // مدارک حقیقی
        'national_card',
        'birth_certificate',
        'photo_3x4',
        'commendation_letter',
        'activity_license',
        // مدارک حقوقی
        'official_newspaper',
        'latest_changes',
        'company_statute',
        'ceo_national_card',
        'ceo_birth_certificate',
        'official_introduction',
        'attorney_letter',
        'valid_activity_license',
        'status',
        'rejection_reason',
    ];

    protected $casts = [
        'vehicle_categories' => 'array',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];

    /**
     * Get status badge color
     */
    public function getStatusBadgeAttribute()
    {
        return match($this->status) {
            'pending' => 'warning',
            'approved' => 'success',
            'rejected' => 'danger',
            default => 'secondary'
        };
    }

    /**
     * Get status label
     */
    public function getStatusLabelAttribute()
    {
        return match($this->status) {
            'pending' => 'در انتظار تایید',
            'approved' => 'تایید شده',
            'rejected' => 'رد شده',
            default => 'نامشخص'
        };
    }

    /**
     * Get entity type label
     */
    public function getEntityTypeLabelAttribute()
    {
        return $this->entity_type === 'individual' ? 'حقیقی' : 'حقوقی';
    }

    /**
     * Get vehicle categories as string
     */
    public function getVehicleCategoriesStringAttribute()
    {
        if (!$this->vehicle_categories || !is_array($this->vehicle_categories)) {
            return '-';
        }

        $labels = [
            'van' => 'وانت',
            'light' => 'بار سبک',
            'heavy' => 'بار سنگین',
            'mineral' => 'حمل شن، ماسه و مواد معدنی',
        ];

        $categories = array_map(fn($cat) => $labels[$cat] ?? $cat, $this->vehicle_categories);
        return implode('، ', $categories);
    }
}
