<?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
    {
        if (! Schema::hasTable('stores')) {
            Schema::create('stores', function (Blueprint $table) {
                $table->id();
                $table->string('name', 150);
                $table->string('slug', 160)->unique();
                $table->string('type', 30)->nullable();
                $table->text('description')->nullable();
                $table->string('neighborhood', 100)->nullable();
                $table->string('address_line', 255)->nullable();
                $table->string('reference_point', 255)->nullable();
                $table->decimal('latitude', 10, 8)->nullable();
                $table->decimal('longitude', 11, 8)->nullable();
                $table->string('phone', 30)->nullable();
                $table->string('whatsapp', 30)->nullable();
                $table->string('email', 150)->nullable();
                $table->string('website', 255)->nullable();
                $table->string('photo', 255)->nullable();
                $table->boolean('is_active')->default(true);
                $table->string('status', 30)->nullable()->default('open');
                $table->unsignedInteger('sort_order')->default(0);
                $table->softDeletes();
                $table->timestamps();

                $table->index(['is_active', 'sort_order']);
            });
        }

        if (! Schema::hasTable('products')) {
            Schema::create('products', function (Blueprint $table) {
                $table->id();
                $table->string('name', 200);
                $table->string('slug', 220)->unique();
                $table->text('description')->nullable();
                $table->decimal('price', 10, 2);
                $table->decimal('original_price', 10, 2)->nullable();
                $table->string('badge', 100)->nullable();
                $table->string('badge_color', 30)->nullable();
                $table->string('image', 255)->nullable();
                $table->boolean('is_active')->default(true);
                $table->boolean('featured')->default(false);
                $table->unsignedInteger('sort_order')->default(0);
                $table->softDeletes();
                $table->timestamps();

                $table->index(['is_active', 'featured', 'sort_order']);
            });
        }

        if (! Schema::hasTable('categories')) {
            Schema::create('categories', function (Blueprint $table) {
                $table->id();
                $table->string('name', 100);
                $table->string('slug', 120)->unique();
                $table->string('image', 255)->nullable();
                $table->boolean('show_in_hero')->default(true);
                $table->unsignedInteger('sort_order')->default(0);
                $table->boolean('is_active')->default(true);
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('campaigns')) {
            Schema::create('campaigns', function (Blueprint $table) {
                $table->id();
                $table->string('name', 150);
                $table->string('slug', 180)->unique();
                $table->string('objective', 255);
                $table->string('image', 255)->nullable();
                $table->string('status', 20);
                $table->timestamp('starts_at')->nullable();
                $table->timestamp('ends_at')->nullable();
                $table->unsignedInteger('priority')->default(0);
                $table->boolean('is_pinned')->default(false);
                $table->string('seo_title', 255)->nullable();
                $table->string('seo_description', 500)->nullable();
                $table->text('seo_keywords')->nullable();
                $table->string('seo_canonical', 500)->nullable();
                $table->string('seo_og_title', 255)->nullable();
                $table->string('seo_og_description', 500)->nullable();
                $table->string('seo_twitter_title', 255)->nullable();
                $table->string('seo_twitter_description', 500)->nullable();
                $table->timestamps();

                $table->index('status');
                $table->index('starts_at');
                $table->index('ends_at');
                $table->index('priority');
                $table->index('is_pinned');
            });
        }

        if (! Schema::hasTable('campaign_assets')) {
            Schema::create('campaign_assets', function (Blueprint $table) {
                $table->id();
                $table->foreignId('campaign_id')->constrained()->cascadeOnDelete();
                $table->string('purpose', 30);
                $table->string('file_path', 500);
                $table->string('alt_text', 255);
                $table->unsignedInteger('width');
                $table->unsignedInteger('height');
                $table->string('mime_type', 100);
                $table->unsignedBigInteger('size');
                $table->unsignedInteger('sort_order')->default(0);
                $table->timestamps();

                $table->unique(['campaign_id', 'purpose', 'sort_order']);
            });
        }

        if (! Schema::hasTable('campaign_ctas')) {
            Schema::create('campaign_ctas', function (Blueprint $table) {
                $table->id();
                $table->foreignId('campaign_id')->constrained()->cascadeOnDelete();
                $table->string('label', 100);
                $table->string('destination', 500);
                $table->unsignedInteger('sort_order')->default(0);
                $table->timestamps();

                $table->unique(['campaign_id', 'sort_order']);
            });
        }

        if (! Schema::hasTable('news_articles')) {
            Schema::create('news_articles', function (Blueprint $table) {
                $table->id();
                $table->string('title', 200);
                $table->string('slug', 220)->unique();
                $table->text('excerpt')->nullable();
                $table->text('content');
                $table->string('image', 255)->nullable();
                $table->string('category', 100);
                $table->string('author', 150)->nullable();
                $table->timestamp('published_at')->nullable();
                $table->string('status', 20)->default('draft');
                $table->unsignedSmallInteger('reading_time')->nullable();
                $table->boolean('featured')->default(false);
                $table->string('seo_title', 255)->nullable();
                $table->text('seo_description')->nullable();
                $table->timestamps();

                $table->index('status');
                $table->index('featured');
            });
        }

        if (! Schema::hasTable('partners')) {
            Schema::create('partners', function (Blueprint $table) {
                $table->id();
                $table->string('name', 150);
                $table->string('slug', 180)->nullable()->unique();
                $table->string('logo', 255)->nullable();
                $table->string('segment', 100)->nullable();
                $table->text('description')->nullable();
                $table->string('level', 30)->default('standard');
                $table->string('href', 255)->nullable();
                $table->integer('since')->nullable();
                $table->unsignedInteger('display_order')->default(0);
                $table->boolean('is_active')->default(true);
                $table->string('seo_title', 255)->nullable();
                $table->text('seo_description')->nullable();
                $table->timestamps();

                $table->index('display_order');
                $table->index('is_active');
            });
        }

        if (! Schema::hasTable('vacancies')) {
            Schema::create('vacancies', function (Blueprint $table) {
                $table->id();
                $table->string('title', 200);
                $table->string('slug', 180)->nullable()->unique();
                $table->string('area', 100);
                $table->string('location', 100);
                $table->string('type', 50)->default('full-time');
                $table->text('description');
                $table->string('href', 255)->nullable();
                $table->date('deadline')->nullable();
                $table->boolean('remote')->default(false);
                $table->string('status', 20)->default('draft');
                $table->string('seo_title', 255)->nullable();
                $table->text('seo_description')->nullable();
                $table->timestamps();

                $table->index('status');
            });
        }

        if (! Schema::hasTable('settings')) {
            Schema::create('settings', function (Blueprint $table) {
                $table->id();
                $table->string('group', 50)->default('general');
                $table->string('key', 100);
                $table->text('value')->nullable();
                $table->string('description', 255)->nullable();
                $table->boolean('is_active')->default(true);
                $table->timestamps();

                $table->unique(['group', 'key']);
            });
        }

        if (! Schema::hasTable('seo_metadata')) {
            Schema::create('seo_metadata', function (Blueprint $table) {
                $table->id();
                $table->string('page_name', 80)->unique();
                $table->string('title', 255)->nullable();
                $table->string('description', 500)->nullable();
                $table->string('og_image', 255)->nullable();
                $table->string('canonical', 500)->nullable();
                $table->string('robots', 100)->nullable();
                $table->text('keywords')->nullable();
                $table->string('og_title', 255)->nullable();
                $table->string('og_description', 500)->nullable();
                $table->string('twitter_title', 255)->nullable();
                $table->string('twitter_description', 500)->nullable();
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('team_members')) {
            Schema::create('team_members', function (Blueprint $table) {
                $table->id();
                $table->string('name', 200);
                $table->string('role', 200);
                $table->string('category', 30);
                $table->text('bio')->nullable();
                $table->string('photo', 255)->nullable();
                $table->unsignedSmallInteger('sort_order')->default(0);
                $table->boolean('is_active')->default(true);
                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        foreach ([
            'team_members', 'seo_metadata', 'settings', 'vacancies',
            'partners', 'news_articles', 'campaign_ctas', 'campaign_assets',
            'campaigns', 'categories', 'products', 'stores',
        ] as $table) {
            if (Schema::hasTable($table)) {
                Schema::dropIfExists($table);
            }
        }
    }
};
