<?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_disposals', function (Blueprint $table) {
            $table->id();
            $table->string('disposal_number')->unique();
            $table->foreignId('fixed_asset_id')
                  ->constrained('fixed_assets')->onDelete('cascade');

            $table->date('disposal_date');
            $table->enum('disposal_type', ['sold', 'scrapped', 'donated', 'traded_in', 'stolen', 'lost']);

            // Financial
            $table->decimal('book_value_at_disposal', 15, 2)->default(0);
            $table->decimal('disposal_amount', 15, 2)->default(0);
            $table->decimal('gain_loss', 15, 2)->default(0);

            // Buyer/recipient details
            $table->string('buyer_name', 255)->nullable();
            $table->text('disposal_reason');

            // Approval
            $table->enum('status', ['draft', 'pending_approval', 'approved', 'completed'])->default('draft');
            $table->foreignId('requested_by')
                  ->constrained('users')->onDelete('cascade');
            $table->foreignId('approved_by')->nullable()
                  ->constrained('users')->onDelete('set null');
            $table->dateTime('approved_at')->nullable();

            // Documents
            $table->json('attached_documents')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index('fixed_asset_id');
            $table->index(['status', 'disposal_date']);
        });
    }

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