<?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
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->foreignId('branch_id')->constrained()->cascadeOnDelete();
            $table->foreignId('table_id')->nullable()->constrained('restaurant_tables')->nullOnDelete();
            $table->foreignId('waiter_id')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('biller_id')->nullable()->constrained('users')->nullOnDelete();

            $table->string('order_type')->default('dine_in'); // dine_in, takeaway, delivery
            $table->string('status')->default('active'); // active, settled, cancelled

            $table->decimal('subtotal', 10, 2)->default(0);
            $table->decimal('gst_amount', 10, 2)->default(0);

            $table->string('discount_type')->nullable(); // percentage, fixed
            $table->decimal('discount_value', 10, 2)->nullable();
            $table->decimal('discount_amount', 10, 2)->default(0);

            $table->decimal('grand_total', 10, 2)->default(0);

            $table->string('payment_method')->nullable(); // cash, card, upi, split

            // For split payments
            $table->decimal('split_cash', 10, 2)->nullable();
            $table->decimal('split_card', 10, 2)->nullable();
            $table->decimal('split_upi', 10, 2)->nullable();

            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('orders');
    }
};
