<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * This migration adds 'bitlocker' to the password type ENUM
     * and changes the password field from tinytext to text to support
     * longer encrypted passwords (BitLocker recovery keys).
     */
    public function up(): void
    {
        // Disable strict mode temporarily to avoid timestamp validation issues
        DB::statement("SET SESSION sql_mode = ''");

        // Add 'bitlocker' to the password type ENUM
        DB::statement("ALTER TABLE keychain MODIFY COLUMN type ENUM('windows','email','skype','osx','icloud','appleid','bitlocker') NOT NULL DEFAULT 'windows'");

        // Change password field from tinytext to text to support longer encrypted passwords
        DB::statement("ALTER TABLE keychain MODIFY COLUMN password TEXT");

        // Re-enable strict mode
        DB::statement("SET SESSION sql_mode = 'TRADITIONAL'");
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Disable strict mode temporarily to avoid timestamp validation issues
        DB::statement("SET SESSION sql_mode = ''");

        // Remove 'bitlocker' from the ENUM
        DB::statement("ALTER TABLE keychain MODIFY COLUMN type ENUM('windows','email','skype','osx','icloud','appleid') NOT NULL DEFAULT 'windows'");

        // Revert password field back to tinytext (note: this will truncate any passwords > 255 chars)
        DB::statement("ALTER TABLE keychain MODIFY COLUMN password TINYTEXT");

        // Re-enable strict mode
        DB::statement("SET SESSION sql_mode = 'TRADITIONAL'");
    }
};
