<?php
declare(strict_types=1);

namespace App\Modules\Sales\Models;

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

class SaleItem extends Model
{
    protected static string $table = 'sale_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 = [
        'sale_id',
        'item_id',
        'package_id',
        'item_name_ar',
        'quantity',
        'unit_price',
        'discount_amount',
        'tax_amount',
        'line_total',
        'cost_price',
        'batch_id',
        'is_refunded',
        'refunded_quantity',
    ];

    public static function getForSale(int $saleId): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT si.*, i.`sku`, i.`unit_of_measure`
             FROM `sale_items` si
             LEFT JOIN `inventory_items` i ON i.`id` = si.`item_id`
             WHERE si.`sale_id` = ?
             ORDER BY si.`id` ASC",
            [$saleId]
        );
    }
}
