<?php

namespace Tests;

use Illuminate\Support\Facades\Schema;

/**
 * Creates only the tables needed for virtual try-on tests directly in SQLite,
 * bypassing the full migration chain which has SQLite-incompatible operations.
 */
trait RefreshDatabaseWithoutSeeds
{
    protected function setUpRefreshDatabaseWithoutSeeds(): void
    {
        if (!Schema::hasTable('users')) {
            $this->createTestTables();
        }

        $this->truncateTestTables();
    }

    /**
     * Boot the trait in Laravel's TestCase setUp cycle.
     */
    protected function refreshDatabase()
    {
        $this->setUpRefreshDatabaseWithoutSeeds();
    }

    /**
     * Hook into TestCase setUp via the initializeTrait convention.
     */
    public function setUpTraits()
    {
        $uses = parent::setUpTraits();

        if (isset($uses[RefreshDatabaseWithoutSeeds::class])) {
            $this->refreshDatabase();
        }

        return $uses;
    }

    protected function createTestTables(): void
    {
        Schema::dropIfExists('virtual_tryon_results');
        Schema::dropIfExists('user_face_photos');
        Schema::dropIfExists('seo_metas');
        Schema::dropIfExists('product_details');
        Schema::dropIfExists('products');
        Schema::dropIfExists('users');

        Schema::create('users', function ($table) {
            $table->id();
            $table->string('name')->nullable();
            $table->string('slug')->nullable();
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();
            $table->string('full_name')->nullable();
            $table->string('username')->nullable();
            $table->string('email')->unique();
            $table->string('password');
            $table->string('avatar')->nullable();
            $table->string('remember_token')->nullable();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('identity_status')->nullable();
            $table->string('identity_document_type')->nullable();
            $table->integer('identity_rejection_count')->default(0);
            $table->timestamp('identity_submitted_at')->nullable();
            $table->timestamp('identity_approved_at')->nullable();
            $table->timestamp('identity_rejected_at')->nullable();
            $table->timestamp('tryon_consent_at')->nullable();
            $table->timestamps();
        });

        Schema::create('products', function ($table) {
            $table->id();
            $table->string('code')->nullable();
            $table->string('slug')->nullable();
            $table->string('name')->nullable();
            $table->string('image')->nullable();
            $table->integer('type')->default(0);
            $table->integer('status')->default(1);
            $table->boolean('is_activated')->default(true);
            $table->boolean('is_deleted')->default(false);
            $table->timestamps();
        });

        Schema::create('user_face_photos', function ($table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->string('file_path');
            $table->string('storage_path');
            $table->boolean('is_default')->default(false);
            $table->timestamps();
        });

        Schema::create('product_details', function ($table) {
            $table->id();
            $table->unsignedBigInteger('product_id');
            $table->string('language_code')->default('ja');
            $table->string('name')->nullable();
            $table->text('description')->nullable();
            $table->timestamps();
        });

        Schema::create('virtual_tryon_results', function ($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();
        });

        Schema::create('seo_metas', function ($table) {
            $table->id();
            $table->string('uri');
            $table->string('title')->nullable();
            $table->text('keywords')->nullable();
            $table->text('description')->nullable();
            $table->string('image')->nullable();
            $table->string('type')->nullable();
            $table->integer('post_id')->nullable();
            $table->timestamps();
        });
    }

    protected function truncateTestTables(): void
    {
        if (Schema::hasTable('virtual_tryon_results')) {
            \DB::table('virtual_tryon_results')->delete();
        }
        if (Schema::hasTable('user_face_photos')) {
            \DB::table('user_face_photos')->delete();
        }
        if (Schema::hasTable('products')) {
            \DB::table('products')->delete();
        }
        if (Schema::hasTable('users')) {
            \DB::table('users')->delete();
        }
        if (Schema::hasTable('seo_metas')) {
            \DB::table('seo_metas')->delete();
        }
    }
}
