<?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_wallet_transactions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained('employees')->onDelete('cascade');
            $table->enum('transaction_type', ['credit', 'debit']);
            $table->decimal('amount', 10, 2);
            $table->enum('payment_type', ['advance', 'salary', 'daily_wage', 'adjustment'])->default('salary');
            $table->foreignId('reference_id')->nullable()->constrained('employee_salaries')->nullOnDelete();
            $table->string('reference_type')->nullable(); // e.g., 'salary', 'payout'
            $table->string('month')->nullable(); // YYYY-MM format for monthly salary tracking
            $table->text('notes')->nullable();
            $table->foreignId('created_by')->constrained('users')->onDelete('restrict');
            $table->foreignId('restaurant_id')->nullable()->constrained('restaurants')->nullOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained('branches')->nullOnDelete();
            $table->timestamps();
            
            $table->index('employee_id');
            $table->index('transaction_type');
            $table->index('payment_type');
            $table->index('month');
            $table->index('restaurant_id');
            $table->index('branch_id');
            $table->index('created_by');
        });
    }

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

