<?php

namespace App\Http\Controllers;

use App\Models\Contact;
use Illuminate\Http\Request;

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

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

        $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');
    }

    public function reloadCaptcha(): \Illuminate\Http\JsonResponse
    {
        return response()->json(['captcha'=> captcha_img()]);
    }
}
