<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class FixedAsset extends Model
{
    /**
     * The attributes that are mass assignable.
     * Aligned with edugfqvw_auraaccountifydb schema.
     */
    protected $fillable = [
        'company_id',
        'name',
        'asset_number',
        'asset_code',
        'serial_number',
        'location',
        'purchase_date',
        'purchase_cost',
        'salvage_value',
        'useful_life_months',
        'asset_account_id',
        'accum_dep_account_id',
        'dep_charge_account_id',
        'status',
        'created_by'
    ];

    /**
     * The attributes that should be cast.
     */
    protected $casts = [
        'purchase_date' => 'date',
        'purchase_cost' => 'decimal:2',
        'salvage_value' => 'decimal:2',
        'useful_life_months' => 'integer',
    ];

    /*
    |--------------------------------------------------------------------------
    | Relationships
    |--------------------------------------------------------------------------
    */

    /**
     * The Balance Sheet Asset Account (Cost center).
     */
    public function assetAccount(): BelongsTo
    {
        return $this->belongsTo(Account::class, 'asset_account_id');
    }

    /**
     * The Contra-Asset Account (Accumulated Depreciation).
     */
    public function accumulatedAccount(): BelongsTo
    {
        return $this->belongsTo(Account::class, 'accum_dep_account_id');
    }

    /**
     * The P&L Expense Account (Depreciation Charge).
     */
    public function expenseAccount(): BelongsTo
    {
        return $this->belongsTo(Account::class, 'dep_charge_account_id');
    }

    /**
     * History of monthly depreciation postings.
     */
    public function depreciationLogs(): HasMany
    {
        return $this->hasMany(DepreciationLog::class, 'fixed_asset_id');
    }

    /**
     * The original bill if purchased on credit.
     */
    public function bill(): HasOne
    {
        return $this->hasOne(Bill::class, 'fixed_assets_id');
    }

    /**
     * The cashbook entry if purchased via direct payment.
     */
    public function cashbookEntry(): HasOne
    {
        return $this->hasOne(Cashbook::class, 'fixed_assets_id');
    }

    /**
     * All general ledger entries related to this asset.
     */
    public function journalEntries(): HasMany
    {
        return $this->hasMany(JournalEntry::class, 'fixed_assets_id');
    }

    /*
    |--------------------------------------------------------------------------
    | Accessors & Business Logic
    |--------------------------------------------------------------------------
    */

    /**
     * Calculate Total Depreciation to date.
     */
    public function getTotalDepreciationAttribute(): float
    {
        return (float) $this->depreciationLogs()->sum('depreciation_amount');
    }

    /**
     * Calculate Net Book Value (NBV).
     * Formula: Cost - Accumulated Depreciation.
     */
    public function getNbvAttribute(): float
    {
        return (float) ($this->purchase_cost - $this->total_depreciation);
    }

    /**
     * Determine if the asset is fully depreciated.
     */
    public function getIsFullyDepreciatedAttribute(): bool
    {
        return $this->nbv <= $this->salvage_value || $this->status === 'fully_depreciated';
    }

    /**
     * Monthly Depreciation Amount (Straight Line).
     */
    public function getMonthlyDepreciationAmountAttribute(): float
    {
        if ($this->useful_life_months <= 0) return 0;
        return ($this->purchase_cost - $this->salvage_value) / $this->useful_life_months;
    }
}