<?php

declare(strict_types=1);

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
    {
        // 1. Tables (Table Management)
        Schema::create('tables', function (Blueprint $table) {
            $table->id();
            $table->foreignId('outlet_id')->constrained('outlets')->onDelete('cascade');
            $table->string('number');
            $table->integer('capacity')->default(2);
            $table->string('status')->default('available'); // 'available', 'occupied', 'reserved'
            $table->foreignId('merged_to_id')->nullable()->constrained('tables')->onDelete('set null');
            $table->timestamps();
        });

        // 2. Customers (CRM & Member Loyalty)
        Schema::create('customers', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('phone')->nullable();
            $table->string('email')->nullable();
            $table->boolean('is_member')->default(false);
            $table->integer('loyalty_points')->default(0);
            $table->timestamps();
        });

        // 3. Reservations
        Schema::create('reservations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('customer_id')->constrained('customers')->onDelete('cascade');
            $table->foreignId('table_id')->constrained('tables')->onDelete('cascade');
            $table->foreignId('outlet_id')->constrained('outlets')->onDelete('cascade');
            $table->date('reservation_date');
            $table->time('reservation_time');
            $table->string('status')->default('pending'); // 'pending', 'confirmed', 'cancelled'
            $table->timestamps();
        });

        // 4. Promos (Promo Engine)
        Schema::create('promos', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('type'); // 'percent', 'nominal', 'bogo'
            $table->decimal('value', 10, 2)->default(0);
            $table->decimal('min_purchase', 10, 2)->default(0);
            $table->foreignId('product_id')->nullable()->constrained('products')->onDelete('cascade');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // 5. Modifiers
        Schema::create('modifiers', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->decimal('price', 10, 2)->default(0);
            $table->foreignId('ingredient_id')->nullable()->constrained('ingredients')->onDelete('set null');
            $table->decimal('usage_qty', 12, 4)->nullable();
            $table->timestamps();
        });

        // 6. Stock Opnames
        Schema::create('stock_opnames', function (Blueprint $table) {
            $table->id();
            $table->foreignId('outlet_id')->constrained('outlets')->onDelete('cascade');
            $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
            $table->string('status')->default('draft'); // 'draft', 'completed'
            $table->timestamps();
        });

        Schema::create('stock_opname_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('stock_opname_id')->constrained('stock_opnames')->onDelete('cascade');
            $table->string('item_type'); // 'Ingredient' or 'Product'
            $table->unsignedBigInteger('item_id');
            $table->decimal('system_qty', 12, 4);
            $table->decimal('actual_qty', 12, 4);
            $table->decimal('difference_qty', 12, 4);
            $table->timestamps();
        });

        // 7. Wastes
        Schema::create('wastes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('outlet_id')->constrained('outlets')->onDelete('cascade');
            $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
            $table->string('item_type'); // 'Ingredient' or 'Product'
            $table->unsignedBigInteger('item_id');
            $table->decimal('qty', 12, 4);
            $table->text('notes')->nullable();
            $table->timestamps();
        });

        // 8. Add Columns to Transactions
        Schema::table('transactions', function (Blueprint $table) {
            $table->string('order_type')->default('take_away'); // 'dine_in', 'take_away', 'delivery'
            $table->foreignId('table_id')->nullable()->constrained('tables')->onDelete('set null');
            $table->foreignId('customer_id')->nullable()->constrained('customers')->onDelete('set null');
            $table->string('queue_number')->nullable();
            $table->decimal('discount_amount', 10, 2)->default(0);
            $table->integer('points_redeemed')->default(0);
            $table->foreignId('waiter_id')->nullable()->constrained('users')->onDelete('set null');
        });

        // 9. Add Columns to Transaction Items
        Schema::table('transaction_items', function (Blueprint $table) {
            $table->string('kitchen_status')->default('pending'); // 'pending', 'cooking', 'ready', 'served'
        });

        // 10. Transaction Item Modifiers (Selected modifiers per checkout item)
        Schema::create('transaction_item_modifiers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('transaction_item_id')->constrained('transaction_items')->onDelete('cascade');
            $table->foreignId('modifier_id')->constrained('modifiers')->onDelete('cascade');
            $table->decimal('price', 10, 2);
            $table->timestamps();
        });

        // 11. Add Columns to Ingredients for Unit Conversion and HPP Costing
        Schema::table('ingredients', function (Blueprint $table) {
            $table->string('purchase_unit')->default('unit');
            $table->string('usage_unit')->default('unit');
            $table->decimal('conversion_ratio', 12, 4)->default(1.0);
            $table->decimal('purchase_price', 10, 2)->default(0);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('ingredients', function (Blueprint $table) {
            $table->dropColumn(['purchase_unit', 'usage_unit', 'conversion_ratio', 'purchase_price']);
        });

        Schema::dropIfExists('transaction_item_modifiers');

        Schema::table('transaction_items', function (Blueprint $table) {
            $table->dropColumn('kitchen_status');
        });

        Schema::table('transactions', function (Blueprint $table) {
            $table->dropForeign(['table_id']);
            $table->dropForeign(['customer_id']);
            $table->dropForeign(['waiter_id']);
            $table->dropColumn(['order_type', 'table_id', 'customer_id', 'queue_number', 'discount_amount', 'points_redeemed', 'waiter_id']);
        });

        Schema::dropIfExists('wastes');
        Schema::dropIfExists('stock_opname_items');
        Schema::dropIfExists('stock_opnames');
        Schema::dropIfExists('modifiers');
        Schema::dropIfExists('promos');
        Schema::dropIfExists('reservations');
        Schema::dropIfExists('customers');
        Schema::dropIfExists('tables');
    }
};
