"""Connector APIs — Phase 12.7."""
from __future__ import annotations

from uuid import UUID

from fastapi import APIRouter, Depends, status
from sqlalchemy.ext.asyncio import AsyncSession

from app.api.deps import get_db, get_pagination, require_tenant
from app.core.security import get_current_user
from app.schemas.connectors import (
    ConnectorDispatchCreate,
    ConnectorDispatchRead,
    ConnectorRegistrationCreate,
    ConnectorRegistrationRead,
)
from app.services.connectors import ConnectorDispatchService, ConnectorRegistrationService
from shared.pagination import PaginationParams
from shared.security import CurrentUser

connector_registrations_router = APIRouter()
connector_dispatches_router = APIRouter()


@connector_registrations_router.post(
    "", response_model=ConnectorRegistrationRead, status_code=status.HTTP_201_CREATED
)
async def create_connector_registration(
    body: ConnectorRegistrationCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await ConnectorRegistrationService(db).create(tenant_id, body, actor=user)


@connector_registrations_router.get("", response_model=list[ConnectorRegistrationRead])
async def list_connector_registrations(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await ConnectorRegistrationService(db).list(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@connector_registrations_router.get(
    "/{registration_id}", response_model=ConnectorRegistrationRead
)
async def get_connector_registration(
    registration_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await ConnectorRegistrationService(db).get(tenant_id, registration_id)


@connector_dispatches_router.post(
    "", response_model=ConnectorDispatchRead, status_code=status.HTTP_201_CREATED
)
async def create_connector_dispatch(
    body: ConnectorDispatchCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await ConnectorDispatchService(db).create(tenant_id, body, actor=user)


@connector_dispatches_router.get("", response_model=list[ConnectorDispatchRead])
async def list_connector_dispatches(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await ConnectorDispatchService(db).list(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@connector_dispatches_router.get("/{dispatch_id}", response_model=ConnectorDispatchRead)
async def get_connector_dispatch(
    dispatch_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await ConnectorDispatchService(db).get(tenant_id, dispatch_id)
