<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use App\Models\EmailTemplate;
use App\Models\SystemSetting;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Illuminate\View\View;

class SettingController extends Controller
{
    public function index(): View
    {
        return view('app.settings.index', [
            'settings' => SystemSetting::orderBy('group')->orderBy('key')->get()->groupBy('group'),
            'emailTemplates' => EmailTemplate::orderBy('name')->get(),
        ]);
    }

    public function sendTestEmail(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'email' => ['required', 'email'],
        ]);

        Mail::raw('This is a Mosaic Technology Consulting deployment test email.', function ($message) use ($validated): void {
            $message
                ->to($validated['email'])
                ->subject('Mosaic deployment test email');
        });

        return back()->with('success', 'Test email sent.');
    }
}
