<?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('dishes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->string('name', 50);
            $table->string('category', 30)->nullable()->comment('汤/主食/配菜/甜品');
            $table->text('description')->nullable();
            $table->string('image', 255)->nullable();
            $table->json('ingredients')->nullable();
            $table->json('contraindications')->nullable()->comment('禁忌');
            $table->decimal('price', 8, 2)->default(0);
            $table->tinyInteger('status')->default(1);
            $table->timestamps();
            $table->index('store_id');
        });

        // 排餐模板
        Schema::create('meal_plan_templates', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->string('name', 50);
            $table->string('stage', 30)->nullable()->comment('适用阶段');
            $table->json('meals')->nullable();
            $table->tinyInteger('status')->default(1);
            $table->timestamps();
        });

        // 每日排餐
        Schema::create('daily_meal_plans', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->foreignId('customer_id')->constrained();
            $table->date('plan_date');
            $table->tinyInteger('meal_type')->comment('1早餐 2午餐 3下午茶 4晚餐 5宵夜');
            $table->json('dishes')->nullable();
            $table->text('special_note')->nullable();
            $table->tinyInteger('status')->default(0)->comment('0待备餐 1已备餐 2已送达 3未用');
            $table->timestamp('deliver_at')->nullable();
            $table->timestamps();
            $table->index(['customer_id', 'plan_date']);
            $table->index(['store_id', 'plan_date']);
        });

        // 膳食评价
        Schema::create('meal_reviews', function (Blueprint $table) {
            $table->id();
            $table->foreignId('daily_meal_plan_id')->constrained()->onDelete('cascade');
            $table->foreignId('customer_id')->constrained();
            $table->tinyInteger('score')->nullable()->comment('1-5星');
            $table->text('content')->nullable();
            $table->timestamp('created_at')->nullable();
            $table->index('daily_meal_plan_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('meal_reviews');
        Schema::dropIfExists('daily_meal_plans');
        Schema::dropIfExists('meal_plan_templates');
        Schema::dropIfExists('dishes');
    }
};
