from uuid import UUID

import psycopg
from psycopg.rows import class_row
from src.__types.errors import ResourceNotFoundError
from src.__types.index import Client
from src.settings import Settings

settings = Settings()


class ClientRepository:
    """クライアントを管理するリポジトリ"""

    def __init__(self, connection: psycopg.Connection) -> None:
        self.connection = connection

    def get(self, client_id: UUID) -> Client:
        """指定されたIDのクライアントを取得する"""
        with self.connection.cursor(row_factory=class_row(Client)) as cur:
            cur.execute(
                """--sql
                SELECT id, name, stage FROM clients WHERE id = %(client_id)s
                """,
                {
                    "client_id": client_id,
                },
            )
            if client := cur.fetchone():
                return client
            raise ResourceNotFoundError(f"Client with id {client_id} not found.")
