<?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
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('items', function (Blueprint $table) {
            $table->integer('lost_count')->default(0)->after('major_damage_count');
        });

        Schema::table('import_rows', function (Blueprint $table) {
            $table->unsignedInteger('lost_count')->nullable()->after('major_damage_count');
        });

        DB::statement("ALTER TABLE maintenance_logs MODIFY COLUMN `condition` ENUM('good', 'minor', 'major', 'lost') DEFAULT 'good'");
        DB::statement("ALTER TABLE asset_units MODIFY COLUMN current_condition ENUM('good', 'minor', 'major', 'lost') DEFAULT 'good'");
        DB::statement("ALTER TABLE facility_objects MODIFY COLUMN current_condition ENUM('good', 'minor', 'major', 'lost') DEFAULT 'good'");
        DB::statement("ALTER TABLE loan_lines MODIFY COLUMN condition_on_loan ENUM('good', 'minor', 'major', 'lost') DEFAULT NULL");
        DB::statement("ALTER TABLE loan_lines MODIFY COLUMN condition_on_return ENUM('good', 'minor', 'major', 'lost') DEFAULT NULL");
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('items', function (Blueprint $table) {
            $table->dropColumn('lost_count');
        });

        Schema::table('import_rows', function (Blueprint $table) {
            $table->dropColumn('lost_count');
        });

        // Down migration for ENUMs can be tricky or destructive.
        // We assume we might just leave them, or recreate the columns.
        DB::statement("ALTER TABLE maintenance_logs MODIFY COLUMN `condition` ENUM('good', 'minor', 'major') DEFAULT 'good'");
        DB::statement("ALTER TABLE asset_units MODIFY COLUMN current_condition ENUM('good', 'minor', 'major') DEFAULT 'good'");
        DB::statement("ALTER TABLE facility_objects MODIFY COLUMN current_condition ENUM('good', 'minor', 'major') DEFAULT 'good'");
        DB::statement("ALTER TABLE loan_lines MODIFY COLUMN condition_on_loan ENUM('good', 'minor', 'major') DEFAULT NULL");
        DB::statement("ALTER TABLE loan_lines MODIFY COLUMN condition_on_return ENUM('good', 'minor', 'major') DEFAULT NULL");
    }
};
