[]]], tags: ['Templates'], parameters: [ new OA\Parameter(name: 'slug', description: 'Template slug', in: 'path', required: true, schema: new OA\Schema(type: 'string')), ], responses: [ new OA\Response( response: 202, description: 'Generation started', content: new OA\JsonContent( properties: [ new OA\Property(property: 'message', type: 'string', example: 'درخواست تولید پاورپوینت ثبت شد.'), new OA\Property( property: 'data', properties: [ new OA\Property(property: 'generation', ref: '#/components/schemas/TemplateGeneration'), ], type: 'object' ), ] ) ), new OA\Response(response: 401, description: 'Unauthenticated'), new OA\Response(response: 422, description: 'Validation error'), ] )] public function store(GenerateTemplateRequest $request, Template $template): JsonResponse { $uuid = (string) Str::uuid(); $values = $request->validated('values', []); $storedValues = []; foreach ($template->keys as $key) { if (! array_key_exists($key->key, $values)) { continue; } if ($key->type === TemplateKeyType::Image) { $storedValues[$key->key] = $request->file('values.'.$key->key) ->store('templates/generations/inputs/'.$uuid, 'local'); continue; } $storedValues[$key->key] = $values[$key->key]; } $generation = TemplateGeneration::query()->create([ 'template_id' => $template->id, 'uuid' => $uuid, 'title' => $this->buildGenerationTitle($template, $request), 'status' => TemplateGenerationStatus::Pending, 'values' => $storedValues, 'requested_by' => $request->user()->id, ]); GenerateTemplateJob::dispatch($generation); return $this->success('درخواست تولید پاورپوینت ثبت شد.', [ 'generation' => new TemplateGenerationResource($generation), ], 202); } #[OA\Post( path: '/templates/{slug}/generate-from-text', description: 'Start generating a presentation from free-form text using AI', summary: 'Generate presentation from text (AI)', security: [['sanctum' => []]], tags: ['Templates'], parameters: [ new OA\Parameter(name: 'slug', description: 'Template slug', in: 'path', required: true, schema: new OA\Schema(type: 'string')), ], requestBody: new OA\RequestBody( required: true, content: new OA\JsonContent( required: ['title', 'language', 'slide_count', 'text'], properties: [ new OA\Property(property: 'title', type: 'string', example: 'گزارش فروش سال ۱۴۰۴'), new OA\Property(property: 'language', type: 'string', example: 'fa'), new OA\Property(property: 'slide_count', type: 'integer', example: 10), new OA\Property(property: 'text', type: 'string', example: 'متن، عناوین، بولت‌ها و قوانین موردنظر...'), ] ) ), responses: [ new OA\Response( response: 202, description: 'Generation started', content: new OA\JsonContent( properties: [ new OA\Property(property: 'message', type: 'string', example: 'درخواست تولید پاورپوینت ثبت شد.'), new OA\Property( property: 'data', properties: [ new OA\Property(property: 'generation', ref: '#/components/schemas/TemplateGeneration'), ], type: 'object' ), ] ) ), new OA\Response(response: 401, description: 'Unauthenticated'), new OA\Response(response: 422, description: 'Validation error'), ] )] public function storeFromText(GenerateAiPresentationRequest $request, Template $template): JsonResponse { $generation = TemplateGeneration::query()->create([ 'template_id' => $template->id, 'uuid' => (string) Str::uuid(), 'source' => TemplateGenerationSource::AiText, 'title' => $request->validated('title'), 'language' => $request->validated('language'), 'slide_count' => $request->validated('slide_count'), 'source_text' => $request->validated('text'), 'status' => TemplateGenerationStatus::Pending, 'requested_by' => $request->user()->id, ]); GenerateTemplateJob::dispatch($generation); return $this->success('درخواست تولید پاورپوینت ثبت شد.', [ 'generation' => new TemplateGenerationResource($generation), ], 202); } #[OA\Get( path: '/generations', description: 'List the authenticated user presentation generations (both template-based and AI-generated)', summary: 'List my presentations', security: [['sanctum' => []]], tags: ['Templates'], parameters: [ new OA\Parameter(name: 'per_page', description: 'Items per page', in: 'query', schema: new OA\Schema(type: 'integer', example: 15)), ], responses: [ new OA\Response( response: 200, description: 'Generations fetched successfully', content: new OA\JsonContent( properties: [ new OA\Property(property: 'message', type: 'string', example: 'پاورپوینت‌های شما با موفقیت دریافت شد.'), new OA\Property( property: 'data', properties: [ new OA\Property( property: 'generations', type: 'array', items: new OA\Items(ref: '#/components/schemas/TemplateGeneration') ), ], type: 'object' ), ] ) ), new OA\Response(response: 401, description: 'Unauthenticated'), ] )] public function index(Request $request): JsonResponse { $perPage = min(max((int) $request->integer('per_page', 15), 1), 100); $generations = TemplateGeneration::query() ->with('template') ->where('requested_by', $request->user()->id) ->latest() ->paginate($perPage); return $this->success('پاورپوینت‌های شما با موفقیت دریافت شد.', [ 'generations' => TemplateGenerationResource::collection($generations)->response()->getData(true), ]); } #[OA\Get( path: '/generations/{uuid}', description: 'Get the status of a presentation generation request', summary: 'Show generation status', security: [['sanctum' => []]], tags: ['Templates'], parameters: [ new OA\Parameter(name: 'uuid', description: 'Generation uuid', in: 'path', required: true, schema: new OA\Schema(type: 'string')), ], responses: [ new OA\Response( response: 200, description: 'Generation status fetched successfully', content: new OA\JsonContent( properties: [ new OA\Property(property: 'message', type: 'string', example: 'وضعیت تولید با موفقیت دریافت شد.'), new OA\Property( property: 'data', properties: [ new OA\Property(property: 'generation', ref: '#/components/schemas/TemplateGeneration'), ], type: 'object' ), ] ) ), new OA\Response(response: 401, description: 'Unauthenticated'), new OA\Response(response: 403, description: 'Forbidden'), new OA\Response(response: 404, description: 'Not found'), ] )] public function show(TemplateGeneration $generation): JsonResponse { abort_if($generation->requested_by !== request()->user()->id, 403); return $this->success('وضعیت تولید با موفقیت دریافت شد.', [ 'generation' => new TemplateGenerationResource($generation), ]); } #[OA\Get( path: '/generations/{uuid}/download', description: 'Download the generated presentation file', summary: 'Download generated presentation', security: [['sanctum' => []]], tags: ['Templates'], parameters: [ new OA\Parameter(name: 'uuid', description: 'Generation uuid', in: 'path', required: true, schema: new OA\Schema(type: 'string')), ], responses: [ new OA\Response(response: 200, description: 'File download'), new OA\Response(response: 401, description: 'Unauthenticated'), new OA\Response(response: 403, description: 'Forbidden'), new OA\Response(response: 404, description: 'Not found or not ready'), ] )] public function download(TemplateGeneration $generation): StreamedResponse { abort_if($generation->requested_by !== request()->user()->id, Response::HTTP_FORBIDDEN); abort_if($generation->status !== TemplateGenerationStatus::Completed || ! $generation->output_path, Response::HTTP_NOT_FOUND); return Storage::disk('local')->download($generation->output_path, $generation->downloadFilename().'.pptx'); } private function buildGenerationTitle(Template $template, Request $request): string { $label = $request->user()?->name ?: 'کاربر'; return sprintf('%s - %s - %s', $template->title, $label, Jalali::date(now())); } }