<?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_assets', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('business_id')->constrained()->restrictOnDelete();
            $table->foreignId('branch_id')->constrained()->restrictOnDelete();
            $table->string('asset_number')->unique();
            $table->string('name');
            $table->string('category');
            $table->text('description')->nullable();
            $table->string('serial_number')->nullable()->index();
            $table->string('supplier')->nullable();
            $table->string('purchase_reference')->nullable();
            $table->string('acquisition_source')->default('opening');
            $table->date('acquisition_date');
            $table->date('depreciation_start_date')->nullable();
            $table->decimal('acquisition_cost', 18, 2);
            $table->decimal('residual_value', 18, 2)->default(0);
            $table->string('depreciation_method')->default('straight_line');
            $table->unsignedInteger('useful_life_months')->nullable();
            $table->decimal('accumulated_depreciation', 18, 2)->default(0);
            $table->decimal('book_value', 18, 2);
            $table->foreignId('financial_account_id')->nullable()->constrained('financial_accounts')->nullOnDelete();
            $table->string('location')->nullable();
            $table->foreignId('custodian_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->string('status')->default('active');
            $table->date('next_maintenance_date')->nullable();
            $table->text('notes')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index(['business_id', 'category']);
            $table->index(['branch_id', 'status']);
            $table->index(['branch_id', 'next_maintenance_date']);
        });

        Schema::create('fixed_asset_depreciations', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('fixed_asset_id')->constrained('fixed_assets')->cascadeOnDelete();
            $table->date('period_date');
            $table->decimal('amount', 18, 2);
            $table->decimal('accumulated_after', 18, 2);
            $table->decimal('book_value_after', 18, 2);
            $table->string('journal_number')->unique();
            $table->text('notes')->nullable();
            $table->timestamp('posted_at');
            $table->foreignId('posted_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->unique(['fixed_asset_id', 'period_date'], 'fixed_asset_period_unique');
        });

        Schema::create('fixed_asset_maintenances', function (Blueprint $table): void {
            $table->id();
            $table->string('maintenance_number')->unique();
            $table->foreignId('fixed_asset_id')->constrained('fixed_assets')->cascadeOnDelete();
            $table->foreignId('branch_id')->constrained()->restrictOnDelete();
            $table->date('maintenance_date');
            $table->string('maintenance_type');
            $table->text('description');
            $table->string('supplier')->nullable();
            $table->decimal('cost', 18, 2)->default(0);
            $table->foreignId('financial_account_id')->nullable()->constrained('financial_accounts')->nullOnDelete();
            $table->string('reference')->nullable();
            $table->string('journal_number')->nullable()->unique();
            $table->date('next_due_date')->nullable();
            $table->text('notes')->nullable();
            $table->foreignId('recorded_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index(['branch_id', 'maintenance_date']);
            $table->index(['fixed_asset_id', 'maintenance_date']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('fixed_asset_maintenances');
        Schema::dropIfExists('fixed_asset_depreciations');
        Schema::dropIfExists('fixed_assets');
    }
};
