<?php

namespace App\Models;

use App\Enums\DocumentStatus;
use App\Models\Concerns\HasPublicId;
use App\Models\Concerns\ImmutableWhenPosted;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;

final class VendorBill extends Model
{
    use HasPublicId, ImmutableWhenPosted;

    protected $fillable = [
        'public_id','company_id','branch_id','party_id','transport_job_id','trip_id','vehicle_id','driver_id',
        'bill_number','vendor_invoice_number','invoice_date','supply_date','due_date','currency_code','exchange_rate',
        'pricing_mode','taxable_total','recoverable_vat_total','nonrecoverable_vat_total','grand_total','paid_total',
        'allocated_total','payable_account_id','expense_account_id','input_vat_account_id','tax_code_id','status',
        'approved_by','approved_at','posted_at','created_by','notes','attachment_hash',
    ];

    protected function casts(): array
    {
        return [
            'invoice_date'=>'date','supply_date'=>'date','due_date'=>'date','exchange_rate'=>'decimal:8',
            'taxable_total'=>'decimal:4','recoverable_vat_total'=>'decimal:4','nonrecoverable_vat_total'=>'decimal:4',
            'grand_total'=>'decimal:4','paid_total'=>'decimal:4','allocated_total'=>'decimal:4','status'=>DocumentStatus::class,
            'approved_at'=>'datetime','posted_at'=>'datetime',
        ];
    }

    public function company(): BelongsTo { return $this->belongsTo(Company::class); }
    public function branch(): BelongsTo { return $this->belongsTo(Branch::class); }
    public function party(): BelongsTo { return $this->belongsTo(Party::class); }
    public function transportJob(): BelongsTo { return $this->belongsTo(TransportJob::class); }
    public function trip(): BelongsTo { return $this->belongsTo(Trip::class); }
    public function vehicle(): BelongsTo { return $this->belongsTo(Vehicle::class); }
    public function driver(): BelongsTo { return $this->belongsTo(Driver::class); }
    public function taxCode(): BelongsTo { return $this->belongsTo(TaxCode::class); }
    public function creator(): BelongsTo { return $this->belongsTo(User::class, 'created_by'); }
    public function approver(): BelongsTo { return $this->belongsTo(User::class, 'approved_by'); }
    public function items(): HasMany { return $this->hasMany(VendorBillItem::class); }
    public function allocations(): HasMany { return $this->hasMany(PaymentAllocation::class); }
    public function journal(): MorphOne { return $this->morphOne(JournalEntry::class, 'source'); }
}
