<?php

namespace App\Models\Supplier;

use App\Models\Agency\Agency;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Supplier extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'suppliers';

    protected $fillable = [
        'agency_id',
        'type',
        'supplier_type',
        'first_name',
        'last_name',
        'company_name',
        'company_tax_number',
        'company_tax_office',
        'responsible_person_name',
        'responsible_person_phone',
        'responsible_person_email',
        'billing_address',
        'billing_city',
        'billing_district',
        'billing_tax_number',
        'billing_tax_office',
        'billing_email',
        'emails',
        'phones',
        'phone',
        'address',
        'city',
        'district',
        'country',
        'postal_code',
        'notes',
        'status',
        'created_by',
        'updated_by',
    ];

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

    // Relationships
    public function agency()
    {
        return $this->belongsTo(Agency::class);
    }

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

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

    public function priceLists()
    {
        return $this->hasMany(SupplierPriceList::class);
    }

    public function activities()
    {
        return $this->hasMany(SupplierActivity::class);
    }

    // Accessors & Mutators
    public function getFullNameAttribute(): string
    {
        if ($this->type === 'corporate') {
            return $this->company_name ?? '';
        }
        return trim(($this->first_name ?? '') . ' ' . ($this->last_name ?? ''));
    }

    public function getInitialsAttribute(): string
    {
        if ($this->type === 'corporate') {
            return mb_strtoupper(mb_substr($this->company_name ?? '', 0, 2, 'UTF-8'), 'UTF-8');
        }
        $first = mb_substr($this->first_name ?? '', 0, 1, 'UTF-8');
        $last = mb_substr($this->last_name ?? '', 0, 1, 'UTF-8');
        return mb_strtoupper($first . $last, 'UTF-8');
    }

    public function getStatusLabelAttribute(): string
    {
        return [
            'active' => 'Aktif',
            'inactive' => 'Pasif',
            'blocked' => 'Engellenmiş',
            'potential' => 'Potansiyel',
        ][$this->status] ?? ucfirst($this->status);
    }

    public static function supplierTypes(): array
    {
        return [
            'hotel' => 'Otel',
            'transportation' => 'Taşıma Firması',
            'restaurant' => 'Restoran / Lokanta / Mekan',
            'insurance' => 'Sigorta Şirketi',
            'visa' => 'Vize Danışmanı',
            'airline' => 'Havayolu Firması',
        ];
    }

    public function getSupplierTypeLabelAttribute(): string
    {
        return self::supplierTypes()[$this->supplier_type] ?? ucfirst($this->supplier_type);
    }

    // Scopes
    public function scopeActive($query)
    {
        return $query->where('status', 'active');
    }

    public function scopeByAgency($query, int $agencyId)
    {
        return $query->where('agency_id', $agencyId);
    }

    protected static function boot(): void
    {
        parent::boot();

        static::creating(function (Supplier $supplier) {
            if (auth()->check()) {
                $supplier->created_by = auth()->id();
            }
        });

        static::updating(function (Supplier $supplier) {
            if (auth()->check()) {
                $supplier->updated_by = auth()->id();
            }
        });
    }
}
