<?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('warehouses', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->string('name', 50);
            $table->unsignedBigInteger('manager_id')->nullable();
            $table->tinyInteger('status')->default(1);
            $table->timestamps();
        });

        // 供应商
        Schema::create('suppliers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->string('name', 100);
            $table->string('contact', 50)->nullable();
            $table->string('phone', 20)->nullable();
            $table->string('address', 255)->nullable();
            $table->string('bank_info', 255)->nullable();
            $table->tinyInteger('status')->default(1);
            $table->timestamps();
        });

        // 物资（SKU）
        Schema::create('materials', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->string('name', 100);
            $table->string('code', 50)->nullable()->comment('SKU编码');
            $table->string('category', 50)->nullable();
            $table->string('unit', 20)->nullable()->comment('单位');
            $table->string('spec', 100)->nullable()->comment('规格');
            $table->integer('min_stock')->default(0)->comment('安全库存');
            $table->integer('shelf_life_days')->nullable()->comment('保质天数');
            $table->tinyInteger('status')->default(1);
            $table->timestamps();
            $table->index('store_id');
            $table->index('code');
        });

        // 库存
        Schema::create('inventories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('warehouse_id')->constrained();
            $table->foreignId('material_id')->constrained();
            $table->decimal('quantity', 12, 2)->default(0);
            $table->string('batch_no', 50)->nullable();
            $table->date('expire_date')->nullable();
            $table->timestamp('updated_at')->nullable();
            $table->unique(['warehouse_id', 'material_id', 'batch_no'], 'idx_wh_mat_batch');
        });

        // 采购单
        Schema::create('purchase_orders', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->string('order_no', 50)->unique();
            $table->unsignedBigInteger('supplier_id')->nullable();
            $table->decimal('total_amount', 12, 2)->nullable();
            $table->tinyInteger('status')->default(0)->comment('0草稿 1待审批 2已审批 3已入库 4已取消');
            $table->json('items')->nullable()->comment('采购明细');
            $table->unsignedBigInteger('audit_user_id')->nullable();
            $table->timestamp('audit_at')->nullable();
            $table->text('remark')->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->timestamps();
        });

        // 出入库单
        Schema::create('stock_movements', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->foreignId('warehouse_id')->constrained();
            $table->string('movement_no', 50)->unique();
            $table->tinyInteger('type')->comment('1入库 2出库 3调拨 4盘点');
            $table->tinyInteger('direction')->nullable()->comment('1入 2出');
            $table->json('items')->nullable()->comment('物资明细');
            $table->string('related_order', 50)->nullable()->comment('关联单号');
            $table->tinyInteger('status')->default(0)->comment('0草稿 1已确认');
            $table->unsignedBigInteger('operator_id')->nullable();
            $table->text('remark')->nullable();
            $table->timestamps();
            $table->index(['store_id', 'type']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('stock_movements');
        Schema::dropIfExists('purchase_orders');
        Schema::dropIfExists('inventories');
        Schema::dropIfExists('materials');
        Schema::dropIfExists('suppliers');
        Schema::dropIfExists('warehouses');
    }
};
