<?php

namespace App\Http\Controllers;

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

class DuaCategoryController extends Controller
{
    // Show all categories
    public function index()
    {
        $categories = DuaCategory::all();
        return view('dua_categories.index', compact('categories'));
    }

    // Show the form for creating a new category
    public function create()
    {
        return view('dua_categories.create');
    }

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

        DuaCategory::create($request->only('name', 'description'));

        return redirect()->route('dua-categories.index')->with('success', 'Category created successfully!');
    }

    // Show the form for editing a category
    public function edit(DuaCategory $duaCategory)
    {
        return view('dua_categories.edit', compact('duaCategory'));
    }

    // Update a category
    public function update(Request $request, DuaCategory $duaCategory)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
        ]);

        $duaCategory->update($request->only('name', 'description'));

        return redirect()->route('dua-categories.index')->with('success', 'Category updated successfully!');
    }

    // Delete a category
    public function destroy(DuaCategory $duaCategory)
    {
        $duaCategory->delete();
        return redirect()->route('dua-categories.index')->with('success', 'Category deleted successfully!');
    }
}