<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\News;
use Illuminate\Http\JsonResponse;
use OpenApi\Attributes as OA;

class LauncherNewsController extends Controller
{
    #[OA\Get(
        path: '/api/launcher/news',
        summary: 'Список новостей лаунчера',
        description: 'Возвращает опубликованные новости для отображения в лаунчере. Отсортированы по sort_order и дате создания.',
        tags: ['Launcher'],
        security: [['bearerAuth' => []]],
        responses: [
            new OA\Response(
                response: 200,
                description: 'Список новостей',
                content: new OA\JsonContent(
                    properties: [
                        new OA\Property(
                            property: 'data',
                            type: 'array',
                            items: new OA\Items(
                                properties: [
                                    new OA\Property(property: 'id', type: 'integer', example: 1),
                                    new OA\Property(property: 'title', type: 'string', example: 'Обновление 1.2'),
                                    new OA\Property(property: 'body', type: 'string', example: 'Описание обновления...'),
                                    new OA\Property(property: 'image_url', type: 'string', format: 'uri', nullable: true, example: 'https://storage.yandexcloud.net/warcraft-client/news/update-1.2.jpg'),
                                    new OA\Property(property: 'created_at', type: 'string', format: 'date-time'),
                                ],
                            ),
                        ),
                    ],
                ),
            ),
            new OA\Response(
                response: 401,
                description: 'Не авторизован',
                content: new OA\JsonContent(
                    properties: [
                        new OA\Property(property: 'message', type: 'string', example: 'Unauthenticated.'),
                    ],
                ),
            ),
        ],
    )]
    public function index(): JsonResponse
    {
        $news = News::published()
            ->ordered()
            ->get()
            ->map(fn (News $item) => [
                'id' => $item->id,
                'title' => $item->title,
                'body' => $item->body,
                'image_url' => $item->image_url,
                'created_at' => $item->created_at->toIso8601String(),
            ]);

        return response()->json(['data' => $news]);
    }
}
