<?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('daily_closing_reports', function (Blueprint $table) {
            $table->id();
            $table->foreignId('restaurant_id')->constrained('restaurants')->cascadeOnDelete();
            $table->foreignId('branch_id')->constrained('branches')->cascadeOnDelete();
            $table->date('closing_date');
            $table->decimal('available_cash', 10, 2)->default(0);
            $table->decimal('gross_sales', 10, 2)->default(0);
            $table->decimal('online_card_payments', 10, 2)->default(0);
            $table->decimal('food_bills_total', 10, 2)->default(0);
            $table->decimal('advance_payments', 10, 2)->default(0);
            $table->decimal('expenses_paid', 10, 2)->default(0);
            $table->decimal('daily_wages', 10, 2)->default(0);
            $table->decimal('total_deductions', 10, 2)->default(0);
            $table->decimal('remaining_cash', 10, 2)->default(0);
            $table->decimal('difference', 10, 2)->default(0);
            $table->boolean('has_difference')->default(false);
            $table->text('comments')->nullable();
            $table->foreignId('created_by')->constrained('users')->cascadeOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            
            // Indexes for performance
            $table->index('restaurant_id');
            $table->index('branch_id');
            $table->index('closing_date');
            $table->index('has_difference');
            
            // Unique constraint: one report per branch per date
            $table->unique(['branch_id', 'closing_date']);
        });
    }

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