# Code generated by sqlc. DO NOT EDIT.
# versions:
#   sqlc v1.29.0
# source: released_project.sql
import dataclasses
from typing import AsyncIterator, Iterator, Optional

import sqlalchemy
import sqlalchemy.ext.asyncio

from mcpuniverse.app.db.sqlc import models


CREATE_RELEASED_PROJECT = """-- name: create_released_project \\:one
INSERT INTO "released_project" (owner_id,
                                name,
                                tag,
                                description,
                                configuration)
VALUES (:p1, :p2, :p3, :p4, :p5)
RETURNING id, owner_id, name, tag, description, configuration, created_at
"""


@dataclasses.dataclass()
class CreateReleasedProjectParams:
    owner_id: int
    name: str
    tag: str
    description: Optional[str]
    configuration: Optional[str]


DELETE_RELEASED_PROJECT = """-- name: delete_released_project \\:exec
DELETE
FROM "released_project"
WHERE owner_id = :p1 AND name = :p2 AND tag = :p3
"""


GET_RELEASED_PROJECT_BY_ID = """-- name: get_released_project_by_id \\:one
SELECT id, owner_id, name, tag, description, configuration, created_at
FROM "released_project"
WHERE id = :p1
LIMIT 1
"""


GET_RELEASED_PROJECT_BY_NAME = """-- name: get_released_project_by_name \\:many
SELECT id, owner_id, name, tag, description, configuration, created_at
FROM "released_project"
WHERE owner_id = :p1 AND name = :p2
"""


GET_RELEASED_PROJECT_BY_NAME_AND_TAG = """-- name: get_released_project_by_name_and_tag \\:one
SELECT id, owner_id, name, tag, description, configuration, created_at
FROM "released_project"
WHERE owner_id = :p1 AND name = :p2 AND tag = :p3
LIMIT 1
"""


GET_RELEASED_TAGS = """-- name: get_released_tags \\:many
SELECT tag
FROM "released_project"
WHERE owner_id = :p1 AND name = :p2
"""


LIST_RELEASED_PROJECTS = """-- name: list_released_projects \\:many
SELECT DISTINCT name
FROM "released_project"
WHERE owner_id = :p1
LIMIT :p2
OFFSET :p3
"""


class Querier:
    def __init__(self, conn: sqlalchemy.engine.Connection):
        self._conn = conn

    def create_released_project(self, arg: CreateReleasedProjectParams) -> Optional[models.ReleasedProject]:
        row = self._conn.execute(sqlalchemy.text(CREATE_RELEASED_PROJECT), {
            "p1": arg.owner_id,
            "p2": arg.name,
            "p3": arg.tag,
            "p4": arg.description,
            "p5": arg.configuration,
        }).first()
        if row is None:
            return None
        return models.ReleasedProject(
            id=row[0],
            owner_id=row[1],
            name=row[2],
            tag=row[3],
            description=row[4],
            configuration=row[5],
            created_at=row[6],
        )

    def delete_released_project(self, *, owner_id: int, name: str, tag: str) -> None:
        self._conn.execute(sqlalchemy.text(DELETE_RELEASED_PROJECT), {"p1": owner_id, "p2": name, "p3": tag})

    def get_released_project_by_id(self, *, id: int) -> Optional[models.ReleasedProject]:
        row = self._conn.execute(sqlalchemy.text(GET_RELEASED_PROJECT_BY_ID), {"p1": id}).first()
        if row is None:
            return None
        return models.ReleasedProject(
            id=row[0],
            owner_id=row[1],
            name=row[2],
            tag=row[3],
            description=row[4],
            configuration=row[5],
            created_at=row[6],
        )

    def get_released_project_by_name(self, *, owner_id: int, name: str) -> Iterator[models.ReleasedProject]:
        result = self._conn.execute(sqlalchemy.text(GET_RELEASED_PROJECT_BY_NAME), {"p1": owner_id, "p2": name})
        for row in result:
            yield models.ReleasedProject(
                id=row[0],
                owner_id=row[1],
                name=row[2],
                tag=row[3],
                description=row[4],
                configuration=row[5],
                created_at=row[6],
            )

    def get_released_project_by_name_and_tag(self, *, owner_id: int, name: str, tag: str) -> Optional[models.ReleasedProject]:
        row = self._conn.execute(sqlalchemy.text(GET_RELEASED_PROJECT_BY_NAME_AND_TAG), {"p1": owner_id, "p2": name, "p3": tag}).first()
        if row is None:
            return None
        return models.ReleasedProject(
            id=row[0],
            owner_id=row[1],
            name=row[2],
            tag=row[3],
            description=row[4],
            configuration=row[5],
            created_at=row[6],
        )

    def get_released_tags(self, *, owner_id: int, name: str) -> Iterator[str]:
        result = self._conn.execute(sqlalchemy.text(GET_RELEASED_TAGS), {"p1": owner_id, "p2": name})
        for row in result:
            yield row[0]

    def list_released_projects(self, *, owner_id: int, limit: int, offset: int) -> Iterator[str]:
        result = self._conn.execute(sqlalchemy.text(LIST_RELEASED_PROJECTS), {"p1": owner_id, "p2": limit, "p3": offset})
        for row in result:
            yield row[0]


