from typing import Annotated
from uuid import UUID
from fastapi import APIRouter, Depends, Query, status
from sqlalchemy.ext.asyncio import AsyncSession
from apps.api.dependencies import get_current_tenant
from shared.schemas import ApiResponse
from infrastructure.database.session import get_db
from modules.notifications.schemas import (
    CreateNotificationRequest,
    MarkReadRequest,
    NotificationResponse,
)
from modules.notifications.service import NotificationService
from modules.tenants.models import Tenant, TenantMember

router = APIRouter(prefix="/notifications", tags=["In-App Notifications"])


@router.get("", response_model=ApiResponse[list[NotificationResponse]], status_code=status.HTTP_200_OK)
async def list_notifications(
    unread_only: bool = Query(default=False),
    tenant_context: Annotated[tuple[Tenant, TenantMember | None], Depends(get_current_tenant)] = None,
    db: Annotated[AsyncSession, Depends(get_db)] = None,
):
    """Lists notifications for the tenant."""
    tenant, _ = tenant_context
    service = NotificationService(db)
    notifs = await service.list_notifications(tenant.id, unread_only=unread_only)
    return ApiResponse(
        message="Notifications retrieved",
        data=notifs,
    )


@router.post("", response_model=ApiResponse[NotificationResponse], status_code=status.HTTP_201_CREATED)
async def create_notification(
    req: CreateNotificationRequest,
    tenant_context: Annotated[tuple[Tenant, TenantMember | None], Depends(get_current_tenant)] = None,
    db: Annotated[AsyncSession, Depends(get_db)] = None,
):
    """Dispatches an in-app notification alert."""
    tenant, _ = tenant_context
    service = NotificationService(db)
    notif = await service.create_notification(tenant.id, req)
    return ApiResponse(
        message="Notification created",
        data=notif,
    )


@router.post("/mark-read", response_model=ApiResponse[dict[str, int]], status_code=status.HTTP_200_OK)
async def mark_notifications_read(
    req: MarkReadRequest,
    tenant_context: Annotated[tuple[Tenant, TenantMember | None], Depends(get_current_tenant)] = None,
    db: Annotated[AsyncSession, Depends(get_db)] = None,
):
    """Marks specified notifications as read."""
    tenant, _ = tenant_context
    service = NotificationService(db)
    count = await service.mark_notifications_read(tenant.id, req.notification_ids)
    return ApiResponse(
        message=f"{count} notifications marked as read",
        data={"updated_count": count},
    )


@router.get("/unread-count", response_model=ApiResponse[dict[str, int]], status_code=status.HTTP_200_OK)
async def get_unread_count(
    tenant_context: Annotated[tuple[Tenant, TenantMember | None], Depends(get_current_tenant)] = None,
    db: Annotated[AsyncSession, Depends(get_db)] = None,
):
    """Returns number of unread notifications for badge counters."""
    tenant, _ = tenant_context
    service = NotificationService(db)
    unread = await service.get_unread_count(tenant.id)
    return ApiResponse(
        message="Unread notification count retrieved",
        data={"unread_count": unread},
    )
