from uuid import UUID

from psycopg import Connection
from src.infrastructure.repositories.contract_provider_repository import ContractProviderRepository


class ContractProviderUseCase:
    def __init__(
        self,
        contract_provider_repository: ContractProviderRepository,
        connection: Connection,
    ) -> None:
        self.contract_provider_repository = contract_provider_repository
        self.connection = connection

    @classmethod
    def create(cls, connection: Connection) -> "ContractProviderUseCase":
        contract_provider_repository = ContractProviderRepository(connection=connection)
        return ContractProviderUseCase(
            contract_provider_repository=contract_provider_repository,
            connection=connection,
        )

    def add(
        self,
        contract_provider_id: UUID,  # NOTE: application_clientの命名規則の依存を断ち切った後にDB側で生成するようにする。
        name: str,
        application_client_id: str,
    ) -> None:
        """ContractProviderを追加する。※ toolsからの呼び出しに限定する。"""
        with self.connection.transaction():
            self.contract_provider_repository.add(
                contract_provider_id=contract_provider_id,
                name=name,
                application_client_id=application_client_id,
            )
