<?php
namespace App\Http\Controllers;

use App\Models\Matiere;
use Illuminate\Http\Request;
use App\Models\Annee;

class MatiereController extends Controller
{
    public function index()
    {
        $matieres = Matiere::all();
        $annees = Annee::all();

        return view('matieres.index', compact('matieres','annees'));
    }

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

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'coefficient' => 'required',
            'year' => 'required',

        ]);

        Matiere::create($validated);

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

    public function show(Matiere $matiere)
    {
        return view('matieres.show', compact('matiere'));
    }

    public function edit(Matiere $matiere)
    {
        return view('matieres.edit', compact('matiere'));
    }

    public function update(Request $request, Matiere $matiere)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'coefficient' => 'required|integer',
            'year' => 'required',
        ]);

        $matiere->update($validated);

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

    public function destroy(Matiere $matiere)
    {
        $matiere->delete();

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