<?php

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

class AddSidebarModuleTogglesToSettingsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // Add new sidebar module toggle keys to the existing modules JSON column
        $settings = \App\Models\Settings::find(1);
        if ($settings) {
            $modules = $settings->modules ?? [];

            // Add new sidebar module keys with defaults (all enabled)
            $newKeys = [
                'investment_plans' => true,
                'car_inventory'    => true,
                'live_trading'     => true,
                'copy_trading'     => true,
                'ai_bots'          => true,
                'signals'          => true,
                'wallet'           => true,
                'loans'            => true,
                'referral'         => true,
            ];

            foreach ($newKeys as $key => $default) {
                if (!array_key_exists($key, $modules)) {
                    $modules[$key] = $default;
                }
            }

            $settings->modules = $modules;
            $settings->save();
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        $settings = \App\Models\Settings::find(1);
        if ($settings) {
            $modules = $settings->modules ?? [];
            $removeKeys = [
                'investment_plans', 'car_inventory', 'live_trading',
                'copy_trading', 'ai_bots', 'signals', 'wallet', 'loans', 'referral',
            ];
            foreach ($removeKeys as $key) {
                unset($modules[$key]);
            }
            $settings->modules = $modules;
            $settings->save();
        }
    }
}
