<?php

namespace App\Models;

use App\Casts\TrimmedDecimal;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class PurchaseItem extends Model
{
    /**
     * @var list<string>
     */
    protected $fillable = [
        'purchase_id',
        'purchase_product_id',
        'quantity',
        'unit_price',
        'line_total',
    ];

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'purchase_id' => 'integer',
            'purchase_product_id' => 'integer',
            'quantity' => TrimmedDecimal::class.':3',
            'unit_price' => TrimmedDecimal::class.':2',
            'line_total' => TrimmedDecimal::class.':2',
        ];
    }

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

    /**
     * @return BelongsTo<PurchaseProduct, $this>
     */
    public function product(): BelongsTo
    {
        return $this->belongsTo(PurchaseProduct::class, 'purchase_product_id');
    }
}
