<?php

/* Titan © TakfeDev (MKJUEL). All Rights. */

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class AuthSettingsController extends Controller
{
    public function index(): JsonResponse
    {
        $value = Setting::where('group', 'auth')->where('key', 'registration_enabled')->value('value');
        
        return response()->json([
            'registration_enabled' => $value === 'false' ? false : true,
        ]);
    }

    public function update(Request $request): JsonResponse
    {
        $request->validate([
            'registration_enabled' => 'required|boolean',
        ]);

        Setting::updateOrCreate(
            ['group' => 'auth', 'key' => 'registration_enabled'],
            ['value' => $request->registration_enabled ? 'true' : 'false']
        );

        return response()->json(['message' => 'Authentication settings updated.']);
    }
}