<?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::create('employees', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->string('emp_number')->unique();
            $table->string('name')->index();
            $table->string('phone', 20)->nullable();
            $table->string('work_phone', 20)->nullable();
            $table->string('email')->nullable()->unique();
            $table->date('dob')->nullable();
            $table->string('nid',20)->nullable();
            $table->string('father_name')->nullable();
            $table->string('mother_name')->nullable();

            $table->foreignId('nation_id')->nullable()->constrained('countries')->nullOnDelete();
            $table->foreignId('gender_id')->nullable()->constrained('genders')->nullOnDelete();
            $table->foreignId('religion_id')->nullable()->constrained('religions')->nullOnDelete();
            $table->foreignId('blood_id')->nullable()->constrained('bloods')->nullOnDelete();
            $table->foreignId('marital_status_id')->nullable()->constrained('marital_statuses')->nullOnDelete();
            $table->foreignId('employment_status_id')->nullable()->constrained('employment_statuses')->restrictOnDelete();

            $table->foreignId('department_id')->nullable()->constrained('departments')->nullOnDelete();
            $table->foreignId('designation_id')->nullable()->constrained('designations')->nullOnDelete();
            $table->foreignId('project_id')->nullable()->constrained('projects')->nullOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained('branches')->nullOnDelete();

            $table->foreignId('shift_id')->nullable()->constrained('shifts')->nullOnDelete();
            $table->foreignId('attendance_policy_id')->nullable()->constrained('attendance_policies')->nullOnDelete();
            $table->foreignId('weekend_policy_id')->nullable()->constrained('weekend_policies')->nullOnDelete();
            $table->foreignId('holiday_allowance_policy_id')->nullable()->constrained('holiday_allowance_policies')->nullOnDelete();
            $table->foreignId('report_to')->nullable()->constrained('employees')->nullOnDelete();

            $table->date('joined_date')->nullable();

            $table->unsignedBigInteger('emp_termination_id')->nullable();

            $table->decimal('current_basic_salary', 12, 2)->default(0);

            $table->boolean('cpf_active')->default(true);
            $table->boolean('attendance_required')->default(true);
            $table->boolean('overtime_allowed')->default(false);
            $table->boolean('is_payroll')->default(true);
            $table->boolean('tax_deduction')->default(false);

            $table->string('image')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('employees');
    }
};
