<?php

namespace App\Http\Controllers;

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

class CategoryController extends Controller
{
    public function index()
    {
        $categories = Category::all();

        return view('categories.list', compact('categories'));
    }

    public function create()
    {
        return view('categories.add');
    }
    public function edit($id)
    {
    $category = Category::findOrFail($id);

    return view('categories.edit', compact('category'));
}
public function update(Request $request, $id)
{
    $category = Category::findOrFail($id);

    $validated = $request->validate([
        'category_name' => 'required|string|max:255',
        'description' => 'nullable|string',
    ]);

    $category->update($validated);

    return redirect('/categories/list')
        ->with('success', 'Category updated successfully.');
}
public function destroy($id)
{
    $category = Category::findOrFail($id);

    $category->delete();

    return redirect('/categories/list')
        ->with('success', 'Category deleted successfully.');
}
public function show($id)
{
    $category = Category::findOrFail($id);

    return view('categories.show', compact('category'));
}
    public function store(Request $request)
    {
        $validated = $request->validate([
            'category_name' => 'required|string|max:255',
            'description' => 'nullable|string',
        ]);

        Category::create($validated);

        return redirect('/categories/list')
            ->with('success', 'Category added successfully.');
    }
}