<?php

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

return new class extends Migration
{
    public function up(): void
    {
        DB::statement('ALTER TABLE users AUTO_INCREMENT = 1024');
        
        DB::unprepared('
            DROP TRIGGER IF EXISTS users_before_insert;
        ');
        
        DB::unprepared('
            CREATE TRIGGER users_before_insert
            BEFORE INSERT ON users
            FOR EACH ROW
            BEGIN
                IF NEW.ID IS NULL OR NEW.ID = 0 THEN
                    SET @max_id = (SELECT IFNULL(MAX(ID), 1008) FROM users);
                    SET NEW.ID = @max_id + 16;
                END IF;
            END
        ');
    }

    public function down(): void
    {
        DB::unprepared('DROP TRIGGER IF EXISTS users_before_insert');
    }
};
