<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     * Updates user_settings table for backend-controlled features
     */
    public function up(): void
    {
        Schema::table('user_settings', function (Blueprint $table) {
            // Backend-controlled sync setting
            if (!Schema::hasColumn('user_settings', 'sync_enabled')) {
                $table->boolean('sync_enabled')->default(true)->after('reply_to_outgoing');
            }

            // Sync interval in minutes
            if (!Schema::hasColumn('user_settings', 'sync_interval_minutes')) {
                $table->integer('sync_interval_minutes')->default(15)->after('sync_enabled');
            }

            // Promotion settings
            if (!Schema::hasColumn('user_settings', 'promotion_on_incoming')) {
                $table->boolean('promotion_on_incoming')->default(false)->after('sync_interval_minutes');
            }
            if (!Schema::hasColumn('user_settings', 'promotion_on_outgoing')) {
                $table->boolean('promotion_on_outgoing')->default(false)->after('promotion_on_incoming');
            }
            if (!Schema::hasColumn('user_settings', 'promotion_on_missed')) {
                $table->boolean('promotion_on_missed')->default(false)->after('promotion_on_outgoing');
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('user_settings', function (Blueprint $table) {
            $table->dropColumn([
                'sync_enabled',
                'sync_interval_minutes',
                'promotion_on_incoming',
                'promotion_on_outgoing',
                'promotion_on_missed'
            ]);
        });
    }
};
