<?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('employees', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->string('employee_code', 64);
            $table->string('full_name');
            $table->string('fingerprint_id', 128)->nullable();
            $table->string('phone_1', 40);
            $table->string('phone_2', 40)->nullable();
            $table->string('email')->nullable();
            $table->foreignId('department_id')->constrained('departments')->restrictOnDelete();
            $table->foreignId('position_id')->constrained('positions')->restrictOnDelete();
            $table->decimal('salary', 15, 2);
            $table->date('hire_date');
            $table->string('gender', 20);
            $table->string('nationality', 100)->nullable();
            $table->string('national_id', 120);
            $table->string('employment_status', 40)->default('full_time');
            $table->boolean('is_active')->default(true);
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->unique(['tenant_id', 'employee_code']);
            $table->unique(['tenant_id', 'national_id']);
            $table->index(['tenant_id', 'is_active']);
            $table->index(['tenant_id', 'department_id']);
            $table->index(['tenant_id', 'position_id']);
        });

        Schema::create('employee_addresses', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->string('country', 100)->nullable();
            $table->string('state', 100)->nullable();
            $table->string('city', 100)->nullable();
            $table->string('street', 255)->nullable();
            $table->string('building_number', 50)->nullable();
            $table->string('location_link', 500)->nullable();
            $table->timestamps();

            $table->unique('employee_id');
        });

        Schema::create('employee_documents', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->string('type', 40);
            $table->string('file_path', 500);
            $table->foreignId('uploaded_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

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

    public function down(): void
    {
        Schema::dropIfExists('employee_documents');
        Schema::dropIfExists('employee_addresses');
        Schema::dropIfExists('employees');
    }
};
