<?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('user_id')->constrained()->cascadeOnDelete();
            // Personal Info
            $table->string('first_name');
            $table->string('last_name');
            $table->string('middle_name')->nullable();
            $table->string('present_address')->nullable();
            $table->string('permanent_address')->nullable();
            $table->date('date_of_birth')->nullable();
            $table->string('place_of_birth')->nullable();
            $table->string('citizenship')->nullable();
            $table->string('gender')->nullable();
            $table->string('marital_status')->nullable();
            $table->text('children_info')->nullable();
            $table->string('physical_limitation')->nullable();
            $table->string('national_id')->nullable();
            $table->string('passport_number')->nullable();
            // Employment Info
            $table->string('position')->nullable();
            $table->string('employee_number')->unique()->nullable();
            $table->foreignId('department_id')->nullable()->constrained('departments')->nullOnDelete();
            $table->date('date_of_employment')->nullable();
            $table->enum('employment_type', ['full_time', 'part_time', 'contract', 'temporary'])->default('full_time');
            $table->enum('status', ['active', 'on_leave', 'suspended', 'terminated', 'retired'])->default('active');
            $table->decimal('salary_usd', 10, 2)->nullable();
            $table->decimal('salary_lrd', 12, 2)->nullable();
            $table->decimal('salary_other_source', 10, 2)->nullable();
            $table->string('bank_name')->nullable();
            $table->string('bank_account')->nullable();
            // Additional
            $table->text('special_skills')->nullable();
            $table->text('professional_memberships')->nullable();
            $table->text('relatives_at_ul')->nullable();
            $table->text('publications')->nullable();
            $table->text('scholastic_honors')->nullable();
            $table->text('notes')->nullable();
            $table->string('emergency_contact_name')->nullable();
            $table->string('emergency_contact_phone')->nullable();
            $table->string('emergency_contact_relation')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('employee_education', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
            $table->enum('level', ['high_school', 'undergraduate', 'graduate', 'post_graduate', 'professional', 'other']);
            $table->string('institution_name');
            $table->string('institution_location')->nullable();
            $table->date('attendance_from')->nullable();
            $table->date('attendance_to')->nullable();
            $table->boolean('graduated')->default(false);
            $table->string('certificate_degree')->nullable();
            $table->string('major')->nullable();
            $table->string('minor')->nullable();
            $table->timestamps();
        });

        Schema::create('employee_employment_history', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
            $table->date('date_from')->nullable();
            $table->date('date_to')->nullable();
            $table->string('position');
            $table->string('employer_name');
            $table->string('employer_address')->nullable();
            $table->decimal('highest_salary', 10, 2)->nullable();
            $table->text('reason_for_leaving')->nullable();
            $table->timestamps();
        });

        Schema::create('employee_references', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
            $table->string('name');
            $table->string('address')->nullable();
            $table->string('title')->nullable();
            $table->string('phone')->nullable();
            $table->string('email')->nullable();
            $table->timestamps();
        });

        Schema::create('employee_documents', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
            $table->string('document_type');
            $table->string('document_name');
            $table->string('file_path');
            $table->string('file_size')->nullable();
            $table->date('expiry_date')->nullable();
            $table->boolean('verified')->default(false);
            $table->foreignId('verified_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('employee_documents');
        Schema::dropIfExists('employee_references');
        Schema::dropIfExists('employee_employment_history');
        Schema::dropIfExists('employee_education');
        Schema::dropIfExists('employees');
    }
};
