<?php

namespace App\Http\Controllers;

use App\Models\Admission;
use App\Models\Level;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class AdmissionController extends Controller
{
    public function index(): View
    {
        $levels = Level::active()->orderBy('order')->get();

        return view('admissions', compact('levels'));
    }

    public function store(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'level_id' => ['nullable', 'exists:levels,id'],
            'guardian_name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'email', 'max:255'],
            'phone' => ['nullable', 'string', 'max:50'],
            'student_name' => ['required', 'string', 'max:255'],
            'birth_date' => ['nullable', 'date'],
            'message' => ['nullable', 'string', 'max:5000'],
        ]);

        $validated['status'] = 'pendiente';

        Admission::create($validated);

        return back()->with('success', 'Solicitud de admision enviada correctamente. Nos pondremos en contacto contigo.');
    }
}
