<?php
declare(strict_types=1);

namespace App\Modules\Sales\Models;

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

class PackageItem extends Model
{
    protected static string $table = 'package_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 = [
        'package_id',
        'item_id',
        'quantity',
        'sort_order',
    ];

    public static function getForPackage(int $packageId): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT pi.*, i.`name_ar` as item_name, i.`sku`, i.`unit_of_measure`,
                    i.`sale_price_member`, i.`sale_price_nonmember`
             FROM `package_items` pi
             JOIN `inventory_items` i ON i.`id` = pi.`item_id`
             WHERE pi.`package_id` = ?
             ORDER BY pi.`sort_order` ASC, i.`name_ar` ASC",
            [$packageId]
        );
    }
}
