<?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
    {
        // Photos table
        if (! Schema::hasTable('photos')) {
            Schema::create('photos', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('event_id');
                $table->unsignedBigInteger('emotion_id')->nullable();
                $table->unsignedBigInteger('task_id')->nullable();
                $table->string('guest_name');
                $table->string('file_path');
                $table->string('thumbnail_path');
                $table->integer('likes_count')->default(0);
                $table->boolean('is_featured')->default(false);
                $table->json('metadata')->nullable();
                $table->unsignedBigInteger('tenant_id')->nullable(); // Consolidated from adds
                $table->timestamps();
                $table->foreign('event_id')->references('id')->on('events')->onDelete('cascade');
                $table->foreign('emotion_id')->references('id')->on('emotions')->onDelete('set null');
                $table->foreign('task_id')->references('id')->on('tasks')->onDelete('set null');
                $table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
                $table->index(['event_id', 'emotion_id', 'tenant_id']);
            });
        } else {
            // Add tenant_id if missing (consolidate duplicates)
            if (! Schema::hasColumn('photos', 'tenant_id')) {
                Schema::table('photos', function (Blueprint $table) {
                    $table->unsignedBigInteger('tenant_id')->nullable()->after('event_id');
                    $table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
                    $table->index('tenant_id');
                });
            }
        }

        // Photo Likes table
        if (! Schema::hasTable('photo_likes')) {
            Schema::create('photo_likes', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('photo_id');
                $table->string('guest_name');
                $table->string('ip_address', 45)->nullable();
                $table->timestamp('created_at')->useCurrent();
                $table->unique(['photo_id', 'guest_name', 'ip_address']);
                $table->foreign('photo_id')->references('id')->on('photos')->onDelete('cascade');
            });
        }
    }

    public function down(): void
    {
        if (app()->environment('local', 'testing')) {
            if (Schema::hasTable('photo_likes')) {
                Schema::table('photo_likes', function (Blueprint $table) {
                    $table->dropForeign(['photo_id']);
                });
                Schema::dropIfExists('photo_likes');
            }
            if (Schema::hasTable('photos')) {
                Schema::table('photos', function (Blueprint $table) {
                    $table->dropForeign(['event_id']);
                    $table->dropForeign(['emotion_id']);
                    $table->dropForeign(['task_id']);
                    $table->dropForeign(['tenant_id']);
                });
                Schema::dropIfExists('photos');
            }
        }
    }
};
