<?php

namespace App\Http\Controllers;

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

class ClassroomController extends Controller
{
    public function index()
    {
        $classrooms = Classroom::orderBy('name')->paginate(20);

        return view('admin.classrooms.index', compact('classrooms'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name'     => 'required|string|max:100',
            'code'     => 'required|string|max:20|unique:classrooms,code',
            'capacity' => 'nullable|integer|min:1',
        ]);

        Classroom::create($validated);

        return back()->with('success', 'Ruang kelas berhasil ditambahkan!');
    }

    public function destroy(int $id)
    {
        Classroom::findOrFail($id)->delete();

        return back()->with('success', 'Ruang kelas berhasil dihapus.');
    }
}
