<?php

namespace App\Http\Controllers;

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

class DeductionTypeController extends Controller
{
    public function index()
    {
        $deductionTypes = DeductionType::all();
        return view('admin.deduction_types.index', compact('deductionTypes'));
    }

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

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
        ]);

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

        return redirect()->route('deduction-types.index')->with('success', 'Deduction type created successfully.');
    }

    public function edit(DeductionType $deductionType)
    {
        return view('admin.deduction_types.edit', compact('deductionType'));
    }

    public function update(Request $request, DeductionType $deductionType)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
        ]);

        $deductionType->update($request->all());

        return redirect()->route('deduction-types.index')->with('success', 'Deduction type updated successfully.');
    }

    public function destroy(DeductionType $deductionType)
    {
        $deductionType->delete();
        return redirect()->route('deduction-types.index')->with('success', 'Deduction type deleted successfully.');
    }
}
