<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Services\TodayIssueService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class TodayIssueController extends Controller
{
    public function __construct(
        private readonly TodayIssueService $todayIssueService
    ) {}

    /**
     * 오늘의 이슈 리스트 조회
     */
    public function summary(Request $request): JsonResponse
    {
        $limit = (int) $request->input('limit', 30);
        $date = $request->input('date'); // YYYY-MM-DD (이전 이슈 조회용)

        return ApiResponse::handle(function () use ($limit, $date) {
            return $this->todayIssueService->summary($limit, null, $date);
        }, __('message.fetched'));
    }

    /**
     * 읽지 않은 이슈 목록 조회 (헤더 알림용)
     */
    public function unread(Request $request): JsonResponse
    {
        $limit = (int) $request->input('limit', 10);

        return ApiResponse::handle(function () use ($limit) {
            return $this->todayIssueService->getUnreadList($limit);
        }, __('message.fetched'));
    }

    /**
     * 읽지 않은 이슈 개수 조회 (헤더 알림 뱃지용)
     */
    public function unreadCount(): JsonResponse
    {
        return ApiResponse::handle(function () {
            return ['count' => $this->todayIssueService->getUnreadCount()];
        }, __('message.fetched'));
    }

    /**
     * 이슈 읽음 처리
     */
    public function markAsRead(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            $result = $this->todayIssueService->markAsRead($id);
            if (! $result) {
                throw new \App\Exceptions\ApiException(__('error.not_found'), 404);
            }

            return null;
        }, __('message.today_issue.marked_as_read'));
    }

    /**
     * 모든 이슈 읽음 처리
     */
    public function markAllAsRead(): JsonResponse
    {
        return ApiResponse::handle(function () {
            $count = $this->todayIssueService->markAllAsRead();

            return ['count' => $count];
        }, __('message.today_issue.all_marked_as_read'));
    }
}