class AsyncQuerier:
    def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection):
        self._conn = conn

    async def create_released_project(self, arg: CreateReleasedProjectParams) -> Optional[models.ReleasedProject]:
        row = (await self._conn.execute(sqlalchemy.text(CREATE_RELEASED_PROJECT), {
            "p1": arg.owner_id,
            "p2": arg.name,
            "p3": arg.tag,
            "p4": arg.description,
            "p5": arg.configuration,
        })).first()
        if row is None:
            return None
        return models.ReleasedProject(
            id=row[0],
            owner_id=row[1],
            name=row[2],
            tag=row[3],
            description=row[4],
            configuration=row[5],
            created_at=row[6],
        )

    async def delete_released_project(self, *, owner_id: int, name: str, tag: str) -> None:
        await self._conn.execute(sqlalchemy.text(DELETE_RELEASED_PROJECT), {"p1": owner_id, "p2": name, "p3": tag})

    async def get_released_project_by_id(self, *, id: int) -> Optional[models.ReleasedProject]:
        row = (await self._conn.execute(sqlalchemy.text(GET_RELEASED_PROJECT_BY_ID), {"p1": id})).first()
        if row is None:
            return None
        return models.ReleasedProject(
            id=row[0],
            owner_id=row[1],
            name=row[2],
            tag=row[3],
            description=row[4],
            configuration=row[5],
            created_at=row[6],
        )

    async def get_released_project_by_name(self, *, owner_id: int, name: str) -> AsyncIterator[models.ReleasedProject]:
        result = await self._conn.stream(sqlalchemy.text(GET_RELEASED_PROJECT_BY_NAME), {"p1": owner_id, "p2": name})
        async for row in result:
            yield models.ReleasedProject(
                id=row[0],
                owner_id=row[1],
                name=row[2],
                tag=row[3],
                description=row[4],
                configuration=row[5],
                created_at=row[6],
            )

    async def get_released_project_by_name_and_tag(self, *, owner_id: int, name: str, tag: str) -> Optional[models.ReleasedProject]:
        row = (await self._conn.execute(sqlalchemy.text(GET_RELEASED_PROJECT_BY_NAME_AND_TAG), {"p1": owner_id, "p2": name, "p3": tag})).first()
        if row is None:
            return None
        return models.ReleasedProject(
            id=row[0],
            owner_id=row[1],
            name=row[2],
            tag=row[3],
            description=row[4],
            configuration=row[5],
            created_at=row[6],
        )

    async def get_released_tags(self, *, owner_id: int, name: str) -> AsyncIterator[str]:
        result = await self._conn.stream(sqlalchemy.text(GET_RELEASED_TAGS), {"p1": owner_id, "p2": name})
        async for row in result:
            yield row[0]

    async def list_released_projects(self, *, owner_id: int, limit: int, offset: int) -> AsyncIterator[str]:
        result = await self._conn.stream(sqlalchemy.text(LIST_RELEASED_PROJECTS), {"p1": owner_id, "p2": limit, "p3": offset})
        async for row in result:
            yield row[0]
