<?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
    {
        // Blog Categories
        if (!Schema::hasTable('blog_categories')) {
            Schema::create('blog_categories', function (Blueprint $table) {
                $table->id();
                $table->string('name')->nullable();
                $table->string('slug')->unique();
                $table->longText('description')->nullable();
                $table->json('translations')->nullable(); // From add_translations
                $table->boolean('is_visible')->default(false);
                $table->date('deleted_at')->nullable();
                $table->timestamps();
            });
        } else {
            if (!Schema::hasColumn('blog_categories', 'name')) {
                Schema::table('blog_categories', function (Blueprint $table) {
                    $table->string('name')->nullable()->after('id');
                    $table->longText('description')->nullable()->after('name');
                });
            }
            if (!Schema::hasColumn('blog_categories', 'translations')) {
                Schema::table('blog_categories', function (Blueprint $table) {
                    $table->json('translations')->nullable()->after('description');
                });
            }
        }

        // Blog Authors
        if (!Schema::hasTable('blog_authors')) {
            Schema::create('blog_authors', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->string('email')->unique();
                $table->string('photo')->nullable();
                $table->longText('bio')->nullable();
                $table->string('github_handle')->nullable();
                $table->string('twitter_handle')->nullable();
                $table->timestamps();
            });
        }

        // Blog Posts
        if (!Schema::hasTable('blog_posts')) {
            Schema::create('blog_posts', function (Blueprint $table) {
                $table->id();
                $table->foreignId('blog_author_id')->nullable()->constrained()->cascadeOnDelete();
                $table->foreignId('blog_category_id')->nullable()->constrained()->nullOnDelete();
                $table->string('title');
                $table->string('slug')->unique();
                $table->text('excerpt')->nullable();
                $table->string('banner')->nullable();
                $table->longText('content');
                $table->json('translations')->nullable(); // From add_translations
                $table->date('published_at')->nullable();
                $table->date('deleted_at')->nullable();
                $table->timestamps();
            });
        } else {
            if (!Schema::hasColumn('blog_posts', 'translations')) {
                Schema::table('blog_posts', function (Blueprint $table) {
                    $table->json('translations')->nullable()->after('content');
                });
            }
        }

        // Tags
        if (!Schema::hasTable('tags')) {
            Schema::create('tags', function (Blueprint $table) {
                $table->id();
                $table->json('name');
                $table->json('slug');
                $table->string('type')->nullable();
                $table->integer('order_column')->nullable();
                $table->timestamps();
            });
        }

        // Taggables (polymorphic)
        if (!Schema::hasTable('taggables')) {
            Schema::create('taggables', function (Blueprint $table) {
                $table->foreignId('tag_id')->constrained()->cascadeOnDelete();
                $table->morphs('taggable');
                $table->unique(['tag_id', 'taggable_id', 'taggable_type']);
            });
        }
    }

    public function down(): void
    {
        if (app()->environment('local', 'testing')) {
            if (Schema::hasTable('taggables')) {
                Schema::table('taggables', function (Blueprint $table) {
                    $table->dropForeign(['tag_id']);
                });
                Schema::dropIfExists('taggables');
            }
            Schema::dropIfExists('tags');
            if (Schema::hasColumn('blog_posts', 'translations')) {
                Schema::table('blog_posts', function (Blueprint $table) {
                    $table->dropColumn('translations');
                });
            }
            Schema::dropIfExists('blog_posts');
            Schema::dropIfExists('blog_authors');
            if (Schema::hasColumn('blog_categories', 'translations')) {
                Schema::table('blog_categories', function (Blueprint $table) {
                    $table->dropColumn('translations');
                });
            }
            if (Schema::hasColumn('blog_categories', 'name')) {
                Schema::table('blog_categories', function (Blueprint $table) {
                    $table->dropColumn(['name', 'description']);
                });
            }
            Schema::dropIfExists('blog_categories');
        }
    }
};