<?php

declare(strict_types=1);

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

/**
 * 餐饮系统核心表迁移
 *
 * 合规依据：
 *  - 《食品安全法》第五十三条：进货查验记录保留 >= 2 年
 *  - 《餐饮服务食品安全操作规范》7.5：食品留样 >= 125g + 48h
 *  - 《反食品浪费法》第七条：适量点餐提示
 */
return new class extends Migration
{
    public function up(): void
    {
        // 供应商表
        Schema::create('catering_suppliers', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100)->comment('供应商名称');
            $table->string('contact', 50)->nullable()->comment('联系人');
            $table->string('phone', 20)->nullable()->comment('联系电话');
            $table->string('address', 255)->nullable()->comment('地址');
            $table->string('license_number', 100)->nullable()->comment('许可证编号');
            $table->date('license_expiry')->nullable()->comment('许可证到期日');
            $table->string('status', 20)->default('active')->comment('状态');
            $table->timestamps();
        });

        // 菜品分类表
        Schema::create('catering_categories', function (Blueprint $table) {
            $table->id();
            $table->string('name', 50)->comment('分类名称');
            $table->unsignedBigInteger('parent_id')->nullable()->comment('父分类ID');
            $table->unsignedInteger('sort_order')->default(0)->comment('排序');
            $table->string('status', 20)->default('active')->comment('状态');
            $table->timestamps();
        });

        // 菜品表
        Schema::create('catering_dishes', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('category_id')->nullable()->comment('分类ID');
            $table->string('name', 100)->comment('菜品名称');
            $table->decimal('price', 10, 2)->default(0)->comment('售价');
            $table->decimal('cost', 10, 2)->default(0)->comment('成本');
            $table->string('description', 500)->nullable()->comment('描述');
            $table->string('image_url', 255)->nullable()->comment('图片URL');
            $table->string('unit', 20)->default('份')->comment('单位');
            $table->json('specifications')->nullable()->comment('规格');
            $table->json('allergens')->nullable()->comment('过敏原');
            $table->boolean('is_on_sale')->default(true)->comment('是否上架');
            $table->unsignedInteger('sort_order')->default(0)->comment('排序');
            $table->timestamps();
        });

        // 桌台表
        Schema::create('catering_tables', function (Blueprint $table) {
            $table->id();
            $table->string('table_number', 20)->comment('桌号');
            $table->string('area', 50)->nullable()->comment('区域');
            $table->unsignedInteger('seats')->default(4)->comment('座位数');
            $table->string('status', 20)->default('available')->comment('状态');
            $table->decimal('min_charge', 10, 2)->nullable()->comment('最低消费');
            $table->string('qr_code_url', 255)->nullable()->comment('二维码URL');
            $table->unsignedInteger('sort_order')->default(0)->comment('排序');
            $table->timestamps();
        });

        // 订单表
        Schema::create('catering_orders', function (Blueprint $table) {
            $table->id();
            $table->string('order_no', 32)->unique()->comment('订单号');
            $table->unsignedBigInteger('table_id')->nullable()->comment('桌台ID');
            $table->unsignedBigInteger('user_id')->nullable()->comment('用户ID');
            $table->string('order_type', 20)->default('dine_in')->comment('订单类型');
            $table->string('status', 20)->default('pending')->comment('订单状态');
            $table->decimal('total_amount', 10, 2)->default(0)->comment('总金额');
            $table->decimal('discount_amount', 10, 2)->default(0)->comment('折扣金额');
            $table->decimal('actual_amount', 10, 2)->default(0)->comment('实付金额');
            $table->string('payment_method', 20)->nullable()->comment('支付方式');
            $table->string('payment_status', 20)->default('unpaid')->comment('支付状态');
            $table->string('remark', 500)->nullable()->comment('备注');
            $table->timestamps();
        });

        // 订单明细表
        Schema::create('catering_order_items', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('order_id')->comment('订单ID');
            $table->unsignedBigInteger('dish_id')->nullable()->comment('菜品ID');
            $table->string('dish_name', 100)->comment('菜品名称');
            $table->decimal('price', 10, 2)->comment('单价');
            $table->unsignedInteger('quantity')->default(1)->comment('数量');
            $table->json('specifications')->nullable()->comment('规格');
            $table->string('remark', 255)->nullable()->comment('备注');
            $table->string('status', 20)->default('pending')->comment('状态');
            $table->timestamps();

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

        // 库存表
        Schema::create('catering_stocks', function (Blueprint $table) {
            $table->id();
            $table->string('ingredient_name', 100)->comment('食材名称');
            $table->string('category', 50)->nullable()->comment('分类');
            $table->string('unit', 20)->default('kg')->comment('单位');
            $table->decimal('current_stock', 10, 2)->default(0)->comment('当前库存');
            $table->decimal('min_stock', 10, 2)->default(0)->comment('最低库存');
            $table->decimal('unit_price', 10, 2)->default(0)->comment('单价');
            $table->unsignedBigInteger('supplier_id')->nullable()->comment('供应商ID');
            $table->date('expiry_date')->nullable()->comment('过期日期');
            $table->string('batch_number', 50)->nullable()->comment('批号');
            $table->timestamps();

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

        // 库存变动日志表
        Schema::create('catering_stock_logs', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('stock_id')->comment('库存ID');
            $table->string('type', 20)->comment('变动类型');
            $table->decimal('quantity', 10, 2)->comment('变动数量');
            $table->decimal('before_stock', 10, 2)->comment('变动前库存');
            $table->decimal('after_stock', 10, 2)->comment('变动后库存');
            $table->string('remark', 255)->nullable()->comment('备注');
            $table->timestamps();

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

        // 食品留样记录表
        // 依据《餐饮服务食品安全操作规范》7.5：留样 >= 125g + 48h
        Schema::create('catering_sample_records', function (Blueprint $table) {
            $table->id();
            $table->string('dish_name', 100)->comment('菜品名称');
            $table->dateTime('sample_time')->comment('留样时间');
            $table->decimal('sample_amount', 8, 2)->comment('留样量(g)');
            $table->string('storage_location', 100)->nullable()->comment('存放位置');
            $table->string('sampler', 50)->nullable()->comment('留样人');
            $table->dateTime('retention_until')->comment('保留至');
            $table->string('remark', 255)->nullable()->comment('备注');
            $table->timestamps();
        });

        // 进货查验记录表
        // 依据《食品安全法》第五十三条：记录保存 >= 2 年
        Schema::create('catering_inspection_records', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('supplier_id')->nullable()->comment('供应商ID');
            $table->string('ingredient_name', 100)->comment('食材名称');
            $table->decimal('quantity', 10, 2)->comment('数量');
            $table->string('unit', 20)->default('kg')->comment('单位');
            $table->date('production_date')->nullable()->comment('生产日期');
            $table->date('expiry_date')->nullable()->comment('过期日期');
            $table->string('batch_number', 50)->nullable()->comment('批号');
            $table->string('inspection_result', 20)->default('pending')->comment('检验结果');
            $table->string('inspector', 50)->nullable()->comment('检验人');
            $table->string('remark', 255)->nullable()->comment('备注');
            $table->date('retention_until')->nullable()->comment('保留至');
            $table->timestamps();
        });

        // 统计表
        Schema::create('catering_statistics', function (Blueprint $table) {
            $table->id();
            $table->date('date')->comment('统计日期');
            $table->decimal('revenue', 12, 2)->default(0)->comment('营收');
            $table->decimal('cost', 12, 2)->default(0)->comment('成本');
            $table->decimal('profit', 12, 2)->default(0)->comment('利润');
            $table->unsignedInteger('customer_count')->default(0)->comment('客流量');
            $table->unsignedInteger('order_count')->default(0)->comment('订单数');
            $table->decimal('avg_order_amount', 10, 2)->default(0)->comment('客单价');
            $table->timestamps();

            $table->unique('date');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('catering_statistics');
        Schema::dropIfExists('catering_inspection_records');
        Schema::dropIfExists('catering_sample_records');
        Schema::dropIfExists('catering_stock_logs');
        Schema::dropIfExists('catering_stocks');
        Schema::dropIfExists('catering_order_items');
        Schema::dropIfExists('catering_orders');
        Schema::dropIfExists('catering_tables');
        Schema::dropIfExists('catering_dishes');
        Schema::dropIfExists('catering_categories');
        Schema::dropIfExists('catering_suppliers');
    }
};