<?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
    {
        // 门店表
        Schema::create('stores', function (Blueprint $table) {
            $table->id();
            $table->string('store_name', 100)->comment('门店名称');
            $table->string('store_code', 32)->unique()->comment('门店编码');
            $table->string('store_type', 20)->default('convenience')->comment('门店类型:convenience/supermarket/specialty');
            $table->string('address')->nullable()->comment('门店地址');
            $table->string('contact_phone', 20)->nullable()->comment('联系电话');
            $table->string('business_hours', 50)->nullable()->comment('营业时间');
            $table->tinyInteger('status')->default(1)->comment('状态:1启用 0禁用');
            $table->timestamps();
            $table->softDeletes();

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

        // 分类表
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('parent_id')->default(0)->comment('父级分类ID');
            $table->string('category_name', 64)->comment('分类名称');
            $table->string('category_code', 32)->unique()->comment('分类编码');
            $table->integer('sort_order')->default(0)->comment('排序');
            $table->tinyInteger('status')->default(1)->comment('状态:1启用 0禁用');
            $table->timestamps();

            $table->index('parent_id');
            $table->index('sort_order');
        });

        // 供应商表
        Schema::create('suppliers', function (Blueprint $table) {
            $table->id();
            $table->string('supplier_name', 100)->comment('供应商名称');
            $table->string('contact_person', 32)->nullable()->comment('联系人');
            $table->string('contact_phone', 20)->nullable()->comment('联系电话');
            $table->string('settlement_type', 20)->default('monthly')->comment('结算方式:monthly/quarterly/cash');
            $table->string('address')->nullable()->comment('地址');
            $table->text('remark')->nullable()->comment('备注');
            $table->tinyInteger('status')->default(1)->comment('状态:1启用 0禁用');
            $table->timestamps();
            $table->softDeletes();

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

        // 商品表
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('sku_code', 32)->unique()->comment('SKU编码');
            $table->string('barcode', 32)->nullable()->unique()->comment('条码');
            $table->string('product_name', 128)->comment('商品名称');
            $table->foreignId('category_id')->constrained()->comment('分类ID');
            $table->foreignId('supplier_id')->nullable()->constrained()->comment('供应商ID');
            $table->string('brand', 64)->nullable()->comment('品牌');
            $table->string('spec', 64)->nullable()->comment('规格');
            $table->string('unit', 16)->comment('计量单位');
            $table->decimal('purchase_price', 10, 2)->default(0)->comment('进货价');
            $table->decimal('retail_price', 10, 2)->default(0)->comment('零售价');
            $table->decimal('member_price', 10, 2)->nullable()->comment('会员价');
            $table->string('image', 255)->nullable()->comment('商品图片');
            $table->tinyInteger('status')->default(1)->comment('状态:1上架 0下架');
            $table->timestamps();
            $table->softDeletes();

            $table->index('category_id');
            $table->index('supplier_id');
            $table->index('status');
        });

        // 库存表
        Schema::create('inventories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained()->comment('门店ID');
            $table->foreignId('product_id')->constrained()->comment('商品ID');
            $table->integer('quantity')->default(0)->comment('当前库存');
            $table->integer('warning_min')->default(0)->comment('最低库存预警');
            $table->integer('warning_max')->default(0)->comment('最高库存预警');
            $table->timestamps();

            $table->unique(['store_id', 'product_id']);
            $table->index('quantity');
        });

        // 库存变动日志
        Schema::create('inventory_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained()->comment('门店ID');
            $table->foreignId('product_id')->constrained()->comment('商品ID');
            $table->string('type', 20)->comment('变动类型:stock_in/stock_out/sale/return/loss/check');
            $table->integer('quantity')->comment('变动数量');
            $table->integer('before_qty')->default(0)->comment('变动前数量');
            $table->integer('after_qty')->default(0)->comment('变动后数量');
            $table->foreignId('order_id')->nullable()->comment('关联订单ID');
            $table->string('remark')->nullable()->comment('备注');
            $table->foreignId('operator_id')->nullable()->comment('操作人ID');
            $table->timestamps();

            $table->index('store_id');
            $table->index('product_id');
            $table->index('type');
            $table->index('created_at');
        });

        // 会员等级表
        Schema::create('member_levels', function (Blueprint $table) {
            $table->id();
            $table->string('level_name', 32)->comment('等级名称');
            $table->integer('min_points')->default(0)->comment('最低积分');
            $table->decimal('discount_rate', 3, 2)->default(1.00)->comment('折扣率');
            $table->decimal('points_rate', 3, 2)->default(1.00)->comment('积分倍率');
            $table->string('benefits')->nullable()->comment('等级权益描述');
            $table->integer('sort_order')->default(0)->comment('排序');
            $table->timestamps();
        });

        // 会员表
        Schema::create('members', function (Blueprint $table) {
            $table->id();
            $table->string('member_no', 32)->unique()->comment('会员编号');
            $table->string('name', 32)->comment('会员姓名');
            $table->string('phone', 20)->unique()->comment('手机号（加密存储）');
            $table->foreignId('level_id')->default(1)->constrained('member_levels')->comment('等级ID');
            $table->foreignId('store_id')->nullable()->constrained()->comment('注册门店ID');
            $table->integer('points')->default(0)->comment('积分');
            $table->decimal('balance', 10, 2)->default(0)->comment('储值余额');
            $table->date('birthday')->nullable()->comment('生日');
            $table->string('wechat_openid')->nullable()->unique()->comment('微信OpenID');
            $table->tinyInteger('status')->default(1)->comment('状态:1正常 0禁用');
            $table->timestamp('last_consume_at')->nullable()->comment('最后消费时间');
            $table->timestamps();
            $table->softDeletes();

            $table->index('level_id');
            $table->index('store_id');
            $table->index('status');
        });

        // 订单表
        Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->string('order_no', 32)->unique()->comment('订单号');
            $table->foreignId('store_id')->constrained()->comment('门店ID');
            $table->foreignId('user_id')->comment('收银员ID');
            $table->foreignId('member_id')->nullable()->comment('会员ID');
            $table->decimal('total_amount', 10, 2)->default(0)->comment('订单总额');
            $table->decimal('discount_amount', 10, 2)->default(0)->comment('优惠金额');
            $table->decimal('pay_amount', 10, 2)->default(0)->comment('实付金额');
            $table->string('pay_method', 20)->nullable()->comment('支付方式');
            $table->string('status', 20)->default('pending')->comment('状态:pending/paid/completed/refunding/refunded/cancelled');
            $table->string('remark')->nullable()->comment('备注');
            $table->timestamp('paid_at')->nullable()->comment('支付时间');
            $table->timestamp('completed_at')->nullable()->comment('完成时间');
            $table->timestamps();

            $table->index('store_id');
            $table->index('user_id');
            $table->index('member_id');
            $table->index('status');
            $table->index('created_at');
        });

        // 订单明细表
        Schema::create('order_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('order_id')->constrained()->cascadeOnDelete()->comment('订单ID');
            $table->foreignId('product_id')->constrained()->comment('商品ID');
            $table->string('product_name')->comment('商品名称（快照）');
            $table->string('product_sku', 32)->nullable()->comment('商品编码（快照）');
            $table->decimal('unit_price', 10, 2)->comment('单价');
            $table->integer('quantity')->comment('数量');
            $table->decimal('subtotal', 10, 2)->comment('小计');
            $table->timestamps();

            $table->index('order_id');
            $table->index('product_id');
        });

        // 操作日志表
        Schema::create('operation_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->comment('操作人ID');
            $table->string('module', 32)->comment('模块');
            $table->string('action', 32)->comment('动作');
            $table->string('target_type', 32)->nullable()->comment('目标类型');
            $table->unsignedBigInteger('target_id')->nullable()->comment('目标ID');
            $table->json('params')->nullable()->comment('请求参数');
            $table->string('ip', 45)->nullable()->comment('IP地址');
            $table->string('user_agent')->nullable()->comment('用户代理');
            $table->timestamps();

            $table->index('user_id');
            $table->index('module');
            $table->index('created_at');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('operation_logs');
        Schema::dropIfExists('order_items');
        Schema::dropIfExists('orders');
        Schema::dropIfExists('members');
        Schema::dropIfExists('member_levels');
        Schema::dropIfExists('inventory_logs');
        Schema::dropIfExists('inventories');
        Schema::dropIfExists('products');
        Schema::dropIfExists('suppliers');
        Schema::dropIfExists('categories');
        Schema::dropIfExists('stores');
    }
};
