<?php

namespace App\Models\Warehouse\FixedAssets;

use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class FixedAssetDepreciation extends Model
{
    protected $table = 'fixed_asset_depreciation';

    protected $fillable = [
        'fixed_asset_id',
        'depreciation_year',
        'depreciation_month',
        'opening_book_value',
        'depreciation_amount',
        'accumulated_depreciation',
        'closing_book_value',
        'depreciation_method',
        'useful_life_years',
        'salvage_value',
        'depreciation_rate',
        'calculated_by',
        'calculated_at',
        'notes',
    ];

    protected $casts = [
        'opening_book_value' => 'decimal:2',
        'depreciation_amount' => 'decimal:2',
        'accumulated_depreciation' => 'decimal:2',
        'closing_book_value' => 'decimal:2',
        'salvage_value' => 'decimal:2',
        'calculated_at' => 'datetime',
    ];

    // ── Relationships ──────────────────────────────────────────────────

    public function fixedAsset()
    {
        return $this->belongsTo(FixedAsset::class, 'fixed_asset_id');
    }

    public function calculatedBy()
    {
        return $this->belongsTo(User::class, 'calculated_by');
    }

    // ── Scopes ─────────────────────────────────────────────────────────

    public function scopeForPeriod(Builder $query, int $year, int $month): Builder
    {
        return $query->where('depreciation_year', $year)
            ->where('depreciation_month', $month);
    }

    public function scopeForYear(Builder $query, int $year): Builder
    {
        return $query->where('depreciation_year', $year);
    }
}
