<?php

namespace App\Http\Controllers;

use App\Models\Project;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class ContactController extends Controller
{
    public function submit(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'phone' => ['required', 'regex:/^\+?[0-9]{8,15}$/'],
            'email' => ['required', 'email', 'max:255'],
            'service' => ['required', 'string', 'max:255'],
            'details' => ['required', 'string', 'max:3000'],
        ], [
            'phone.regex' => 'Please enter a valid phone number (8-15 digits, optional leading +).',
        ]);

        $submittedAt = now();

        Project::query()->create([
            'name' => $validated['name'],
            'phone' => $validated['phone'],
            'email' => $validated['email'],
            'service' => $validated['service'],
            'details' => $validated['details'],
            'submitted_from' => 'contact_form',
            'submitted_at' => $submittedAt,
        ]);

        return back()->with('status', 'Your project request was sent successfully at '.$submittedAt->format('Y-m-d H:i:s').'.');
    }
}