<?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::table('plans', function (Blueprint $table): void {
            $table->unsignedTinyInteger('max_fuel_types')->nullable()->after('stripe_price_id_annual')
                ->comment('Null = unlimited');

            $table->boolean('email_enabled')->default(true)->after('max_fuel_types');
            $table->string('email_frequency', 20)->default('weekly_digest')->after('email_enabled')
                ->comment('weekly_digest | daily | triggered');

            $table->boolean('push_enabled')->default(false)->after('email_frequency');
            $table->string('push_frequency', 20)->default('none')->after('push_enabled')
                ->comment('none | daily | triggered');

            $table->boolean('whatsapp_enabled')->default(false)->after('push_frequency');
            $table->unsignedSmallInteger('whatsapp_daily_limit')->default(0)->after('whatsapp_enabled');
            $table->unsignedTinyInteger('whatsapp_scheduled_updates')->default(0)->after('whatsapp_daily_limit');

            $table->boolean('sms_enabled')->default(false)->after('whatsapp_scheduled_updates');
            $table->unsignedSmallInteger('sms_daily_limit')->default(0)->after('sms_enabled');

            $table->boolean('ai_predictions')->default(false)->after('sms_daily_limit');
            $table->boolean('price_threshold')->default(false)->after('ai_predictions');
            $table->boolean('score_alerts')->default(false)->after('price_threshold');

            $table->dropColumn('features');
        });
    }

    public function down(): void
    {
        Schema::table('plans', function (Blueprint $table): void {
            $table->json('features')->after('stripe_price_id_annual');

            $table->dropColumn([
                'max_fuel_types',
                'email_enabled',
                'email_frequency',
                'push_enabled',
                'push_frequency',
                'whatsapp_enabled',
                'whatsapp_daily_limit',
                'whatsapp_scheduled_updates',
                'sms_enabled',
                'sms_daily_limit',
                'ai_predictions',
                'price_threshold',
                'score_alerts',
            ]);
        });
    }
};
