<?php

namespace App\Http\Controllers;

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

class DivisionController extends Controller
{
    
    public function index()
    {
        $divisions = Division::orderBy('name_en')->get();

        return view('divisions.index', compact('divisions'));
    }

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

    
    public function store(Request $request)
    {
        $validated = $request->validate([
            'name_bn' => ['required', 'string', 'max:100', 'unique:divisions,name_bn'],
            'name_en' => ['required', 'string', 'max:100', 'unique:divisions,name_en'],
        ]);

        Division::create($validated);

        return redirect()
            ->route('divisions.index')
            ->with('success', 'বিভাগ সফলভাবে যোগ করা হয়েছে।');
    }

   
    public function edit(Division $division)
    {
        return view('divisions.edit', compact('division'));
    }

    
    public function update(Request $request, Division $division)
    {
        $validated = $request->validate([
            'name_bn' => ['required', 'string', 'max:100', 'unique:divisions,name_bn,' . $division->id],
            'name_en' => ['required', 'string', 'max:100', 'unique:divisions,name_en,' . $division->id],
        ]);

        $division->update($validated);

        return redirect()
            ->route('divisions.index')
            ->with('success', 'বিভাগ তথ্য আপডেট করা হয়েছে।');
    }

    
    public function destroy(Division $division)
    {
        $division->delete();

        return redirect()
            ->route('divisions.index')
            ->with('success', 'বিভাগ মুছে ফেলা হয়েছে।');
    }
}
