<?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
    {
        Schema::create('class_types', function (Blueprint $table) {
            $table->id();
            $table->string('slug')->unique();
            $table->string('number', 2);
            $table->string('name');
            $table->text('description');
            $table->unsignedSmallInteger('duration_minutes');
            $table->string('level_label');
            $table->unsignedTinyInteger('intensity')->default(3);
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('coaches', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique();
            $table->string('role');
            $table->text('bio')->nullable();
            $table->string('photo_path')->nullable();
            $table->string('media_tag')->nullable();
            $table->json('tags')->nullable();
            $table->boolean('show_on_home')->default(false);
            $table->boolean('show_on_about')->default(false);
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('class_sessions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('class_type_id')->constrained()->cascadeOnDelete();
            $table->foreignId('coach_id')->constrained()->cascadeOnDelete();
            $table->string('day_of_week', 3);
            $table->time('start_time');
            $table->unsignedSmallInteger('duration_minutes')->nullable();
            $table->string('spots_label')->nullable();
            $table->string('availability')->default('open');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('schedule_settings', function (Blueprint $table) {
            $table->id();
            $table->date('week_start_date');
            $table->timestamps();
        });

        Schema::create('membership_tiers', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique();
            $table->text('description')->nullable();
            $table->unsignedInteger('price_cents');
            $table->string('currency', 3)->default('AED');
            $table->string('billing_period');
            $table->boolean('is_featured')->default(false);
            $table->string('badge_text')->nullable();
            $table->string('cta_variant')->default('ghost');
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('membership_tier_features', function (Blueprint $table) {
            $table->id();
            $table->foreignId('membership_tier_id')->constrained()->cascadeOnDelete();
            $table->string('text');
            $table->boolean('is_included')->default(true);
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();
        });

        Schema::create('class_packs', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->unsignedInteger('price_cents');
            $table->string('per_unit_label')->nullable();
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('testimonials', function (Blueprint $table) {
            $table->id();
            $table->text('quote');
            $table->string('author_name');
            $table->string('author_role')->nullable();
            $table->string('avatar_path')->nullable();
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->boolean('is_featured')->default(true);
            $table->timestamps();
        });

        Schema::create('gallery_sections', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('subtitle')->nullable();
            $table->string('slug')->unique();
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();
        });

        Schema::create('gallery_images', function (Blueprint $table) {
            $table->id();
            $table->foreignId('gallery_section_id')->nullable()->constrained()->nullOnDelete();
            $table->string('image_path');
            $table->string('caption')->nullable();
            $table->string('height_class')->default('h-md');
            $table->string('grid_class')->nullable();
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->boolean('is_featured')->default(false);
            $table->timestamps();
        });

        Schema::create('faqs', function (Blueprint $table) {
            $table->id();
            $table->string('question');
            $table->text('answer');
            $table->string('context')->default('membership');
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();
        });

        Schema::create('content_blocks', function (Blueprint $table) {
            $table->id();
            $table->string('page');
            $table->string('section_key');
            $table->string('title')->nullable();
            $table->text('body')->nullable();
            $table->json('meta')->nullable();
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();

            $table->unique(['page', 'section_key']);
        });

        Schema::create('site_settings', function (Blueprint $table) {
            $table->id();
            $table->string('key')->unique();
            $table->json('value');
            $table->timestamps();
        });

        Schema::create('contact_submissions', function (Blueprint $table) {
            $table->id();
            $table->string('first_name');
            $table->string('last_name')->nullable();
            $table->string('email');
            $table->string('phone')->nullable();
            $table->string('interest');
            $table->text('message');
            $table->string('ip_address', 45)->nullable();
            $table->timestamp('read_at')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('contact_submissions');
        Schema::dropIfExists('site_settings');
        Schema::dropIfExists('content_blocks');
        Schema::dropIfExists('faqs');
        Schema::dropIfExists('gallery_images');
        Schema::dropIfExists('gallery_sections');
        Schema::dropIfExists('testimonials');
        Schema::dropIfExists('class_packs');
        Schema::dropIfExists('membership_tier_features');
        Schema::dropIfExists('membership_tiers');
        Schema::dropIfExists('schedule_settings');
        Schema::dropIfExists('class_sessions');
        Schema::dropIfExists('coaches');
        Schema::dropIfExists('class_types');
    }
};
