<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->id();  // Primary Key
            $table->string('order_id', 20);  // Unique Order ID
            $table->string('user_id');  // Foreign key to Users table (VARCHAR)
            $table->string('vendor_id');  // Foreign key to Users table (vendors as VARCHAR)
            
            $table->text('vendor_address');
            $table->decimal('vendor_latitude', 10, 7)->nullable();
            $table->decimal('vendor_longitude', 10, 7)->nullable();
            
            $table->text('items');  // Items selected by user
            $table->text('vendor_items')->nullable();  // Vendor provided items

            // Pickup and delivery details
            $table->date('pickup_date')->nullable();
            $table->time('pickup_time')->nullable();
            $table->date('delivery_date')->nullable();
            $table->time('delivery_time')->nullable();
            
            $table->string('pickup_floor', 10)->nullable();
            $table->text('customer_address');
            $table->text('how_to_reach')->nullable();  // Delivery instructions
            
            $table->decimal('user_latitude', 10, 7)->nullable();
            $table->decimal('user_longitude', 10, 7)->nullable();
            $table->text('extra_note')->nullable();
            
            $table->boolean('support_your_rider')->default(false);  // Support rider (tips)
            $table->decimal('delivery_charges', 10, 2)->nullable();
            $table->decimal('user_total_cost', 10, 2);  // Total cost user paid
            $table->decimal('vendor_total_cost', 10, 2)->nullable();  // Total cost vendor received
            $table->decimal('cleaneo_profit', 10, 2)->nullable();  // Cleaneo's profit
            $table->decimal('cleaneo_commission', 10, 2)->nullable();  // Cleaneo's commission from vendor

            // Payment and coupon details
            $table->string('payment_mode', 50)->nullable();  // Payment mode
            $table->unsignedBigInteger('coupon_id')->nullable();
            
            // Foreign keys for pickup and delivery agents (referencing `users` table, using VARCHAR)
            $table->string('pickup_agent_id')->nullable();
            $table->string('delivery_agent_id')->nullable();
            
            // Items by weight
            $table->text('items_by_kg')->nullable();
            $table->text('vendor_by_kg')->nullable();
            $table->decimal('total_kg', 10, 2)->nullable();  // Total weight in KG
            $table->boolean('cloth_softener')->default(false);
            $table->boolean('anti_germs_liquid')->default(false);
            $table->string('liquid')->nullable();
            $table->integer('count_by_kg')->nullable();

            // Status and timestamps
            $table->enum('status', [
    'Pending',
    'Accepted',
    'Pickup In Progress',
    'Picked Up',
    'At Vendor',
    'Ready for Delivery',
    'Delivery In Progress',
    'Delivered',
    'Completed',
    'Cancelled'
])->default('Pending');

            $table->json('status_timestamps')->nullable();  // JSON field for status timestamps
            
            $table->timestamps();  // `created_at` and `updated_at`

            // Indexing and foreign key constraints
            $table->foreign('user_id')->references('user_id')->on('users')->onDelete('cascade');
            $table->foreign('vendor_id')->references('user_id')->on('users')->onDelete('cascade');
            $table->foreign('pickup_agent_id')->references('user_id')->on('users')->onDelete('set null');
            $table->foreign('delivery_agent_id')->references('user_id')->on('users')->onDelete('set null');
            $table->foreign('coupon_id')->references('id')->on('coupons')->onDelete('set null');
        });
    }

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