<?php
declare(strict_types=1);

namespace App\Modules\Procurement\Models;

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

class GrnItem extends Model
{
    protected static string $table = 'grn_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 = [
        'grn_id',
        'po_item_id',
        'item_id',
        'quantity_received',
        'quantity_accepted',
        'quantity_rejected',
        'rejection_reason',
        'batch_number',
        'expiry_date',
        'unit_cost',
        'line_total',
        'notes',
    ];

    public static function getForGRN(int $grnId): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT gi.*, i.`name_ar` as item_name, i.`sku`
             FROM `grn_items` gi
             JOIN `inventory_items` i ON i.`id` = gi.`item_id`
             WHERE gi.`grn_id` = ?
             ORDER BY gi.`id` ASC",
            [$grnId]
        );
    }
}
