<?php

namespace Kylin\Notification\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Kylin\Core\ApiResponse;
use Kylin\Core\Controllers\BaseController;
use Kylin\Notification\Services\NotificationService;

class NotificationController extends BaseController
{
    public function __construct(private readonly NotificationService $service) {}

    public function index(Request $request): JsonResponse
    {
        $user = $request->user();
        $notifications = $this->service->listForUser(get_class($user), $user->id, $request->input('per_page', 15));
        return ApiResponse::paginate($notifications);
    }

    public function unreadCount(Request $request): JsonResponse
    {
        $user = $request->user();
        return ApiResponse::success(['count' => $this->service->unreadCount(get_class($user), $user->id)]);
    }

    public function markAsRead(int $id): JsonResponse
    {
        $this->service->markAsRead($id);
        return ApiResponse::success(null, '已标记为已读');
    }

    public function markAllAsRead(Request $request): JsonResponse
    {
        $user = $request->user();
        $count = $this->service->markAllAsRead(get_class($user), $user->id);
        return ApiResponse::success(['count' => $count], "已标记 {$count} 条为已读");
    }
}
