<?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) {
            $table->id();

            // Identity
            $table->string('asset_code', 50)->unique();
            $table->string('asset_name');
            $table->string('asset_tag', 50)->unique()->nullable();
            $table->string('serial_number', 100)->nullable();
            $table->string('model_number', 100)->nullable();
            $table->string('manufacturer', 100)->nullable();

            // Classification
            $table->foreignId('category_id')
                  ->constrained('fixed_asset_categories')->onDelete('restrict');

            // Purchase Information
            $table->foreignId('supplier_id')->nullable()
                  ->constrained('suppliers')->onDelete('set null');
            $table->string('purchase_order_number', 100)->nullable();
            $table->string('invoice_number', 100)->nullable();
            $table->date('invoice_date')->nullable();
            $table->date('purchase_date')->nullable();
            $table->decimal('purchase_price', 15, 2)->default(0);
            $table->enum('currency', ['AED', 'USD', 'EUR'])->default('AED');

            // Depreciation
            $table->string('depreciation_method', 30)->default('straight_line');
            $table->integer('useful_life_years')->default(5);
            $table->decimal('salvage_value', 15, 2)->default(0);
            $table->decimal('current_book_value', 15, 2)->default(0);
            $table->date('depreciation_start_date')->nullable();

            // Current State
            $table->enum('condition', ['new', 'good', 'fair', 'poor', 'damaged', 'written_off'])->default('new');
            $table->enum('lifecycle_status', [
                'acquired',
                'in_warehouse',
                'allocated',
                'in_use',
                'under_maintenance',
                'returned',
                'disposed',
                'retired',
            ])->default('acquired');

            // Location
            $table->foreignId('current_project_id')->nullable()
                  ->constrained('projects')->onDelete('set null');
            $table->foreignId('current_division_id')->nullable()
                  ->constrained('project_divisions')->onDelete('set null');
            $table->string('current_location', 255)->nullable();

            // Warranty
            $table->date('warranty_start_date')->nullable();
            $table->date('warranty_end_date')->nullable();
            $table->text('warranty_terms')->nullable();

            // Documents & Media
            $table->string('photo')->nullable();
            $table->json('attached_documents')->nullable();

            // Meta
            $table->enum('status', ['active', 'inactive'])->default('active');
            $table->text('notes')->nullable();
            $table->foreignId('registered_by')->nullable()
                  ->constrained('users')->onDelete('set null');
            $table->timestamps();
            $table->softDeletes();

            $table->index('asset_code');
            $table->index('asset_tag');
            $table->index('serial_number');
            $table->index('category_id');
            $table->index('lifecycle_status');
            $table->index('condition');
            $table->index('current_project_id');
            $table->index(['lifecycle_status', 'condition']);
        });
    }

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