<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateOrdersAndOrderItemsTables extends Migration {
    public function up(){
        Schema::create('orders', function(Blueprint $table){
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('restaurant_id')->constrained()->onDelete('cascade');
            $table->foreignId('rider_id')->nullable()->constrained('users')->onDelete('set null');
            $table->foreignId('address_id')->nullable()->constrained('addresses')->onDelete('set null');
            $table->enum('status',['pending','accepted','preparing','out_for_delivery','delivered','cancelled'])->default('pending');
            $table->enum('payment_method',['COD','UPI','Card','Wallet'])->nullable();
            $table->enum('payment_status',['pending','paid','failed'])->default('pending');
            $table->integer('total_amount')->default(0);
            $table->integer('delivery_fee')->default(0);
            $table->integer('tax_amount')->default(0);
            $table->integer('discount')->default(0);
            $table->timestamp('placed_at')->nullable();
            $table->timestamp('delivered_at')->nullable();
            $table->timestamps();
        });

        Schema::create('order_items', function(Blueprint $table){
            $table->id();
            $table->foreignId('order_id')->constrained()->onDelete('cascade');
            $table->foreignId('menu_item_id')->constrained()->onDelete('cascade');
            $table->integer('quantity')->default(1);
            $table->integer('price');
            $table->integer('total');
            $table->timestamps();
        });
    }
    public function down(){
        Schema::dropIfExists('order_items');
        Schema::dropIfExists('orders');
    }
}
