<?php

namespace App\Http\Controllers;

use App\Models\ContactMessage;
use App\Models\SiteSetting;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class ContactController extends Controller
{
    public function show(): View
    {
        $s = SiteSetting::allKeyed();

        return view('contact', [
            'heroKicker' => $s['contact_hero_kicker'] ?? null,
            'heroTitle' => $s['contact_hero_title'] ?? null,
            'heroText' => $s['contact_hero_text'] ?? null,
            'whatsappUrl' => $s['contact_whatsapp_url'] ?? null,
            'emailDisplay' => $s['contact_email_display'] ?? null,
            'calendlyUrl' => $s['contact_calendly_url'] ?? null,
        ]);
    }

    public function store(Request $request): RedirectResponse
    {
        $data = $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'email', 'max:255'],
            'besoin' => ['required', 'in:sourcing,logistique,vente,cbt'],
            'message' => ['required', 'string', 'max:10000'],
        ]);

        ContactMessage::query()->create([
            ...$data,
            'ip_address' => $request->ip(),
        ]);

        return redirect()->route('contact')->with('contact_ok', true);
    }
}
