<?php

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

class CreateServiceRequestsTable extends Migration
{
    public function up()
    {
        Schema::create('service_requests', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->comment('申請者ID');
            $table->unsignedBigInteger('product_id')->nullable()->comment('対象商品ID');
            $table->unsignedSmallInteger('type')->comment('0=出品申請, 1=出品停止, 2=利用停止, 3=一時返却');
            $table->unsignedSmallInteger('status')->default(0)->comment('0=申請中, 1=対応中, 2=完了, 3=却下');
            $table->text('content')->nullable()->comment('申請内容・備考');
            $table->text('admin_note')->nullable()->comment('管理者メモ');
            $table->timestamp('completed_at')->nullable()->comment('対応完了日');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('product_id')->references('id')->on('products')->onDelete('set null');
            $table->index(['user_id', 'status']);
            $table->index('type');
        });
    }

    public function down()
    {
        Schema::dropIfExists('service_requests');
    }
}
