<?php
declare(strict_types=1);

namespace App\Modules\Inventory\Models;

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

class StockAuditItem extends Model
{
    protected static string $table = 'stock_audit_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 = [
        'audit_id',
        'item_id',
        'batch_id',
        'system_quantity',
        'physical_quantity',
        'variance',
        'variance_cost',
        'status',
        'counted_by',
        'counted_at',
        'notes',
    ];

    public static function getForAudit(int $auditId): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT sai.*, i.`name_ar` as item_name, i.`sku`, i.`unit_of_measure`, i.`cost_price`
             FROM `stock_audit_items` sai
             JOIN `inventory_items` i ON i.`id` = sai.`item_id`
             WHERE sai.`audit_id` = ?
             ORDER BY i.`name_ar` ASC",
            [$auditId]
        );
    }
}
