<?php

namespace App\Http\Controllers;

use App\Models\Faq;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class FaqController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = Faq::query();

        $status = $request->get('status');
        if ($status === null) {
            $query->where('status', true);
        } elseif (in_array($status, ['active', 'inactive'], true)) {
            $query->where('status', $status === 'active');
        }

        if ($request->filled('search')) {
            $search = $request->search;
            $query->where(function ($q) use ($search) {
                $q->where('question', 'like', "%{$search}%")
                    ->orWhere('question_km', 'like', "%{$search}%")
                    ->orWhere('answer', 'like', "%{$search}%")
                    ->orWhere('answer_km', 'like', "%{$search}%");
            });
        }

        $sortBy = $request->get('sort_by', 'order');
        $sortOrder = $request->get('sort_order', 'asc');
        if (! in_array($sortBy, ['order', 'created_at', 'updated_at'], true)) {
            $sortBy = 'order';
        }
        if (! in_array(strtolower((string) $sortOrder), ['asc', 'desc'], true)) {
            $sortOrder = 'asc';
        }

        $query->orderBy($sortBy, $sortOrder);
        if ($sortBy !== 'created_at') {
            $query->orderByDesc('created_at');
        }

        $perPage = (int) $request->get('per_page', 15);
        $perPage = min(100, max(1, $perPage));

        $faqs = $query->paginate($perPage);

        $lang = $request->get('lang', 'en');
        $transformedFaqs = $faqs->getCollection()->map(function (Faq $faq) use ($lang) {
            $questionText = $lang === 'km' ? ($faq->question_km ?: $faq->question) : $faq->question;
            $answerText = $lang === 'km' ? ($faq->answer_km ?: $faq->answer) : $faq->answer;

            return [
                'id' => $faq->id,
                'question' => $faq->question,
                'question_km' => $faq->question_km,
                'answer' => $faq->answer,
                'answer_km' => $faq->answer_km,
                'question_text' => $questionText,
                'answer_text' => $answerText,
                'order' => $faq->order,
                'status' => $faq->status,
                'created_at' => $faq->created_at,
                'updated_at' => $faq->updated_at,
            ];
        });

        return response()->json([
            'success' => true,
            'data' => $transformedFaqs,
            'pagination' => [
                'current_page' => $faqs->currentPage(),
                'last_page' => $faqs->lastPage(),
                'per_page' => $faqs->perPage(),
                'total' => $faqs->total(),
            ],
        ]);
    }

    public function show(Request $request, Faq $faq): JsonResponse
    {
        if (! $faq->status) {
            return response()->json([
                'success' => false,
                'message' => 'FAQ not found',
            ], 404);
        }

        $lang = $request->get('lang', 'en');
        $questionText = $lang === 'km' ? ($faq->question_km ?: $faq->question) : $faq->question;
        $answerText = $lang === 'km' ? ($faq->answer_km ?: $faq->answer) : $faq->answer;

        return response()->json([
            'success' => true,
            'data' => [
                'id' => $faq->id,
                'question' => $faq->question,
                'question_km' => $faq->question_km,
                'answer' => $faq->answer,
                'answer_km' => $faq->answer_km,
                'question_text' => $questionText,
                'answer_text' => $answerText,
                'order' => $faq->order,
                'status' => $faq->status,
                'created_at' => $faq->created_at,
                'updated_at' => $faq->updated_at,
            ],
        ]);
    }
}
