<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\AdminNotificationSetting;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class AdminNotificationSettingsController extends Controller
{
    public function edit(): Response
    {
        $settings = AdminNotificationSetting::current();

        return Inertia::render('admin/notification-settings', [
            'settings' => [
                'notify_new_user_registered' => $settings->notify_new_user_registered,
                'notify_investment_pending' => $settings->notify_investment_pending,
                'notify_withdrawal_requested' => $settings->notify_withdrawal_requested,
                'notify_kyc_submitted' => $settings->notify_kyc_submitted,
            ],
        ]);
    }

    public function update(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'notify_new_user_registered' => ['required', 'boolean'],
            'notify_investment_pending' => ['required', 'boolean'],
            'notify_withdrawal_requested' => ['required', 'boolean'],
            'notify_kyc_submitted' => ['required', 'boolean'],
        ]);

        AdminNotificationSetting::current()->update($validated);

        Inertia::flash('toast', [
            'type' => 'success',
            'message' => 'Preferencias de notificación guardadas.',
        ]);

        return to_route('admin.notification-settings.edit');
    }
}
