<?php

namespace App\Http\Controllers\website;

use App\Http\Controllers\Controller;
use App\Models\Inquiry;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ContactUsController extends Controller
{
    function index()
    {
        $page = DB::table('contacts')->select('title')->first();
        $pageTitle = $page->title ?? 'Contact us';
        return view('website.contact_us', compact('pageTitle'));
    }

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

        Inquiry::create([
            'name'        => $validated['name'],
            'email'       => $validated['email'],
            'phone'       => $validated['phone'] ?? null,
            'description' => $validated['message'],
        ]);

        if ($request->wantsJson()) {
            return response()->json([
                'success' => true,
                'message' => 'Your inquiry has been submitted successfully!'
            ]);
        }

        return redirect()->back()->with('toast', [
            'type' => 'success',
            'message' => 'Your inquiry has been submitted successfully!'
        ]);
    }
}
