<?php

namespace App\Http\Controllers\SuperAdmin;

use App\Http\Controllers\Controller;
use App\Http\Requests\UpdatePlatformSettingsRequest;
use App\Services\SuperAdmin\PlatformAdminService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class PlatformSettingsController extends Controller
{
    public function system(PlatformAdminService $platform): View
    {
        return view('super-admin.settings.system', [
            'settings' => $platform->settings('system'),
            'formAction' => route('super-admin.settings.update', 'system'),
        ]);
    }

    public function landing(PlatformAdminService $platform): View
    {
        return view('super-admin.settings.landing', [
            'settings' => $platform->settings('landing'),
            'formAction' => route('super-admin.settings.update', 'landing'),
        ]);
    }

    public function email(PlatformAdminService $platform): View
    {
        return view('super-admin.settings.email', [
            'settings' => $platform->settings('email'),
            'formAction' => route('super-admin.settings.update', 'email'),
        ]);
    }

    public function whatsapp(PlatformAdminService $platform): View
    {
        return view('super-admin.settings.whatsapp', [
            'settings' => $platform->settings('whatsapp'),
            'formAction' => route('super-admin.settings.update', 'whatsapp'),
        ]);
    }

    public function update(UpdatePlatformSettingsRequest $request, string $group, PlatformAdminService $platform): RedirectResponse
    {
        abort_unless(in_array($group, ['system', 'landing', 'email', 'whatsapp'], true), 404);

        $platform->saveSettings($group, $request->payload(), $request->user(), $request);

        return redirect()->back()->with('status', 'Platform settings saved successfully.');
    }
}
