<?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('employee_payrolls', function (Blueprint $table) {
            $table->id();
            $table->string('payroll_code')->unique();
            $table->unsignedBigInteger('employee_id');
            $table->date('period_start');
            $table->date('period_end');
            $table->enum('pay_type', ['daily', 'monthly'])->default('monthly');

            // Attendance-based calculation fields
            $table->integer('days_present')->default(0);
            $table->integer('days_absent')->default(0);
            $table->integer('days_half')->default(0);
            $table->integer('days_leave')->default(0);
            $table->integer('days_holiday')->default(0);
            $table->decimal('total_working_hours', 8, 2)->default(0);

            // Salary components
            $table->decimal('base_amount', 15, 2)->default(0)->comment('Base salary or daily wage × days');
            $table->decimal('incentives', 15, 2)->default(0)->comment('Sum of applicable incentives');
            $table->decimal('deductions', 15, 2)->default(0)->comment('Deductions for absences or other reasons');
            $table->decimal('adjustments', 15, 2)->default(0)->comment('Manual adjustments (+ or -)');
            $table->decimal('gross_salary', 15, 2)->default(0)->comment('Total before deductions');
            $table->decimal('net_salary', 15, 2)->default(0)->comment('Final payable amount');

            // Payment tracking
            $table->enum('status', ['draft', 'calculated', 'approved', 'paid', 'cancelled'])->default('draft');
            $table->date('payment_date')->nullable();
            $table->string('payment_method')->nullable();
            $table->string('payment_reference')->nullable();

            $table->text('remarks')->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('approved_by')->nullable();
            $table->unsignedBigInteger('paid_by')->nullable();
            $table->timestamp('approved_at')->nullable();
            $table->timestamp('paid_at')->nullable();
            $table->timestamps();
            $table->softDeletes();

            // Foreign keys
            $table->foreign('employee_id')
                ->references('id')
                ->on('accounts')
                ->onDelete('cascade');

            $table->foreign('created_by')
                ->references('id')
                ->on('users')
                ->onDelete('set null');

            $table->foreign('approved_by')
                ->references('id')
                ->on('users')
                ->onDelete('set null');

            $table->foreign('paid_by')
                ->references('id')
                ->on('users')
                ->onDelete('set null');

            // Indexes
            $table->index(['employee_id', 'period_start', 'period_end']);
            $table->index('status');
            $table->index('payment_date');
        });
    }

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