<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->timestamps();
        });

        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
            $table->foreignId('role_id')->constrained()->default(2);
            $table->boolean('email_notifications_enabled')->default(true);
            $table->string('theme_preference')->default('light');
            $table->string('locale')->default('en');
            $table->text('two_factor_secret')->nullable();
            $table->text('two_factor_recovery_codes')->nullable();
            $table->timestamp('two_factor_confirmed_at')->nullable();
        });

        Schema::create('api_providers', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->boolean('enabled')->default(true);
            $table->string('api_url');
            $table->string('username')->nullable();
            $table->string('password')->nullable();
            $table->string('token')->nullable();
            $table->string('plugin')->nullable();
            $table->timestamps();
        });

        Schema::create('ai_models', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('model_id');
            $table->string('model_type');
            $table->boolean('enabled')->default(true);
            $table->foreignId('api_provider_id')->nullable()->constrained();
            $table->json('parameters')->nullable();
            $table->timestamps();
        });

        Schema::create('styles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('prompt');
            $table->text('description');
            $table->string('preview_image');
            $table->text('parameters')->nullable();
            $table->foreignId('ai_model_id')->constrained();
            $table->boolean('enabled')->default(true);
            $table->integer('sort_order')->default(0);
            $table->timestamps();
        });

        Schema::create('images', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->nullable()->unique();
            $table->string('comfyui_prompt_id')->nullable();
            $table->unsignedBigInteger('original_image_id')->nullable();
            $table->foreign('original_image_id')->references('id')->on('images')->onDelete('set null');
            $table->unsignedInteger('style_id')->nullable();
            $table->foreign('style_id')->references('id')->on('styles')->onDelete('set null');
            $table->boolean('is_temp')->default(false);
            $table->string('path');
            $table->boolean('is_public')->default(false);
            $table->timestamps();
        });

        Schema::create('settings', function (Blueprint $table) {
            $table->id();
            $table->string('key')->unique();
            $table->text('value')->nullable();
            $table->timestamps();
        });


        Schema::create('password_reset_tokens', function (Blueprint $table) {
            $table->string('email')->primary();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });

        Schema::create('failed_jobs', function (Blueprint $table) {
            $table->id();
            $table->string('uuid')->unique();
            $table->text('connection');
            $table->text('queue');
            $table->longText('payload');
            $table->longText('exception');
            $table->timestamp('failed_at')->useCurrent();
        });

        Schema::create('personal_access_tokens', function (Blueprint $table) {
            $table->id();
            $table->morphs('tokenable');
            $table->string('name');
            $table->string('token', 64)->unique();
            $table->text('abilities')->nullable();
            $table->timestamp('last_used_at')->nullable();
            $table->timestamp('expires_at')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('personal_access_tokens');
        Schema::dropIfExists('failed_jobs');
        Schema::dropIfExists('password_reset_tokens');
        Schema::dropIfExists('settings');
        Schema::dropIfExists('images');
        Schema::dropIfExists('styles');
        Schema::dropIfExists('ai_models');
        Schema::dropIfExists('api_providers');
        Schema::dropIfExists('users');
        Schema::dropIfExists('roles');
    }
};