<?php

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

class CreateOrderTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->id();
            // The user placing/paying for this order
            $table->bigInteger('user_id')->unsigned()->index();
            // If this order is linked to a subscription (renewal, partial charge, etc.)
            $table->bigInteger('subscription_id')->unsigned()->nullable()->index();
            $table->bigInteger('invoice_number')->unique()->nullable();
            $table->string('order_reference')->unique();
            $table->tinyInteger('order_type')->index(); // check the Constant seeder for the values
            $table->tinyInteger('status')->default(getConstant('ORDER_STATUS_PENDING'));
            $table->decimal('discount_amount', 10, 2)->default(0);
            $table->string('discount_code')->nullable();
            $table->boolean('on_subtotal')->default(false);
            $table->decimal('tax_amount', 10, 2)->default(0);
            $table->decimal('tax_rate', 10, 2)->default(0);
            $table->decimal('subtotal', 10, 2);
            $table->decimal('total', 10, 2);
            $table->string('currency')->default('USD');
            $table->string('payment_method')->nullable();
            $table->text('payment_error')->nullable();
            $table->string('offline_payment_method')->nullable();
            $table->string('transaction_id')->nullable();
            $table->string('customer_ip_address')->nullable();
            $table->timestamps();
        });

        Schema::create('order_items', function ( Blueprint $table ) {
            $table->id();
            $table->bigInteger('order_id')->unsigned()->index();
//            $table->bigInteger('subscription_addon_id')->unsigned()->nullable();
            $table->string('purchasable_type')->nullable();
            $table->bigInteger('purchasable_id')->unsigned()->nullable();
            $table->string('name')->nullable();
            $table->decimal('price', 10, 2)->default(0);
            $table->integer('quantity')->default(1);
            $table->string('description')->nullable();
            $table->timestamps();
        });

        Schema::create('billing_info', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('order_id')->index(); // One-to-one relationship with orders
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();
            $table->string('address')->nullable();
            $table->string('city')->nullable();
            $table->string('zip')->nullable();
            $table->string('state_id')->nullable();
            $table->string('country_id')->nullable();
            $table->string('phone_number')->nullable();
            $table->string('email')->nullable();
            $table->timestamps();
        });

        Schema::create('order_addons', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('order_id')->index();
            $table->bigInteger('addon_id')->index();
            $table->string('addon_name')->nullable();
            $table->integer('quantity')->default(1);
            $table->decimal('price', 10, 2)->nullable(); // Optional for historical tracking
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {

        Schema::dropIfExists('orders');
        Schema::dropIfExists('billing_info');
        Schema::dropIfExists('order_items');
        Schema::dropIfExists('order_addons');
    }
}
