<?php

namespace App\Http\Controllers\Api;

use App\Core\Http\Controllers\BaseApiController;
use App\Models\AiPromptTemplate;
use App\Services\Subscriptions\AiQuotaService;
use App\Services\Subscriptions\PlanCapabilities;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/**
 * AI Studio — قوالب جاهزة.
 */
class AiStudioTemplatesController extends BaseApiController
{
    public function __construct(
        private readonly AiQuotaService $quota,
    ) {}

    public function index(Request $request): JsonResponse
    {
        $user = $request->user();
        $plan = $this->quota->resolveEffectivePlan($user);
        $slug = (string) $plan->slug;
        $isFree = $slug === 'free';

        $rows = AiPromptTemplate::query()->orderBy('sort_order')->orderBy('id')->get();

        $items = $rows->map(static function (AiPromptTemplate $t) use ($isFree) {
            $locked = $t->requires_pro && $isFree;

            return [
                'slug' => $t->slug,
                'title_ar' => $t->title_ar,
                'description_ar' => $t->description_ar,
                'prompt_body' => $locked ? null : $t->prompt_body,
                'category' => $t->category,
                'requires_pro' => $t->requires_pro,
                'locked' => $locked,
            ];
        })->all();

        return $this->successResponse('OK', ['items' => $items]);
    }
}
