<?php
declare(strict_types=1);

namespace App\Modules\Inventory\Models;

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

class DepreciationEntry extends Model
{
    protected static string $table = 'depreciation_entries';
    protected static string $primaryKey = 'id';
    protected static bool $timestamps = false;
    protected static bool $softDelete = false;
    protected static bool $autoTrackAuthor = false;

    protected static array $fillable = [
        'asset_id',
        'period_month',
        'depreciation_amount',
        'accumulated_total',
        'book_value_after',
        'method_used',
        'is_manual',
        'notes',
        'created_by',
    ];

    public static function getForAsset(int $assetId): array
    {
        return static::query()
            ->where('asset_id', '=', $assetId)
            ->orderBy('period_month', 'DESC')
            ->get();
    }

    public static function getForPeriod(string $periodMonth): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT de.*, ar.`asset_tag`, i.`name_ar` as item_name
             FROM `depreciation_entries` de
             JOIN `asset_register` ar ON ar.`id` = de.`asset_id`
             JOIN `inventory_items` i ON i.`id` = ar.`item_id`
             WHERE de.`period_month` = ?
             ORDER BY ar.`asset_tag` ASC",
            [$periodMonth]
        );
    }
}
