<?php
declare(strict_types=1);

namespace App\Modules\Inventory\Models;

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

class PurchaseOrderItem extends Model
{
    protected static string $table = 'purchase_order_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 = [
        'purchase_order_id',
        'item_id',
        'quantity_ordered',
        'quantity_received',
        'unit_cost',
        'line_total',
        'notes',
    ];

    /**
     * Get all items for a purchase order with item details.
     */
    public static function getForPO(int $poId): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT poi.*, i.`name_ar` as item_name, i.`sku`, i.`unit_of_measure`
             FROM `purchase_order_items` poi
             JOIN `inventory_items` i ON i.`id` = poi.`item_id`
             WHERE poi.`purchase_order_id` = ?
             ORDER BY i.`name_ar` ASC",
            [$poId]
        );
    }
}
