<?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
    {
        if (! Schema::hasTable('departments')) {
            Schema::create('departments', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('company_id')->nullable();
                $table->string('name');
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('designations')) {
            Schema::create('designations', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('company_id')->nullable();
                $table->string('name');
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('locations')) {
            Schema::create('locations', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('company_id')->nullable();
                $table->string('name');
                $table->text('address')->nullable();
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('shifts')) {
            Schema::create('shifts', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('company_id')->nullable();
                $table->string('name');
                $table->time('clock_in_time');
                $table->time('clock_out_time');
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('leave_types')) {
            Schema::create('leave_types', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('company_id')->nullable();
                $table->string('name');
                $table->boolean('is_paid')->default(false);
                $table->integer('total_leaves');
                $table->string('count_type', 255)->default('same_for_all');
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('attendances')) {
            Schema::create('attendances', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('company_id')->nullable();
                $table->unsignedBigInteger('user_id');
                $table->date('date')->nullable();
                $table->dateTime('clock_in_date_time')->nullable();
                $table->dateTime('clock_out_date_time')->nullable();
                $table->string('clock_in_ip_address')->nullable();
                $table->string('clock_out_ip_address')->nullable();
                $table->time('clock_in_time')->nullable();
                $table->time('clock_out_time')->nullable();
                $table->integer('total_duration')->nullable();
                $table->string('status')->default('present');
                $table->boolean('is_holiday')->default(false);
                $table->boolean('is_leave')->default(false);
                $table->unsignedBigInteger('leave_id')->nullable();
                $table->unsignedBigInteger('holiday_id')->nullable();
                $table->boolean('is_late')->default(false);
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('work_durations')) {
            Schema::create('work_durations', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('company_id')->nullable();
                $table->unsignedBigInteger('attendance_id')->nullable();
                $table->time('start_time')->nullable();
                $table->time('end_time')->nullable();
                $table->integer('duration')->nullable();
                $table->string('status')->nullable();
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('leaves')) {
            Schema::create('leaves', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('company_id')->nullable();
                $table->unsignedBigInteger('user_id');
                $table->unsignedBigInteger('leave_type_id');
                $table->date('start_date');
                $table->date('end_date')->nullable();
                $table->float('total_days')->default(0);
                $table->boolean('is_half_day')->default(false);
                $table->string('half_leave', 20)->default('before_break');
                $table->text('reason');
                $table->boolean('is_paid')->default(false);
                $table->string('status', 20)->default('pending');
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('payrolls')) {
            Schema::create('payrolls', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('company_id')->nullable();
                $table->unsignedBigInteger('user_id')->nullable();
                $table->integer('month');
                $table->integer('year');
                $table->double('basic_salary');
                $table->double('salary_amount');
                $table->double('pre_payment_amount')->default(0);
                $table->double('expense_amount')->default(0);
                $table->double('net_salary');
                $table->double('total_days');
                $table->double('working_days');
                $table->double('present_days');
                $table->string('status')->default('generated');
                $table->date('payment_date')->nullable();
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('payroll_components')) {
            Schema::create('payroll_components', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('company_id')->nullable();
                $table->unsignedBigInteger('payroll_id')->nullable();
                $table->unsignedBigInteger('salary_component_id')->nullable();
                $table->string('name');
                $table->string('value_type')->default('fixed');
                $table->double('amount');
                $table->boolean('is_earning')->default(true);
                $table->string('type', 20)->default('salary');
                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('payroll_components');
        Schema::dropIfExists('payrolls');
        Schema::dropIfExists('leaves');
        Schema::dropIfExists('work_durations');
        Schema::dropIfExists('attendances');
        Schema::dropIfExists('leave_types');
        Schema::dropIfExists('shifts');
        Schema::dropIfExists('locations');
        Schema::dropIfExists('designations');
        Schema::dropIfExists('departments');
    }
};
