<?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.
     */
    public function up(): void
    {
        // Add user_id column to link employees to users table
        Schema::table('employees', function (Blueprint $table) {
            $table->foreignId('user_id')->nullable()->after('id')->constrained('users')->nullOnDelete();
            $table->index('user_id');
        });

        // Update role enum to include all employee-related roles
        // MySQL doesn't support ALTER ENUM directly, so we need to use raw SQL
        DB::statement("ALTER TABLE employees MODIFY COLUMN role ENUM('pos_staff', 'branch_cashier', 'pos_kitchen_staff', 'branch_manager', 'inventory_manager', 'waiter') DEFAULT 'pos_staff'");
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Remove user_id column
        Schema::table('employees', function (Blueprint $table) {
            $table->dropForeign(['user_id']);
            $table->dropIndex(['user_id']);
            $table->dropColumn('user_id');
        });

        // Revert role enum to original values
        DB::statement("ALTER TABLE employees MODIFY COLUMN role ENUM('pos_staff', 'branch_cashier', 'pos_kitchen_staff') DEFAULT 'pos_staff'");
    }
};
