<?php

namespace App\Http\Controllers;

use App\Models\Contact;
use App\Rules\ValidCaptcha;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class ContactController extends Controller
{
    /**
     * Display Contact Page.
     */
    public function index(): View
    {
        return view('contact.form');
    }

    /**
     * Store Contact Submission.
     */
    public function store(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'name' => 'required|max:30',
            'email' => 'required|max:50',
            'message' => 'required|max:1000',
            'subject' => 'required|max:50',
            'altcha' => ['required', new ValidCaptcha],
        ]);

        $contact = new Contact;
        $contact->name = $request->input('name');
        $contact->email = $request->input('email');
        $contact->message = $request->input('message');
        $contact->subject = $request->input('subject');
        $contact->save();

        return back()->with('status', 'contact-submitted');
    }
}
