<?php
declare(strict_types=1);

namespace App\Modules\Procurement\Models;

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

class VendorInvoiceItem extends Model
{
    protected static string $table = 'vendor_invoice_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 = [
        'invoice_id', 'po_item_id', 'item_id', 'description',
        'quantity', 'unit_cost', 'tax_rate', 'tax_amount',
        'discount_amount', 'line_total',
    ];

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