<?php

namespace App\Http\Controllers;

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

class AdminRegistrationController extends Controller
{
    /**
     * Display a listing of the registrations.
     */
    public function index()
    {
        $registrations = Registration::all();
        return view('admin.registrations.index', compact('registrations'));
    }

    /**
     * Show the form for creating a new registration.
     */
    public function create()
    {
        return view('admin.registrations.create');
    }

    /**
     * Store a newly created registration in storage.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'registration_number' => 'required|unique:registrations',
            'first_name' => 'required|string|max:255',
            'last_name' => 'required|string|max:255',
            'national_id' => 'required|string|max:20',
            'dob' => 'required|date',
            'programme_code' => 'required|string|max:20',
        ]);

        Registration::create($validated);

        return redirect()->route('admin.registrations.index')->with('success', 'Registration created successfully.');
    }

    /**
     * Show the form for editing the specified registration.
     */
    public function edit($id)
    {
        $registration = Registration::findOrFail($id);
        return view('admin.registrations.edit', compact('registration'));
    }

    /**
     * Update the specified registration in storage.
     */
    public function update(Request $request, $id)
    {
        $registration = Registration::findOrFail($id);

        $validated = $request->validate([
            'registration_number' => 'required|unique:registrations,registration_number,' . $id,
            'first_name' => 'required|string|max:255',
            'last_name' => 'required|string|max:255',
            'national_id' => 'required|string|max:20',
            'dob' => 'required|date',
            'programme_code' => 'required|string|max:20',
        ]);

        $registration->update($validated);

        return redirect()->route('admin.registrations.index')->with('success', 'Registration updated successfully.');
    }

    /**
     * Remove the specified registration from storage.
     */
    public function destroy($id)
    {
        $registration = Registration::findOrFail($id);
        $registration->delete();

        return redirect()->route('admin.registrations.index')->with('success', 'Registration deleted successfully.');
    }

    /**
     * Display the specified registration (optional).
     */
    public function show($id)
    {
        $registration = Registration::findOrFail($id);
        return view('admin.registrations.show', compact('registration'));
    }
}
