<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('fixed_asset_movements')) {
            return;
        }

        $this->extendMovementTypeEnum();

        Schema::table('fixed_asset_movements', function (Blueprint $table) {
            if (! Schema::hasColumn('fixed_asset_movements', 'movement_date')) {
                $table->dateTime('movement_date')->nullable()->after('movement_type');
            }

            if (! Schema::hasColumn('fixed_asset_movements', 'from_division_id')) {
                $table->unsignedBigInteger('from_division_id')->nullable()->after('from_project_id');
            }

            if (! Schema::hasColumn('fixed_asset_movements', 'to_division_id')) {
                $table->unsignedBigInteger('to_division_id')->nullable()->after('to_project_id');
            }

            if (! Schema::hasColumn('fixed_asset_movements', 'performed_by')) {
                $table->foreignId('performed_by')->nullable()->after('user_id')->constrained('users')->onDelete('set null');
            }
        });

        if (Schema::hasColumn('fixed_asset_movements', 'movement_date')) {
            DB::table('fixed_asset_movements')
                ->whereNull('movement_date')
                ->update(['movement_date' => DB::raw('created_at')]);
        }
    }

    public function down(): void
    {
        if (! Schema::hasTable('fixed_asset_movements')) {
            return;
        }

        Schema::table('fixed_asset_movements', function (Blueprint $table) {
            if (Schema::hasColumn('fixed_asset_movements', 'performed_by')) {
                $table->dropConstrainedForeignId('performed_by');
            }

            if (Schema::hasColumn('fixed_asset_movements', 'to_division_id')) {
                $table->dropColumn('to_division_id');
            }

            if (Schema::hasColumn('fixed_asset_movements', 'from_division_id')) {
                $table->dropColumn('from_division_id');
            }

            if (Schema::hasColumn('fixed_asset_movements', 'movement_date')) {
                $table->dropColumn('movement_date');
            }
        });
    }

    private function extendMovementTypeEnum(): void
    {
        DB::statement("
            ALTER TABLE fixed_asset_movements
            MODIFY movement_type ENUM(
                'received',
                'allocated',
                'transferred',
                'returned',
                'maintenance_start',
                'maintenance_end',
                'disposed',
                'retired',
                'condition_change',
                'location_change',
                'incoming',
                'allocation',
                'return',
                'maintenance',
                'maintenance_complete',
                'disposal'
            ) NOT NULL
        ");
    }
};
