<?php

namespace App\Http\Controllers;

use App\Mail\ContactFormMail;
use Illuminate\Http\Request;
use App\Models\Contact;
use Illuminate\Support\Facades\Mail;

class ContactController extends Controller
{
    public function index()
    {
        return view('pages.contact');
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:100',
            'email' => 'required|email|max:100',
            'phone' => 'required|string|max:20',
            'inquiry_type' => 'nullable|string|max:100',
            'message' => 'required|string|max:1000',
            'project' => 'nullable|string|max:255',
            'subject' => 'required|string|max:255',
            'project_type' => 'nullable|string|max:255',

        ]);

        try {
            $data = Contact::create($validated);
            $admin_email = config('mail.admin_receiver_email');
            Mail::to($admin_email)->send(new ContactFormMail($data));
            return response()->json([
                'status' => true,
                'message' => 'Your message has been sent successfully!'
            ]);
        } catch (\Throwable $th) {
            return response()->json([
                'status' => false,
                'message' => "Internal Server Error!"
            ], 500);
        }
    }
}
