<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->foreignId('country_id')->nullable()->constrained('countries');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('phone')->nullable()->unique();
            $table->string('is_admin')->default(0);
            $table->tinyInteger('is_hr')->nullable();
            $table->foreignId('role_id')->nullable()->constrained('roles');
            $table->foreignId('department_id')->nullable()->constrained('departments');
            $table->foreignId('designation_id')->nullable()->constrained('designations');
            $table->text('permissions')->nullable();

            $table->foreignId('manager_id')->nullable()->constrained('users');
            $table->string('employee_id')->nullable();
            $table->enum('employee_type', ['Permanent', 'On Probation', 'Contractual', 'Intern'])->nullable();

            $table->foreignId('duty_schedule_id')->nullable();
            $table->json('weekends')->nullable();
            $table->json('holidays')->nullable();

            // passport
            $table->string('passport_number')->nullable();
            $table->foreignId('passport_file_id')->nullable()->constrained('uploads');
            $table->string('passport_expire_date')->nullable();

            // visa
            $table->string('visa_number')->nullable();
            $table->foreignId('visa_file_id')->nullable()->constrained('uploads');
            $table->string('visa_expire_date')->nullable();

            // labour_card
            $table->string('labour_card_number')->nullable();
            $table->foreignId('labour_card_file_id')->nullable()->constrained('uploads');
            $table->string('labour_card_expire_date')->nullable();

            $table->string('bank_name')->nullable();
            $table->string('bank_account')->nullable();
            $table->string('device_uuid')->nullable();
            $table->string('_token')->nullable()->comment('email verify token')->nullable();
            $table->string('password');
            $table->string('verification_code');

            $table->foreignId('avatar_id')->nullable()->constrained('uploads');
            $table->foreignId('status_id')->index()->default(1)->constrained('statuses')->cascadeOnUpdate();
            $table->longText('device_token')->nullable()->comment('device_token from firebase');
            $table->string('address')->nullable();
            $table->enum('gender', ['Male', 'Female', 'Unisex', 'Others'])->nullable();
            $table->date('birth_date')->nullable();
            $table->enum('religion', ['Islam', 'Hinduism', 'Christianity', "Buddhism", "Others"])->nullable();
            $table->enum('blood_group', ['A+', 'A-', 'B+', 'B-', 'O+', 'O-', 'AB+', 'AB-'])->nullable();
            $table->date('joining_date')->nullable();
            $table->double('basic_salary', 16, 2)->nullable()->default(0);
            $table->enum('marital_status', ['Married', 'Unmarried'])->nullable();
            $table->date('contract_start_date')->nullable();
            $table->date('contract_end_date')->nullable();
            $table->tinyInteger('payslip_type')->default(1)->comment('1 = monthly, 2 = weekly, 3 = daily');
            $table->tinyInteger('is_free_ip')->default(0);
            $table->tinyInteger('is_free_location')->default(0);
            $table->string('time_zone')->nullable();
            $table->string('speak_language')->nullable();
            $table->softDeletes();
            $table->timestamps();

            $table->unsignedBigInteger('company_id')->nullable()->default(1);
            $table->unsignedBigInteger('branch_id')->nullable()->default(1);
        });
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }
};
