"""FastAPI dependencies for the formal cloud workbench."""
from __future__ import annotations

import os
from typing import Iterator

from fastapi import HTTPException

from acquisition.repositories.postgres import PostgresAcquisitionRepository
from core.config import CreationDbConfig
from core.db_session import transaction
from decode_content.repositories.postgres import PostgresDecodeRepository


def _env_file() -> str:
    return os.getenv("CK_ENV_FILE", ".env")


def get_creation_db_config() -> CreationDbConfig:
    return CreationDbConfig.from_env(_env_file())


def get_acquisition_repository() -> Iterator[PostgresAcquisitionRepository]:
    with transaction(get_creation_db_config()) as conn:
        yield PostgresAcquisitionRepository(conn)


def get_decode_repository() -> Iterator[PostgresDecodeRepository]:
    with transaction(get_creation_db_config()) as conn:
        yield PostgresDecodeRepository(conn)


def get_pipeline_repository():
    raise HTTPException(status_code=501, detail="pipeline repository persistence is wired in Step 7")
