<?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::table('expenses', function (Blueprint $table) {
            // Add pay later functionality
            $table->enum('payment_status', ['immediate', 'pay_later'])->default('immediate')->after('payment_method');
            $table->date('due_date')->nullable()->after('payment_status');
            $table->text('payment_notes')->nullable()->after('due_date');
            
            // Add petty cash functionality
            $table->boolean('is_petty_cash')->default(false)->after('payment_notes');
            $table->decimal('petty_cash_balance_before', 10, 2)->nullable()->after('is_petty_cash');
            $table->decimal('petty_cash_balance_after', 10, 2)->nullable()->after('petty_cash_balance_before');
            
            // Enhanced tracking
            $table->string('reference_number')->nullable()->after('receipt_number');
            $table->enum('priority', ['low', 'medium', 'high', 'urgent'])->default('medium')->after('reference_number');
            
            // Add indexes for better performance
            $table->index(['payment_status', 'due_date']);
            $table->index(['is_petty_cash']);
            $table->index(['priority']);
        });

        // Create petty cash transactions table
        Schema::create('petty_cash_transactions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('restaurant_id')->constrained()->onDelete('cascade');
            $table->foreignId('branch_id')->nullable()->constrained()->onDelete('cascade');
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('expense_id')->nullable()->constrained()->onDelete('set null');
            
            $table->string('transaction_number')->unique();
            $table->enum('type', ['deposit', 'withdrawal', 'adjustment']);
            $table->decimal('amount', 10, 2);
            $table->decimal('balance_before', 10, 2);
            $table->decimal('balance_after', 10, 2);
            
            $table->string('description');
            $table->text('notes')->nullable();
            $table->json('metadata')->nullable(); // For additional data
            
            $table->timestamps();
            
            // Indexes
            $table->index(['restaurant_id', 'branch_id']);
            $table->index(['type', 'created_at']);
            $table->index('transaction_number');
        });

        // Create petty cash balances table
        Schema::create('petty_cash_balances', function (Blueprint $table) {
            $table->id();
            $table->foreignId('restaurant_id')->constrained()->onDelete('cascade');
            $table->foreignId('branch_id')->nullable()->constrained()->onDelete('cascade');
            
            $table->decimal('current_balance', 10, 2)->default(0);
            $table->decimal('daily_limit', 10, 2)->default(5000); // Default daily limit
            $table->decimal('transaction_limit', 10, 2)->default(1000); // Default per-transaction limit
            
            $table->boolean('is_active')->default(true);
            $table->timestamp('last_transaction_at')->nullable();
            
            $table->timestamps();
            
            // Unique constraint
            $table->unique(['restaurant_id', 'branch_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('petty_cash_balances');
        Schema::dropIfExists('petty_cash_transactions');
        
        Schema::table('expenses', function (Blueprint $table) {
            $table->dropIndex(['payment_status', 'due_date']);
            $table->dropIndex(['is_petty_cash']);
            $table->dropIndex(['priority']);
            
            $table->dropColumn([
                'payment_status',
                'due_date', 
                'payment_notes',
                'is_petty_cash',
                'petty_cash_balance_before',
                'petty_cash_balance_after',
                'reference_number',
                'priority'
            ]);
        });
    }
};