"""Per-route dependencies that construct services.""" from __future__ import annotations from typing import AsyncIterator from fastapi import Depends from motor.motor_asyncio import AsyncIOMotorCollection from ...dependencies import ( RequestContext, get_req_ctx, coll_orgs, coll_projects, coll_artifacts, coll_relationships, coll_jobs, coll_audit, ) from ...repositories import ( OrganizationRepository, ProjectRepository, ArtifactRepository, RelationshipRepository, JobRepository, AuditRepository, ) from ...services import ( OrganizationService, ProjectService, ArtifactService, RelationshipService, JobService, AuditService, OverviewService, ) # -------- Repositories -------- async def org_repo( c: AsyncIOMotorCollection = Depends(coll_orgs), ) -> AsyncIterator[OrganizationRepository]: yield OrganizationRepository(c) async def project_repo( c: AsyncIOMotorCollection = Depends(coll_projects), ) -> AsyncIterator[ProjectRepository]: yield ProjectRepository(c) async def artifact_repo( c: AsyncIOMotorCollection = Depends(coll_artifacts), ) -> AsyncIterator[ArtifactRepository]: yield ArtifactRepository(c) async def relationship_repo( c: AsyncIOMotorCollection = Depends(coll_relationships), arts: AsyncIOMotorCollection = Depends(coll_artifacts), ) -> AsyncIterator[RelationshipRepository]: yield RelationshipRepository(c, arts) async def job_repo( c: AsyncIOMotorCollection = Depends(coll_jobs), ) -> AsyncIterator[JobRepository]: yield JobRepository(c) async def audit_repo( c: AsyncIOMotorCollection = Depends(coll_audit), ) -> AsyncIterator[AuditRepository]: yield AuditRepository(c) # -------- Services (tenant-scoped) -------- async def org_service( orgs: OrganizationRepository = Depends(org_repo), audit: AuditRepository = Depends(audit_repo), ctx: RequestContext = Depends(get_req_ctx), ) -> OrganizationService: return OrganizationService( orgs, audit, actor_id=ctx.user_id, preferred_org_id=ctx.tenant_id, ) async def project_service( projects: ProjectRepository = Depends(project_repo), orgs: OrganizationRepository = Depends(org_repo), audit: AuditRepository = Depends(audit_repo), ctx: RequestContext = Depends(get_req_ctx), ) -> ProjectService: return ProjectService( projects, orgs, audit, tenant_id=ctx.require_tenant(), actor_id=ctx.user_id, ) async def artifact_service( artifacts: ArtifactRepository = Depends(artifact_repo), projects: ProjectRepository = Depends(project_repo), audit: AuditRepository = Depends(audit_repo), ctx: RequestContext = Depends(get_req_ctx), ) -> ArtifactService: return ArtifactService( artifacts, projects, audit, tenant_id=ctx.require_tenant(), actor_id=ctx.user_id, ) async def relationship_service( rels: RelationshipRepository = Depends(relationship_repo), audit: AuditRepository = Depends(audit_repo), ctx: RequestContext = Depends(get_req_ctx), ) -> RelationshipService: return RelationshipService( rels, audit, tenant_id=ctx.require_tenant(), actor_id=ctx.user_id, ) async def job_service( jobs: JobRepository = Depends(job_repo), projects: ProjectRepository = Depends(project_repo), audit: AuditRepository = Depends(audit_repo), ctx: RequestContext = Depends(get_req_ctx), ) -> JobService: return JobService( jobs, projects, audit, tenant_id=ctx.require_tenant(), actor_id=ctx.user_id, ) async def audit_service( audit: AuditRepository = Depends(audit_repo), ctx: RequestContext = Depends(get_req_ctx), ) -> AuditService: return AuditService(audit, tenant_id=ctx.require_tenant()) async def overview_service( artifacts: ArtifactRepository = Depends(artifact_repo), projects: ProjectRepository = Depends(project_repo), jobs: JobRepository = Depends(job_repo), ctx: RequestContext = Depends(get_req_ctx), ) -> OverviewService: return OverviewService( artifacts, projects, jobs, tenant_id=ctx.require_tenant(), )