<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('fixed_asset_depreciation', function (Blueprint $table) {
            $table->id();
            $table->foreignId('fixed_asset_id')
                  ->constrained('fixed_assets')->onDelete('cascade');

            $table->integer('depreciation_year');
            $table->integer('depreciation_month');
            $table->decimal('opening_book_value', 15, 2);
            $table->decimal('depreciation_amount', 15, 2);
            $table->decimal('accumulated_depreciation', 15, 2);
            $table->decimal('closing_book_value', 15, 2);

            $table->string('depreciation_method', 30);
            $table->integer('useful_life_years')->nullable();
            $table->decimal('salvage_value', 15, 2)->default(0);
            $table->decimal('depreciation_rate', 8, 4)->nullable();
            $table->foreignId('calculated_by')->nullable()
                  ->constrained('users')->onDelete('set null');
            $table->dateTime('calculated_at')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->unique(['fixed_asset_id', 'depreciation_year', 'depreciation_month'], 'fa_depreciation_unique');
            $table->index('fixed_asset_id');
            $table->index(['depreciation_year', 'depreciation_month'], 'fa_depreciation_period_idx');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('fixed_asset_depreciation');
    }
};
