<?php
namespace App\Http\Controllers;

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

class ParentController extends Controller
{
    public function index()
    {
        $parents = Parnet::all();
        return view('parents.index', compact('parents'));
    }

    public function create()
    {
        return view('parents.create');
    }

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

        Parnet::create($validated);

        return redirect()->route('parents.index')->with('success', 'Parent created successfully.');
    }

    public function show(Parent $parent)
    {
        return view('parents.show', compact('parent'));
    }

    public function edit(Parent $parent)
    {
        return view('parents.edit', compact('parent'));
    }

    public function update(Request $request, Parent $parent)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:parents,email,' . $parent->id,
            'phone' => 'nullable|string|max:15',
        ]);

        $parent->update($validated);

        return redirect()->route('parents.index')->with('success', 'Parent updated successfully.');
    }

    public function destroy(Parent $parent)
    {
        $parent->delete();

        return redirect()->route('parents.index')->with('success', 'Parent deleted successfully.');
    }
}
