<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class DberpPurchasePriceReferenceSnapshot extends Model
{
    protected $table = 'dberp_purchase_price_reference_snapshots';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'scope_key',
        'scope_type',
        'product_id',
        'supplier_id',
        'latest_transaction_unit_price',
        'latest_transaction_date',
        'latest_receipt_date',
        'latest_lead_time_days',
        'latest_purchase_order_id',
        'latest_purchase_order_item_id',
        'refreshed_at',
    ];

    protected function casts(): array
    {
        return [
            'product_id' => 'integer',
            'supplier_id' => 'integer',
            'latest_transaction_unit_price' => 'decimal:4',
            'latest_transaction_date' => 'date',
            'latest_receipt_date' => 'date',
            'latest_lead_time_days' => 'integer',
            'latest_purchase_order_id' => 'integer',
            'latest_purchase_order_item_id' => 'integer',
            'refreshed_at' => 'datetime',
        ];
    }

    public function product(): BelongsTo
    {
        return $this->belongsTo(DberpProduct::class, 'product_id');
    }

    public function supplier(): BelongsTo
    {
        return $this->belongsTo(DberpSupplier::class, 'supplier_id');
    }

    public function latestPurchaseOrder(): BelongsTo
    {
        return $this->belongsTo(DberpPurchaseOrder::class, 'latest_purchase_order_id');
    }

    public function latestPurchaseOrderItem(): BelongsTo
    {
        return $this->belongsTo(DberpPurchaseOrderItem::class, 'latest_purchase_order_item_id');
    }
}