<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\AppSettingsRequest;
use App\Models\AppSetting;
use Illuminate\Support\Facades\Log;

class AdminSettingsController extends Controller
{
    public function show()
    {
        try {
            return view('admin.settings', [
                'otpEnabled' => AppSetting::get('otp_enabled', 'false'),
                'maintenanceMode' => AppSetting::get('maintenance_mode', 'false'),
                'transferReviewThreshold' => AppSetting::get('transfer_review_threshold', '10000'),
            ]);
        } catch (\Exception $e) {
            Log::error($e->getMessage());
            return back()->with('alert', 'An error occurred.');
        }
    }

    public function update(AppSettingsRequest $request)
    {
        try {
            AppSetting::set('otp_enabled', $request->otp_enabled);
            AppSetting::set('maintenance_mode', $request->maintenance_mode);
            AppSetting::set('transfer_review_threshold', $request->transfer_review_threshold);

            return redirect()->route('admin.settings')->with('success', 'Settings updated successfully.');
        } catch (\Exception $e) {
            Log::error($e->getMessage());
            return back()->with('alert', 'An error occurred.');
        }
    }
}
