<?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('homepage_sections', function (Blueprint $table): void {
            $table->id();
            $table->string('key')->unique();
            $table->string('title')->nullable();
            $table->text('subtitle')->nullable();
            $table->string('slogan')->nullable();
            $table->string('cta_text')->nullable();
            $table->string('cta_link')->nullable();
            $table->longText('content')->nullable();
            $table->boolean('is_active')->default(true);
            $table->unsignedInteger('sort_order')->default(0);
            $table->timestamps();
        });

        Schema::create('homepage_features', function (Blueprint $table): void {
            $table->id();
            $table->string('title');
            $table->text('description')->nullable();
            $table->string('icon')->nullable();
            $table->boolean('is_active')->default(true);
            $table->unsignedInteger('sort_order')->default(0);
            $table->timestamps();
        });

        Schema::table('challenges', function (Blueprint $table): void {
            $table->longText('rules')->nullable()->after('description');
            $table->longText('rewards')->nullable()->after('rules');
        });

        Schema::create('badges', function (Blueprint $table): void {
            $table->id();
            $table->string('name')->unique();
            $table->text('description')->nullable();
            $table->string('icon')->nullable();
            $table->string('requirement_key')->nullable();
            $table->text('requirement')->nullable();
            $table->boolean('is_active')->default(true);
            $table->unsignedInteger('sort_order')->default(0);
            $table->timestamps();
        });

        Schema::table('mistake_tags', function (Blueprint $table): void {
            $table->boolean('is_active')->default(true)->after('is_default');
        });

        Schema::create('emotion_tags', function (Blueprint $table): void {
            $table->id();
            $table->string('name')->unique();
            $table->string('description')->nullable();
            $table->boolean('is_active')->default(true);
            $table->unsignedInteger('sort_order')->default(0);
            $table->timestamps();
        });

        Schema::create('discipline_score_rules', function (Blueprint $table): void {
            $table->id();
            $table->string('key')->unique();
            $table->string('name');
            $table->unsignedSmallInteger('points')->default(10);
            $table->boolean('is_active')->default(true);
            $table->unsignedInteger('sort_order')->default(0);
            $table->timestamps();
        });

        Schema::create('mentor_settings', function (Blueprint $table): void {
            $table->id();
            $table->boolean('mentor_system_enabled')->default(true);
            $table->decimal('mentor_plan_price', 12, 2)->default(0);
            $table->unsignedInteger('max_students_per_plan')->default(20);
            $table->timestamps();
        });

        Schema::create('public_profile_settings', function (Blueprint $table): void {
            $table->id();
            $table->boolean('public_profiles_enabled')->default(true);
            $table->boolean('allow_profit_visibility')->default(true);
            $table->boolean('allow_discipline_visibility')->default(true);
            $table->boolean('allow_profile_sharing')->default(true);
            $table->timestamps();
        });

        Schema::create('referral_settings', function (Blueprint $table): void {
            $table->id();
            $table->boolean('referrals_enabled')->default(true);
            $table->string('invite_3_reward')->nullable();
            $table->string('invite_10_reward')->nullable();
            $table->string('mentor_20_students_reward')->nullable();
            $table->text('bonus_text')->nullable();
            $table->string('status')->default('active');
            $table->timestamps();
        });

        Schema::create('lead_magnets', function (Blueprint $table): void {
            $table->id();
            $table->string('title');
            $table->text('description')->nullable();
            $table->string('file_path')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('share_report_settings', function (Blueprint $table): void {
            $table->id();
            $table->boolean('share_reports_enabled')->default(true);
            $table->string('branding_text')->default('ViZuTrade Weekly Report');
            $table->string('watermark')->default('Trade. Journal. Improve.');
            $table->boolean('allow_profit_display')->default(true);
            $table->string('default_template')->default('blue-classic');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('share_report_settings');
        Schema::dropIfExists('lead_magnets');
        Schema::dropIfExists('referral_settings');
        Schema::dropIfExists('public_profile_settings');
        Schema::dropIfExists('mentor_settings');
        Schema::dropIfExists('discipline_score_rules');
        Schema::dropIfExists('emotion_tags');

        Schema::table('mistake_tags', function (Blueprint $table): void {
            $table->dropColumn('is_active');
        });

        Schema::dropIfExists('badges');

        Schema::table('challenges', function (Blueprint $table): void {
            $table->dropColumn(['rules', 'rewards']);
        });

        Schema::dropIfExists('homepage_features');
        Schema::dropIfExists('homepage_sections');
    }
};
