<?php

declare(strict_types=1);

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

/**
 * 零售管理系统数据库迁移
 *
 * 依据《电子商务法》第十四条：商品信息完整展示。
 * 依据《消费者权益保护法》第二十五条：七日无理由退货。
 * 依据《个人信息保护法》第四十七条：会员注销删除个人信息。
 * 依据 SB/T 10668-2012：库存盘点可追溯。
 */
return new class extends Migration
{
    public function up(): void
    {
        // 商品
        Schema::create('kylin_retail_products', function (Blueprint $table) {
            $table->id();
            $table->string('name', 200)->comment('商品名称');
            $table->string('barcode', 50)->nullable()->unique()->comment('商品条码');
            $table->string('category', 50)->nullable()->comment('商品分类');
            $table->unsignedInteger('price')->comment('售价（分）');
            $table->unsignedInteger('cost_price')->nullable()->comment('成本价（分）');
            $table->unsignedInteger('stock')->default(0)->comment('库存数量');
            $table->unsignedInteger('min_stock')->default(10)->comment('最低库存预警');
            $table->string('status', 20)->default('active')->comment('状态');
            $table->timestamps();
            $table->softDeletes();

            $table->index('category');
        });

        // 会员
        Schema::create('kylin_retail_members', function (Blueprint $table) {
            $table->id();
            $table->string('name', 50)->comment('姓名');
            $table->text('phone')->nullable()->comment('手机号（加密）');
            $table->unsignedInteger('points')->default(0)->comment('积分');
            $table->string('level', 20)->default('normal')->comment('会员等级');
            $table->dateTime('joined_at')->comment('加入时间');
            $table->string('status', 20)->default('active')->comment('状态');
            $table->timestamps();
            $table->softDeletes();
        });

        // 订单
        Schema::create('kylin_retail_orders', function (Blueprint $table) {
            $table->id();
            $table->string('order_no', 30)->unique()->comment('订单编号');
            $table->foreignId('member_id')->nullable()->constrained('kylin_retail_members')->nullOnDelete()->comment('会员 ID');
            $table->unsignedInteger('total_amount')->comment('总金额（分）');
            $table->string('pay_method', 20)->nullable()->comment('支付方式');
            $table->dateTime('paid_at')->nullable()->comment('支付时间');
            $table->string('status', 20)->default('pending')->comment('状态');
            $table->timestamps();

            $table->index('paid_at');
            $table->index(['member_id', 'status']);
        });

        // 订单明细
        Schema::create('kylin_retail_order_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('order_id')->constrained('kylin_retail_orders')->cascadeOnDelete()->comment('订单 ID');
            $table->foreignId('product_id')->constrained('kylin_retail_products')->cascadeOnDelete()->comment('商品 ID');
            $table->unsignedInteger('quantity')->comment('数量');
            $table->unsignedInteger('unit_price')->comment('单价（分）');
            $table->unsignedInteger('subtotal')->comment('小计（分）');
            $table->timestamps();
        });

        // 库存日志
        Schema::create('kylin_retail_inventory_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('product_id')->constrained('kylin_retail_products')->cascadeOnDelete()->comment('商品 ID');
            $table->string('type', 20)->comment('操作类型');
            $table->integer('quantity')->comment('数量变化');
            $table->unsignedInteger('before_stock')->comment('操作前库存');
            $table->unsignedInteger('after_stock')->comment('操作后库存');
            $table->string('remark', 200)->nullable()->comment('备注');
            $table->dateTime('operated_at')->comment('操作时间');
            $table->timestamps();

            $table->index(['product_id', 'operated_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('kylin_retail_inventory_logs');
        Schema::dropIfExists('kylin_retail_order_items');
        Schema::dropIfExists('kylin_retail_orders');
        Schema::dropIfExists('kylin_retail_members');
        Schema::dropIfExists('kylin_retail_products');
    }
};