<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('password_history', function (Blueprint $table) {
            $table->id();
            $table->string('user_type', 20);  // admin | client
            $table->unsignedBigInteger('user_id');
            $table->string('password_hash', 255);
            $table->timestamp('created_at');

            $table->index(['user_type', 'user_id', 'created_at']);
        });

        Schema::table('admins', function (Blueprint $table) {
            $table->timestamp('password_changed_at')->nullable()->after('password');
            $table->boolean('password_must_change')->default(false)->after('password_changed_at');
        });

        Schema::table('clients', function (Blueprint $table) {
            $table->timestamp('password_changed_at')->nullable()->after('password');
            $table->boolean('password_must_change')->default(false)->after('password_changed_at');
        });
    }

    public function down(): void
    {
        Schema::table('clients', function (Blueprint $table) {
            $table->dropColumn(['password_changed_at', 'password_must_change']);
        });
        Schema::table('admins', function (Blueprint $table) {
            $table->dropColumn(['password_changed_at', 'password_must_change']);
        });
        Schema::dropIfExists('password_history');
    }
};
