<?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) {
            $table->foreignId('company_id')->nullable()->after('id')->constrained()->nullOnDelete();
            $table->string('employee_id')->nullable()->after('company_id');
            $table->string('phone')->nullable()->after('email');
            $table->enum('role', ['admin', 'manager', 'employee'])->default('employee')->after('phone');
            $table->string('department')->nullable()->after('role');
            $table->string('position')->nullable()->after('department');
            $table->text('address')->nullable()->after('position');
            $table->decimal('home_latitude', 10, 8)->nullable()->after('address');
            $table->decimal('home_longitude', 11, 8)->nullable()->after('home_latitude');
            $table->boolean('is_active')->default(true)->after('home_longitude');

            $table->unique(['company_id', 'employee_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropForeign(['company_id']);
            $table->dropUnique(['company_id', 'employee_id']);
            $table->dropColumn([
                'company_id',
                'employee_id',
                'phone',
                'role',
                'department',
                'position',
                'address',
                'home_latitude',
                'home_longitude',
                'is_active',
            ]);
        });
    }
};
