<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('tbl_orders')) {
            Schema::create('tbl_orders', function (Blueprint $table) {
                $table->increments('id_order');

                $table->string('order_reference', 9)->unique();
                $table->string('original_order_reference', 9)->nullable()->index();

                $table->foreignId('store_id')
                    ->nullable()
                    ->constrained('tbl_stores')
                    ->cascadeOnUpdate()
                    ->restrictOnDelete();

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

                $table->string('channel', 20)->default('shopify')->index();
                $table->string('order_type', 30)->default('dispatch')->index();
                $table->string('status', 40)->default('ingresado')->index();

                $table->string('guide')->nullable()->index();
                $table->string('carrier', 60)->nullable();

                $table->string('codClient', 50);

                $table->string('customer_name')->nullable();
                $table->string('customer_identification')->nullable();
                $table->string('customer_email')->nullable()->index();
                $table->string('customer_phone')->nullable();
                $table->string('customer_legal_activity')->nullable();
                $table->unsignedInteger('customer_total_orders')->default(1);

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

                $table->decimal('total_productsTI', 20, 6)->default(0);
                $table->decimal('total_productsTE', 20, 6)->default(0);
                $table->decimal('total_discounts', 20, 6)->default(0);
                $table->decimal('total_shipping', 20, 6)->default(0);
                $table->decimal('total_paidTI', 20, 6)->default(0);
                $table->decimal('total_paidTE', 20, 6)->default(0);

                $table->dateTime('date_order')->index();

                // Dirección
                $table->string('postal_code')->nullable();
                $table->string('province')->nullable();
                $table->string('canton')->nullable();
                $table->string('district')->nullable();
                $table->text('address_detail')->nullable();

                $table->timestamps();
            });

            return;
        }

        Schema::table('tbl_orders', function (Blueprint $table) {
            if (! Schema::hasColumn('tbl_orders', 'id_order')) {
                $table->increments('id_order');
            }

            if (! Schema::hasColumn('tbl_orders', 'order_reference')) {
                $table->string('order_reference', 9)->unique()->after('id_order');
            }

            if (! Schema::hasColumn('tbl_orders', 'original_order_reference')) {
                $table->string('original_order_reference', 9)->nullable()->index()->after('order_reference');
            }

            if (! Schema::hasColumn('tbl_orders', 'store_id')) {
                $table->foreignId('store_id')->nullable()->after('original_order_reference');
            }

            if (! Schema::hasColumn('tbl_orders', 'store_name')) {
                $table->string('store_name')->nullable()->after('store_id');
            }

            if (! Schema::hasColumn('tbl_orders', 'channel')) {
                $table->string('channel', 20)->default('shopify')->index()->after('store_name');
            }

            if (! Schema::hasColumn('tbl_orders', 'order_type')) {
                $table->string('order_type', 30)->default('dispatch')->index()->after('channel');
            }

            if (! Schema::hasColumn('tbl_orders', 'status')) {
                $table->string('status', 40)->default('ingresado')->index()->after('order_type');
            }

            if (! Schema::hasColumn('tbl_orders', 'guide')) {
                $table->string('guide')->nullable()->index()->after('status');
            }

            if (! Schema::hasColumn('tbl_orders', 'carrier')) {
                $table->string('carrier', 60)->nullable()->after('guide');
            }

            if (! Schema::hasColumn('tbl_orders', 'codClient')) {
                $table->string('codClient', 50)->after('carrier');
            }

            foreach ([
                'customer_name' => 'string',
                'customer_identification' => 'string',
                'customer_email' => 'string',
                'customer_phone' => 'string',
                'customer_legal_activity' => 'string',
            ] as $column => $type) {
                if (! Schema::hasColumn('tbl_orders', $column)) {
                    $table->{$type}($column)->nullable()->after('codClient');
                }
            }

            if (! Schema::hasColumn('tbl_orders', 'customer_total_orders')) {
                $table->unsignedInteger('customer_total_orders')->default(1)->after('customer_legal_activity');
            }

            foreach ([
                'payment' => ['string', 255],
            ] as $column => $spec) {
                if (! Schema::hasColumn('tbl_orders', $column)) {
                    [$type, $len] = $spec;
                    $table->{$type}($column, $len)->nullable()->after('customer_total_orders');
                }
            }

            foreach ([
                'total_productsTI',
                'total_productsTE',
                'total_discounts',
                'total_shipping',
                'total_paidTI',
                'total_paidTE',
            ] as $column) {
                if (! Schema::hasColumn('tbl_orders', $column)) {
                    $table->decimal($column, 20, 6)->default(0);
                }
            }

            if (! Schema::hasColumn('tbl_orders', 'date_order')) {
                $table->dateTime('date_order')->index();
            }

            foreach ([
                'postal_code' => 'string',
                'province' => 'string',
                'canton' => 'string',
                'district' => 'string',
            ] as $column => $type) {
                if (! Schema::hasColumn('tbl_orders', $column)) {
                    $table->{$type}($column)->nullable();
                }
            }

            if (! Schema::hasColumn('tbl_orders', 'address_detail')) {
                $table->text('address_detail')->nullable();
            }

            if (! Schema::hasColumn('tbl_orders', 'created_at')) {
                $table->timestamps();
            }
        });
    }

    public function down(): void
    {
        // Intencionalmente no hacemos drop para evitar pérdida de datos en entornos existentes.
    }
};
