<?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. Update plans table with limits
        Schema::table('plans', function (Blueprint $table) {
            if (!Schema::hasColumn('plans', 'banner_limit')) {
                $table->integer('banner_limit')->default(1)->after('price_usd');
            }
            if (!Schema::hasColumn('plans', 'has_certificates')) {
                $table->boolean('has_certificates')->default(false)->after('banner_limit');
            }
            if (!Schema::hasColumn('plans', 'has_video')) {
                $table->boolean('has_video')->default(false)->after('has_certificates');
            }
            if (!Schema::hasColumn('plans', 'custom_page_limit')) {
                $table->integer('custom_page_limit')->default(0)->after('has_video');
            }
        });

        // 2. Create seller_banners table
        if (!Schema::hasTable('seller_banners')) {
            Schema::create('seller_banners', function (Blueprint $table) {
                $table->id();
                $table->foreignId('seller_id')->constrained()->onDelete('cascade');
                $table->string('image_path');
                $table->string('title')->nullable();
                $table->string('link')->nullable();
                $table->integer('order')->default(0);
                $table->timestamps();
            });
        }

        // 3. Create seller_certificates table
        if (!Schema::hasTable('seller_certificates')) {
            Schema::create('seller_certificates', function (Blueprint $table) {
                $table->id();
                $table->foreignId('seller_id')->constrained()->onDelete('cascade');
                $table->string('image_path');
                $table->string('title')->nullable();
                $table->timestamps();
            });
        }

        // 4. Create seller_pages table
        if (!Schema::hasTable('seller_pages')) {
            Schema::create('seller_pages', function (Blueprint $table) {
                $table->id();
                $table->foreignId('seller_id')->constrained()->onDelete('cascade');
                $table->string('title');
                $table->string('slug');
                $table->longText('content');
                $table->boolean('is_active')->default(true);
                $table->timestamps();
                
                // Ensure unique slug per seller
                $table->unique(['seller_id', 'slug']);
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('seller_pages');
        Schema::dropIfExists('seller_certificates');
        Schema::dropIfExists('seller_banners');

        Schema::table('plans', function (Blueprint $table) {
            $table->dropColumn(['banner_limit', 'has_certificates', 'has_video', 'custom_page_limit']);
        });
    }
};
