<?php

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

class CreateOrdersTable extends Migration
{
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->id();

            $table->string('uuid')->nullable();

            $table->unsignedBigInteger('branch_id')->nullable();
            $table->foreign('branch_id')->nullable()->on('branches')->references('id')->onDelete('cascade')->onUpdate('cascade');

            $table->unsignedBigInteger('client_id')->nullable();
            $table->foreign('client_id')->nullable()->on('clients')->references('id')->onUpdate('cascade')->onDelete('cascade');

            $table->unsignedBigInteger('address_id')->nullable();
            $table->foreign('address_id')->nullable()->on('addresses')->references('id')->onUpdate('cascade')->onDelete('cascade');

            $table->unsignedBigInteger('delivery_id');
            $table->foreign('delivery_id')->on('deliveries')->references('id')->onUpdate('cascade')->onDelete('cascade');

            $table->unsignedBigInteger('payment_id');
            $table->foreign('payment_id')->on('payments')->references('id')->onUpdate('cascade')->onDelete('cascade');

            $table->decimal('sub_total', 9, 2)->default(0);
            $table->decimal('discount', 9, 2)->default(0);
            $table->integer('discount_percentage')->nullable();
            $table->decimal('vat', 9, 2)->default(0);
            $table->integer('vat_percentage')->nullable();
            $table->decimal('coupon', 9, 2)->default(0);
            $table->integer('coupon_percentage')->nullable();
            $table->string('coupon_text')->nullable();
            $table->decimal('charge_cost', 9, 2)->default(0);
            $table->decimal('net_total', 9, 2)->default(0);

            $table->integer('status')->default(0);
            $table->integer('follow')->default(0);

            $table->string('tap_id')->nullable();
            $table->string('mobile_type')->nullable();
            
            $table->time('delivery_time_from')->nullable();
            $table->time('delivery_time_to')->nullable();
            $table->date('delivery_date')->nullable();
            
            $table->string('notes')->nullable();

            $table->timestamps();
        });

        Schema::create('order_product', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('order_id')->nullable();
            $table->foreign('order_id')->nullable()->on('orders')->references('id')->onUpdate('cascade')->onDelete('cascade');

            $table->unsignedBigInteger('product_id')->nullable();
            $table->foreign('product_id')->nullable()->on('products')->references('id')->onUpdate('cascade')->onDelete('cascade');

            $table->decimal('price', 8, 2)->nullable();
            $table->smallInteger('quantity');
            $table->decimal('total', 9, 2)->nullable();
            
            $table->string('note')->nullable();

            $table->timestamps();
        });

        Schema::create('order_product_item', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('order_product_id')->nullable();
            $table->foreign('order_product_id')->nullable()->on('order_product')->references('id')->onUpdate('cascade')->onDelete('cascade');

            $table->string('title_ar')->nullable();
            $table->string('title_en')->nullable();

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

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