<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\HadithCategory;
use Illuminate\Http\Request;

class HadithCategoryController extends Controller
{
    /**
     * Get all hadith categories
     */
    public function index()
    {
        $categories = HadithCategory::withCount('hadiths')->orderBy('name')->get()->map(function ($category) {
            return [
                'id' => $category->id,
                'name' => $category->name,
                'description' => $category->description,
                'hadiths_count' => $category->hadiths_count,
                'created_at' => $category->created_at,
            ];
        });

        return response()->json([
            'success' => true,
            'data' => $categories,
        ]);
    }

    /**
     * Get a specific hadith category
     */
    public function show($id)
    {
        $category = HadithCategory::withCount('hadiths')->find($id);
        
        if (!$category) {
            return response()->json([
                'success' => false,
                'message' => 'Hadith category not found',
            ], 404);
        }

        return response()->json([
            'success' => true,
            'data' => [
                'id' => $category->id,
                'name' => $category->name,
                'description' => $category->description,
                'hadiths_count' => $category->hadiths_count,
                'created_at' => $category->created_at,
            ],
        ]);
    }
}
