<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\Invoices\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Maestro\Core\Modules\Items\Models\Item;

class InvoiceLine extends Model
{
    /**
     * @var list<string>
     */
    protected $fillable = [
        'invoice_id',
        'sort_order',
        'item_id',
        'description',
        'quantity',
        'unit_price_ex_vat',
        'vat_rate_id',
        'line_total_ex_vat',
        'line_vat_amount',
        'line_total_inc_vat',
    ];

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'quantity' => 'decimal:4',
            'unit_price_ex_vat' => 'decimal:4',
            'line_total_ex_vat' => 'decimal:2',
            'line_vat_amount' => 'decimal:2',
            'line_total_inc_vat' => 'decimal:2',
        ];
    }

    /**
     * @return BelongsTo<Invoice, InvoiceLine>
     */
    public function invoice(): BelongsTo
    {
        return $this->belongsTo(Invoice::class);
    }

    /**
     * @return BelongsTo<VatRate, InvoiceLine>
     */
    public function vatRate(): BelongsTo
    {
        return $this->belongsTo(VatRate::class, 'vat_rate_id');
    }

    /**
     * @return BelongsTo<Item, InvoiceLine>
     */
    public function item(): BelongsTo
    {
        return $this->belongsTo(Item::class, 'item_id');
    }
}
