<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('users', function (Blueprint $table) {
            // Status approval untuk user yang register sendiri
            $table->enum('approval_status', ['pending', 'approved', 'rejected'])
                ->default('approved')
                ->after('is_active');

            // Siapa yang approve user ini
            $table->foreignId('approved_by')
                ->nullable()
                ->after('approval_status')
                ->constrained('users')
                ->nullOnDelete();

            // Kapan di-approve
            $table->timestamp('approved_at')->nullable()->after('approved_by');

            // Alasan penolakan (jika rejected)
            $table->text('rejection_reason')->nullable()->after('approved_at');

            // Avatar/foto profil
            $table->string('avatar')->nullable()->after('rejection_reason');

            // NIP untuk instansi pemerintah
            $table->string('nip')->nullable()->after('employee_id');

            // Index untuk pencarian
            $table->index('approval_status');
            $table->index('nip');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropForeign(['approved_by']);
            $table->dropIndex(['approval_status']);
            $table->dropIndex(['nip']);
            $table->dropColumn([
                'approval_status',
                'approved_by',
                'approved_at',
                'rejection_reason',
                'avatar',
                'nip',
            ]);
        });
    }
};
