<?php

namespace App\Models;

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

class Supplier extends Model
{
    use HasFactory, SoftDeletes;

    /**
     * The attributes that are mass assignable.
     * Matches the ZedBankOS 2026 Supplier Schema.
     */
    protected $fillable = [
        'company_id',
        'name',
        'email',
        'phone',
        'tax_id',
        'physical_address',
        'postal_address',
        'country',
        'opening_balance',
        'contact_first_name',
        'contact_last_name',
        'contact_email',
        'contact_phone',
        'notes',
        'created_by'
    ];

    /**
     * Cast attributes to native types.
     */
    protected $casts = [
        'opening_balance' => 'decimal:2',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];

    /*
    |--------------------------------------------------------------------------
    | Relationships
    |--------------------------------------------------------------------------
    */

    /**
     * Link to the Company/Tenant owning this record.
     */
    public function company(): BelongsTo
    {
        return $this->belongsTo(Company::class);
    }

    /**
     * Link to the User who created the supplier.
     */
    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    /**
     * Link to the Ledger entries (for the Supplier Statement).
     */
    public function journalEntries(): HasMany
    {
        return $this->hasMany(JournalEntry::class);
    }

    /*
    |--------------------------------------------------------------------------
    | Accessors (Usage: $supplier->attribute_name)
    |--------------------------------------------------------------------------
    */

    /**
     * Returns the full name of the primary contact person.
     */
    public function getContactFullNameAttribute(): string
    {
        $fullName = trim("{$this->contact_first_name} {$this->contact_last_name}");
        return $fullName ?: 'N/A';
    }

    /**
     * Returns the opening balance with thousands separator.
     */
    public function getFormattedBalanceAttribute(): string
    {
        return number_format($this->opening_balance, 2);
    }

    /**
     * Formats the display name for dropdowns/selects.
     */
    public function getDisplayNameAttribute(): string
    {
        return "{$this->name} (" . strtoupper($this->country) . ")";
    }

    /*
    |--------------------------------------------------------------------------
    | Scopes (Usage: Supplier::zambian()->get())
    |--------------------------------------------------------------------------
    */

    /**
     * Scope for filtering local Zambian suppliers.
     */
    public function scopeZambian($query)
    {
        return $query->where('country', 'Zambia');
    }

    /**
     * Scope for filtering international suppliers (e.g. China).
     */
    public function scopeInternational($query)
    {
        return $query->where('country', '!=', 'Zambia');
    }
}