<?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
    {
        // 1. Ensure 'order' column in Banners
        if (Schema::hasTable('banners')) {
            Schema::table('banners', function (Blueprint $table) {
                if (!Schema::hasColumn('banners', 'order')) {
                    $table->integer('order')->default(0)->after('active');
                }
            });
        }

        // 2. Ensure 'order' column in Testimonials
        if (Schema::hasTable('testimonials')) {
            Schema::table('testimonials', function (Blueprint $table) {
                if (!Schema::hasColumn('testimonials', 'order')) {
                    $table->integer('order')->default(0);
                }
            });
        }

        // 3. Ensure 'order' column in FAQs
        if (Schema::hasTable('faqs')) {
            Schema::table('faqs', function (Blueprint $table) {
                if (!Schema::hasColumn('faqs', 'order')) {
                    $table->integer('order')->default(0);
                }
            });
        }
        
        // 4. Ensure microsite_settings exists (just in case)
        if (!Schema::hasTable('microsite_settings')) {
             Schema::create('microsite_settings', function (Blueprint $table) {
                $table->id();
                $table->foreignId('seller_id')->constrained()->onDelete('cascade');
                $table->string('tagline')->nullable();
                $table->string('cta_text')->default('Contact Us');
                $table->string('cta_link')->nullable();
                $table->boolean('show_stats')->default(true);
                $table->integer('years_in_business')->nullable();
                $table->integer('happy_clients')->nullable();
                $table->integer('products_sold')->nullable();
                $table->boolean('show_about')->default(true);
                $table->boolean('show_products')->default(true);
                $table->boolean('show_certificates')->default(true);
                $table->boolean('show_testimonials')->default(true);
                $table->boolean('show_faq')->default(true);
                $table->boolean('show_video')->default(false);
                $table->boolean('show_contact')->default(true);
                $table->string('video_url')->nullable();
                $table->string('video_title')->nullable();
                $table->string('brochure_path')->nullable();
                $table->unsignedBigInteger('page_views')->default(0);
                $table->unsignedBigInteger('inquiry_count')->default(0);
                $table->string('meta_title')->nullable();
                $table->text('meta_description')->nullable();
                $table->string('meta_keywords')->nullable();
                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        // No down action to avoid data loss
    }
};
