<?php

namespace App\Http\Controllers;

use App\Models\Section;
use App\Models\SchoolClass;
use Illuminate\Http\Request;

class SectionController extends Controller
{
    public function index()
    {
        // সম্পর্কের (Relationship) মাধ্যমে ডাটা আনা
        $sections = Section::with('schoolClass')->orderBy('created_at', 'asc')->get();
        $classes = SchoolClass::all();
        
        return view('admin.sections', compact('sections', 'classes'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'section_name' => 'required',
            'class_id' => 'required|exists:classes,id'
        ]);

        Section::create($request->all());

        return response()->json(['status' => 'success', 'message' => 'সেকশন তৈরি হয়েছে!']);
    }

    public function update(Request $request, $id)
    {
        $section = Section::findOrFail($id);
        $section->update($request->all());

        return response()->json(['status' => 'success', 'message' => 'সেকশন আপডেট হয়েছে!']);
    }

    public function destroy($id)
    {
        Section::destroy($id);
        return response()->json(['status' => 'success', 'message' => 'সেকশন মুছে ফেলা হয়েছে!']);
    }
}