<?php
declare(strict_types=1);

namespace App\Modules\Inventory\Models;

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

class AssetRegister extends Model
{
    protected static string $table = 'asset_register';
    protected static string $primaryKey = 'id';
    protected static bool $timestamps = true;
    protected static bool $softDelete = true;
    protected static bool $autoTrackAuthor = true;

    protected static array $fillable = [
        'item_id',
        'warehouse_id',
        'asset_tag',
        'serial_number',
        'purchase_date',
        'purchase_cost',
        'useful_life_months',
        'salvage_value',
        'depreciation_method',
        'declining_rate',
        'accumulated_depreciation',
        'book_value',
        'status',
        'disposed_at',
        'disposal_value',
        'disposal_reason',
        'condition_notes',
        'last_depreciation_date',
    ];

    public static function getStatuses(): array
    {
        return [
            'active'           => 'نشط',
            'disposed'         => 'تم التصرف',
            'transferred'      => 'محوّل',
            'under_maintenance' => 'تحت الصيانة',
        ];
    }

    public static function getDepreciationMethods(): array
    {
        return [
            'straight_line'    => 'القسط الثابت',
            'declining_balance' => 'القسط المتناقص',
        ];
    }

    public static function getActiveAssets(): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT ar.*, i.`name_ar` as item_name, i.`sku`, w.`name_ar` as warehouse_name
             FROM `asset_register` ar
             JOIN `inventory_items` i ON i.`id` = ar.`item_id`
             JOIN `warehouses` w ON w.`id` = ar.`warehouse_id`
             WHERE ar.`status` = 'active'
             ORDER BY ar.`asset_tag` ASC"
        );
    }

    public static function search(array $filters, int $perPage = 25, int $page = 1): array
    {
        $db = App::getInstance()->db();
        $where = '1=1';
        $params = [];

        if (!empty($filters['q'])) {
            $search = '%' . $filters['q'] . '%';
            $where .= ' AND (ar.`asset_tag` LIKE ? OR i.`name_ar` LIKE ? OR ar.`serial_number` LIKE ?)';
            $params[] = $search;
            $params[] = $search;
            $params[] = $search;
        }

        if (!empty($filters['status'])) {
            $where .= ' AND ar.`status` = ?';
            $params[] = $filters['status'];
        }

        if (!empty($filters['warehouse_id'])) {
            $where .= ' AND ar.`warehouse_id` = ?';
            $params[] = (int) $filters['warehouse_id'];
        }

        $countRow = $db->selectOne(
            "SELECT COUNT(*) as cnt FROM `asset_register` ar JOIN `inventory_items` i ON i.`id` = ar.`item_id` WHERE {$where}",
            $params
        );
        $total = (int) ($countRow['cnt'] ?? 0);
        $offset = ($page - 1) * $perPage;

        $rows = $db->select(
            "SELECT ar.*, i.`name_ar` as item_name, i.`sku`, w.`name_ar` as warehouse_name
             FROM `asset_register` ar
             JOIN `inventory_items` i ON i.`id` = ar.`item_id`
             JOIN `warehouses` w ON w.`id` = ar.`warehouse_id`
             WHERE {$where}
             ORDER BY ar.`asset_tag` ASC
             LIMIT {$perPage} OFFSET {$offset}",
            $params
        );

        return ['data' => $rows, 'pagination' => Pagination::paginate($total, $perPage, $page)];
    }
}
