<?php
declare(strict_types=1);

namespace App\Modules\Procurement\Models;

use App\Core\Model;
use App\Core\App;

class PurchaseRequisitionItem extends Model
{
    protected static string $table = 'purchase_requisition_items';
    protected static string $primaryKey = 'id';
    protected static bool $timestamps = false;
    protected static bool $softDelete = false;
    protected static bool $autoTrackAuthor = false;

    protected static array $fillable = [
        'requisition_id',
        'item_id',
        'description_ar',
        'description_en',
        'quantity',
        'unit_of_measure',
        'estimated_unit_cost',
        'estimated_line_total',
        'specifications',
        'preferred_supplier_id',
    ];

    public static function getForRequisition(int $requisitionId): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT pri.*,
                    i.`name_ar` as item_name, i.`sku`,
                    s.`name_ar` as preferred_supplier_name
             FROM `purchase_requisition_items` pri
             LEFT JOIN `inventory_items` i ON i.`id` = pri.`item_id`
             LEFT JOIN `suppliers` s ON s.`id` = pri.`preferred_supplier_id`
             WHERE pri.`requisition_id` = ?
             ORDER BY pri.`id` ASC",
            [$requisitionId]
        );
    }
}
