<?php

namespace App\Http\Controllers;

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

class AppointmentController extends Controller
{
    public function store(Request $request)
    {
        // Validate the request data
        $request->validate([
            'first_name' => 'required|string|max:255',
            'last_name' => 'required|string|max:255',
            'email' => 'required|email|max:255',
            'phone' => 'required|string|max:20',
            'department_id' => 'required|exists:departments,id',
            'doctor_id' => 'required|exists:doctors,id',
            'appointment_date' => 'required|date',
            'appointment_time' => 'required',
            'message' => 'nullable|string|max:1000',
        ]);

        $data = $request->all();
        $data['appointment_date'] = date('Y-m-d', strtotime($request->appointment_date));
        $data['appointment_time'] = date('H:i:s', strtotime($request->appointment_time));
        $data['status'] = 'pending';

        // store the appointment in the database or send an email notification
        Appointment::create($data);

        return back()->with('success', 'Your appointment request has been submitted successfully!');
    }
}
