<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->bigIncrements('id')->comment('主键');
            $table->integer('company_id')->comment('公司 ID 与 Tools 保持一致');
            $table->integer('platform_id')->comment('平台 ID 与 Tools 保持一致');;
            $table->integer('store_id')->comment('店铺 ID 与 Tools 保持一致');
            $table->integer('order_status_id')->comment('订单状态 ID');
            $table->text('platform_order_id')->comment('平台 订单 ID');
            $table->integer('payment_method_id')->default('1')->comment('订单状态 ID， 默认为在线支付');
            $table->boolean('presale')->default('false')->comment('是否是预售， 默认为非预售');
            $table->double('total_amount', 10, 2)->default(0)->comment('订单总金额');
            $table->double('total_paid', 10, 2)->default(0)->comment('已支付金额');
            $table->double('total_discount', 10, 2)->default(0)->comment('总折扣金额');
            $table->double('total_received', 10, 2)->default(0)->comment('实收金额');
            $table->double('freight_fee', 10, 2)->default(0)->comment('运费');
            $table->double('tax_fee', 10, 2)->default(0)->comment('税费');
            $table->double('discount_fee', 10, 2)->default(0)->comment('优惠金额');
            $table->double('commission_fee', 10, 2)->default(0)->comment('佣金');
            $table->double('coupon_amount', 10, 2)->default(0)->comment('折扣券 - 优惠券金额');
            $table->double('voucher_amount', 10, 2)->default(0)->comment('代金券 - 兑换券金额');
            $table->integer('order_type_id')->default(1)->comment('订单类型 ID, 默认为一般订单，部分平台可能有补差价，代金券订单等');
            $table->timestampTz('created_date')->comment('订单的创建时间');
            $table->timestampTz('updated_date')->nullable()->default(null)->comment('订单的更新时间');
            $table->timestampTz('paid_date')->nullable()->default(null)->comment('订单的付款时间');
            $table->timestampTz('shipping_date')->nullable()->default(null)->comment('订单的发货时间');
            $table->text('zipcode')->nullable()->default(null)->comment('邮编');
            $table->text('city')->nullable()->default(null)->comment('城市');
            $table->text('province')->nullable()->default(null)->comment('省');
            $table->text('country')->nullable()->default(null)->comment('国家');
            $table->jsonb('raw')->nullable()->default(null)->comment('远程原始数据');
            $table->jsonb('ext')->nullable()->default(null)->comment('扩展字段');
            $table->timestampsTz();
            // 索引
            $table->index('company_id');
            $table->index('platform_id');
            $table->index('store_id');
            $table->index('order_status_id');
            $table->index('order_type_id');
            $table->index('created_date');
            $table->index('updated_date');
            $table->index('paid_date');
            $table->index('shipping_date');

            // 联合唯一索引：店铺 + 平台订单号
            $table->unique(['store_id', 'platform_order_id'], 'orders_store_platform_order_unique');

            // 复合索引：优化按公司查询订单列表并按时间排序
            $table->index(['company_id', 'created_date'], 'orders_company_created_idx');

            // GIN 索引需要使用原生 SQL
        });

        // 为 jsonb 字段创建 GIN 索引（PostgreSQL）
        Schema::getConnection()->statement('CREATE INDEX orders_raw_gin_idx ON orders USING gin (raw)');
        Schema::getConnection()->statement('CREATE INDEX orders_ext_gin_idx ON orders USING gin (ext)');
    }

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