<?php

namespace App\Http\Controllers\Admin\Settings;

use Illuminate\Http\Request;
use Inertia\Inertia;

class SecuritySettingsController extends BaseSettingsController
{
    public function index()
    {
        $allSettings = $this->getAllSettings();

        return Inertia::render('admin/settings/security/index', [
            'pinFieldEnabled' => $this->getBoolSetting($allSettings, 'pin_field_enabled', false),
            'passwordOtpEnabled' => $this->getBoolSetting($allSettings, 'password_otp_enabled', true),
            'turnstileEnabled' => $this->getBoolSetting($allSettings, 'turnstile_enabled', false),
            'turnstileSiteKey' => $this->setting('turnstile_site_key', ''),
            'activationRequired' => $this->getBoolSetting($allSettings, 'activation_required', false),
            'googleLoginEnabled' => $this->getBoolSetting($allSettings, 'google_login_enabled', false),
            'googleClientId' => $this->getGoogleCredential('google_client_id', 'GOOGLE_CLIENT_ID'),
            'googleClientSecret' => $this->getGoogleCredential('google_client_secret', 'GOOGLE_CLIENT_SECRET'),
            'googleRedirectUri' => $this->getGoogleCredential('google_redirect_uri', 'GOOGLE_REDIRECT_URI'),
        ]);
    }

    public function update(Request $request)
    {
        $request->validate([
            'pin_field_enabled' => 'required|boolean',
            'password_otp_enabled' => 'required|boolean',
            'turnstile_enabled' => 'required|boolean',
            'turnstile_site_key' => 'nullable|string|max:255',
            'activation_required' => 'required|boolean',
            'google_login_enabled' => 'required|boolean',
            'google_client_id' => 'nullable|string|max:255',
            'google_client_secret' => 'nullable|string|max:255',
            'google_redirect_uri' => 'nullable|url|max:255',
        ]);

        $this->setBoolSetting('pin_field_enabled', $request->boolean('pin_field_enabled'));
        $this->setBoolSetting('password_otp_enabled', $request->boolean('password_otp_enabled'));
        $this->setBoolSetting('turnstile_enabled', $request->boolean('turnstile_enabled'));
        $this->setSetting('turnstile_site_key', $request->input('turnstile_site_key', ''));
        $this->setBoolSetting('activation_required', $request->boolean('activation_required'));
        $this->setBoolSetting('google_login_enabled', $request->boolean('google_login_enabled'));
        $this->setSetting('google_client_id', $request->input('google_client_id', ''));
        $this->setSetting('google_client_secret', $request->input('google_client_secret', ''));
        $this->setSetting('google_redirect_uri', $request->input('google_redirect_uri', ''));

        $this->refreshSettingsCache();

        return back()->with('success', 'Security settings updated successfully.');
    }

    private function getGoogleCredential(string $settingKey, string $envKey): string
    {
        return $this->setting($settingKey)
            ?: config('services.google.'.strtolower(str_replace('GOOGLE_', '', $envKey)), '')
            ?: env($envKey, '');
    }
}
