<?php

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

class CreateVirtualTryonResultsTable extends Migration
{
    public function up()
    {
        Schema::create('virtual_tryon_results', function (Blueprint $table) {
            $table->id();
            $table->string('code')->unique();
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('product_id');
            $table->unsignedBigInteger('face_photo_id')->nullable();
            $table->string('source_image');
            $table->string('result_image')->nullable();
            $table->string('segmind_request_id')->nullable();
            $table->string('status')->default('pending');
            $table->text('error')->nullable();
            $table->boolean('is_saved')->default(false);
            $table->timestamp('expires_at')->nullable();
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->foreign('face_photo_id')->references('id')->on('user_face_photos')->onDelete('set null');
            $table->index(['user_id', 'is_saved', 'expires_at']);
            $table->index('code');
            $table->index('expires_at');
        });
    }

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