<?php

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

/**
 * CIRCULAR REFERENCE NOTE
 * -----------------------
 * This table intentionally does NOT contain a department_id column.
 *
 * The previous design had:
 *   departments.head_user_id  →  users.id
 *   users.department_id       →  departments.id
 *
 * That is a circular FK dependency: neither table can be fully inserted
 * before the other exists with a committed row.
 *
 * The correct design:
 *   - A user's department is accessed via:  user → employee → department
 *     (users hasOne employees; employees belongsTo departments)
 *   - Storing department_id on users was a denormalisation that created the cycle.
 *   - departments.head_user_id FK is added in migration 000007, after this
 *     table exists, making it a one-directional deferred constraint.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->enum('role', [
                'super_admin', 'hr_admin', 'president', 'dept_head', 'employee',
            ])->default('employee');

            // A human-readable staff ID string (e.g. "UL-0012").
            // NOT a FK — the FK relationship is employees.user_id → users.id.
            $table->string('employee_number', 20)->unique()->nullable();

            $table->string('phone')->nullable();
            $table->string('avatar')->nullable();
            $table->boolean('is_active')->default(true);
            $table->string('theme', 30)->default('navy');
            $table->string('sidebar_style', 10)->default('dark');
            $table->timestamp('last_login_at')->nullable();
            $table->rememberToken();
            $table->timestamps();
            $table->softDeletes();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('users');
    }
};
