"""API routes."""
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.api.permissions import require_permissions
from shared.pagination import PaginationParams
from shared.security import CurrentUser

from app.permissions.definitions import INTEGRATIONS_CREATE, INTEGRATIONS_VIEW
from app.permissions.definitions import MARKETING_CREATE, MARKETING_VIEW
from app.schemas.marketing import CommunicationIntegrationConfigCreate, CommunicationIntegrationConfigRead
from app.schemas.marketing import LoyaltyIntegrationConfigCreate, LoyaltyIntegrationConfigRead
from app.schemas.marketing import MarketingCampaignRefCreate, MarketingCampaignRefRead
from app.services.marketing import CommunicationIntegrationConfigService
from app.services.marketing import LoyaltyIntegrationConfigService
from app.services.marketing import MarketingCampaignRefService


marketingCampaignRef_router = APIRouter()

@marketingCampaignRef_router.post("", response_model=MarketingCampaignRefRead, status_code=status.HTTP_201_CREATED)
async def create_marketingCampaignRef(body: MarketingCampaignRefCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(require_permissions(MARKETING_CREATE))):
    return await MarketingCampaignRefService(db).create(tenant_id, body, actor=user)

@marketingCampaignRef_router.get("", response_model=list[MarketingCampaignRefRead])
async def list_marketingCampaignRef(tenant_id: UUID = Depends(require_tenant), pagination: PaginationParams = Depends(get_pagination), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(MARKETING_VIEW))):
    return await MarketingCampaignRefService(db).list(tenant_id, offset=pagination.offset, limit=pagination.page_size)

@marketingCampaignRef_router.get("/{entity_id}", response_model=MarketingCampaignRefRead)
async def get_marketingCampaignRef(entity_id: UUID, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(MARKETING_VIEW))):
    return await MarketingCampaignRefService(db).get(tenant_id, entity_id)


loyaltyIntegrationConfig_router = APIRouter()

@loyaltyIntegrationConfig_router.post("", response_model=LoyaltyIntegrationConfigRead, status_code=status.HTTP_201_CREATED)
async def create_loyaltyIntegrationConfig(body: LoyaltyIntegrationConfigCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(require_permissions(INTEGRATIONS_CREATE))):
    return await LoyaltyIntegrationConfigService(db).create(tenant_id, body, actor=user)

@loyaltyIntegrationConfig_router.get("", response_model=list[LoyaltyIntegrationConfigRead])
async def list_loyaltyIntegrationConfig(tenant_id: UUID = Depends(require_tenant), pagination: PaginationParams = Depends(get_pagination), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(INTEGRATIONS_VIEW))):
    return await LoyaltyIntegrationConfigService(db).list(tenant_id, offset=pagination.offset, limit=pagination.page_size)

@loyaltyIntegrationConfig_router.get("/{entity_id}", response_model=LoyaltyIntegrationConfigRead)
async def get_loyaltyIntegrationConfig(entity_id: UUID, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(INTEGRATIONS_VIEW))):
    return await LoyaltyIntegrationConfigService(db).get(tenant_id, entity_id)


communicationIntegrationConfig_router = APIRouter()

@communicationIntegrationConfig_router.post("", response_model=CommunicationIntegrationConfigRead, status_code=status.HTTP_201_CREATED)
async def create_communicationIntegrationConfig(body: CommunicationIntegrationConfigCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(require_permissions(INTEGRATIONS_CREATE))):
    return await CommunicationIntegrationConfigService(db).create(tenant_id, body, actor=user)

@communicationIntegrationConfig_router.get("", response_model=list[CommunicationIntegrationConfigRead])
async def list_communicationIntegrationConfig(tenant_id: UUID = Depends(require_tenant), pagination: PaginationParams = Depends(get_pagination), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(INTEGRATIONS_VIEW))):
    return await CommunicationIntegrationConfigService(db).list(tenant_id, offset=pagination.offset, limit=pagination.page_size)

@communicationIntegrationConfig_router.get("/{entity_id}", response_model=CommunicationIntegrationConfigRead)
async def get_communicationIntegrationConfig(entity_id: UUID, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(INTEGRATIONS_VIEW))):
    return await CommunicationIntegrationConfigService(db).get(tenant_id, entity_id)


from app.schemas.marketing import IntegrationDispatchCreate, IntegrationDispatchRead
from app.services.marketing import IntegrationDispatchService

integrationDispatch_router = APIRouter()


@integrationDispatch_router.post("", response_model=IntegrationDispatchRead, status_code=status.HTTP_201_CREATED)
async def create_integration_dispatch(
    body: IntegrationDispatchCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(INTEGRATIONS_CREATE)),
):
    return await IntegrationDispatchService(db).create(tenant_id, body, actor=user)


@integrationDispatch_router.get("", response_model=list[IntegrationDispatchRead])
async def list_integration_dispatches(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(INTEGRATIONS_VIEW)),
):
    return await IntegrationDispatchService(db).list(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@integrationDispatch_router.get("/{entity_id}", response_model=IntegrationDispatchRead)
async def get_integration_dispatch(
    entity_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(INTEGRATIONS_VIEW)),
):
    return await IntegrationDispatchService(db).get(tenant_id, entity_id)
