<?php

namespace App\Http\Controllers\Api;

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

class HadithController extends Controller
{
    /**
     * Get all hadiths
     */
    public function index(Request $request)
    {
        $query = Hadees::with('category');
        
        // Filter by category if provided
        if ($request->has('category_id')) {
            $query->where('hadith_category_id', $request->category_id);
        }
        
        $hadiths = $query->orderBy('created_at', 'desc')->get()->map(function ($hadith) {
            return [
                'id' => $hadith->id,
                'category' => [
                    'id' => $hadith->category->id,
                    'name' => $hadith->category->name,
                ],
                'topic' => $hadith->topic,
                'topic_arabic' => $hadith->topic_arabic,
                'narrator' => $hadith->narrator,
                'arabic_text' => $hadith->arabic_text,
                'english_translation' => $hadith->english_translation,
                'source' => $hadith->source,
                'grade' => $hadith->grade,
                'created_at' => $hadith->created_at,
            ];
        });

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

    /**
     * Get hadiths by category
     */
    public function byCategory($categoryId)
    {
        $hadiths = Hadees::with('category')
            ->where('hadith_category_id', $categoryId)
            ->orderBy('created_at', 'desc')
            ->get()
            ->map(function ($hadith) {
                return [
                    'id' => $hadith->id,
                    'category' => [
                        'id' => $hadith->category->id,
                        'name' => $hadith->category->name,
                    ],
                    'topic' => $hadith->topic,
                    'topic_arabic' => $hadith->topic_arabic,
                    'narrator' => $hadith->narrator,
                    'arabic_text' => $hadith->arabic_text,
                    'english_translation' => $hadith->english_translation,
                    'source' => $hadith->source,
                    'grade' => $hadith->grade,
                    'created_at' => $hadith->created_at,
                ];
            });

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

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

        return response()->json([
            'success' => true,
            'data' => [
                'id' => $hadith->id,
                'category' => [
                    'id' => $hadith->category->id,
                    'name' => $hadith->category->name,
                ],
                'topic' => $hadith->topic,
                'topic_arabic' => $hadith->topic_arabic,
                'narrator' => $hadith->narrator,
                'arabic_text' => $hadith->arabic_text,
                'english_translation' => $hadith->english_translation,
                'source' => $hadith->source,
                'grade' => $hadith->grade,
                'created_at' => $hadith->created_at,
            ],
        ]);
    }
}
