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

import sqlalchemy
import sqlalchemy.ext.asyncio

from mcpuniverse.app.db.sqlc import models


CREATE_RELEASED_TASK = """-- name: create_released_task \\:one
INSERT INTO "released_task" (benchmark_id,
                             name,
                             tag,
                             category,
                             question,
                             data,
                             is_public)
VALUES (:p1, :p2, :p3, :p4, :p5, :p6, :p7)
RETURNING id, benchmark_id, name, tag, category, question, data, is_public, created_at
"""


@dataclasses.dataclass()
class CreateReleasedTaskParams:
    benchmark_id: int
    name: str
    tag: str
    category: Optional[str]
    question: Optional[str]
    data: Optional[str]
    is_public: Optional[bool]


DELETE_RELEASED_TASK = """-- name: delete_released_task \\:exec
DELETE
FROM "released_task"
WHERE id = :p1
"""


GET_RELEASED_TASK_BY_ID = """-- name: get_released_task_by_id \\:one
SELECT id, benchmark_id, name, tag, category, question, data, is_public, created_at
FROM "released_task"
WHERE id = :p1
LIMIT 1
"""


GET_RELEASED_TASK_BY_NAME = """-- name: get_released_task_by_name \\:many
SELECT id, benchmark_id, name, tag, category, question, data, is_public, created_at
FROM "released_task"
WHERE benchmark_id = :p1 AND name = :p2
"""


GET_RELEASED_TASK_BY_NAME_AND_TAG = """-- name: get_released_task_by_name_and_tag \\:one
SELECT id, benchmark_id, name, tag, category, question, data, is_public, created_at
FROM "released_task"
WHERE benchmark_id = :p1 AND name = :p2 AND tag = :p3
LIMIT 1
"""


GET_RELEASED_TASK_CONFIGS = """-- name: get_released_task_configs \\:many
SELECT data
FROM "released_task"
WHERE benchmark_id = :p1
"""


GET_RELEASED_TASK_NAMES = """-- name: get_released_task_names \\:many
SELECT name
FROM "released_task"
WHERE benchmark_id = :p1
"""


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

    def create_released_task(self, arg: CreateReleasedTaskParams) -> Optional[models.ReleasedTask]:
        row = self._conn.execute(sqlalchemy.text(CREATE_RELEASED_TASK), {
            "p1": arg.benchmark_id,
            "p2": arg.name,
            "p3": arg.tag,
            "p4": arg.category,
            "p5": arg.question,
            "p6": arg.data,
            "p7": arg.is_public,
        }).first()
        if row is None:
            return None
        return models.ReleasedTask(
            id=row[0],
            benchmark_id=row[1],
            name=row[2],
            tag=row[3],
            category=row[4],
            question=row[5],
            data=row[6],
            is_public=row[7],
            created_at=row[8],
        )

    def delete_released_task(self, *, id: int) -> None:
        self._conn.execute(sqlalchemy.text(DELETE_RELEASED_TASK), {"p1": id})

    def get_released_task_by_id(self, *, id: int) -> Optional[models.ReleasedTask]:
        row = self._conn.execute(sqlalchemy.text(GET_RELEASED_TASK_BY_ID), {"p1": id}).first()
        if row is None:
            return None
        return models.ReleasedTask(
            id=row[0],
            benchmark_id=row[1],
            name=row[2],
            tag=row[3],
            category=row[4],
            question=row[5],
            data=row[6],
            is_public=row[7],
            created_at=row[8],
        )

    def get_released_task_by_name(self, *, benchmark_id: int, name: str) -> Iterator[models.ReleasedTask]:
        result = self._conn.execute(sqlalchemy.text(GET_RELEASED_TASK_BY_NAME), {"p1": benchmark_id, "p2": name})
        for row in result:
            yield models.ReleasedTask(
                id=row[0],
                benchmark_id=row[1],
                name=row[2],
                tag=row[3],
                category=row[4],
                question=row[5],
                data=row[6],
                is_public=row[7],
                created_at=row[8],
            )

    def get_released_task_by_name_and_tag(self, *, benchmark_id: int, name: str, tag: str) -> Optional[models.ReleasedTask]:
        row = self._conn.execute(sqlalchemy.text(GET_RELEASED_TASK_BY_NAME_AND_TAG), {"p1": benchmark_id, "p2": name, "p3": tag}).first()
        if row is None:
            return None
        return models.ReleasedTask(
            id=row[0],
            benchmark_id=row[1],
            name=row[2],
            tag=row[3],
            category=row[4],
            question=row[5],
            data=row[6],
            is_public=row[7],
            created_at=row[8],
        )

    def get_released_task_configs(self, *, benchmark_id: int) -> Iterator[Optional[str]]:
        result = self._conn.execute(sqlalchemy.text(GET_RELEASED_TASK_CONFIGS), {"p1": benchmark_id})
        for row in result:
            yield row[0]

    def get_released_task_names(self, *, benchmark_id: int) -> Iterator[str]:
        result = self._conn.execute(sqlalchemy.text(GET_RELEASED_TASK_NAMES), {"p1": benchmark_id})
        for row in result:
            yield row[0]


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

    async def create_released_task(self, arg: CreateReleasedTaskParams) -> Optional[models.ReleasedTask]:
        row = (await self._conn.execute(sqlalchemy.text(CREATE_RELEASED_TASK), {
            "p1": arg.benchmark_id,
            "p2": arg.name,
            "p3": arg.tag,
            "p4": arg.category,
            "p5": arg.question,
            "p6": arg.data,
            "p7": arg.is_public,
        })).first()
        if row is None:
            return None
        return models.ReleasedTask(
            id=row[0],
            benchmark_id=row[1],
            name=row[2],
            tag=row[3],
            category=row[4],
            question=row[5],
            data=row[6],
            is_public=row[7],
            created_at=row[8],
        )

    async def delete_released_task(self, *, id: int) -> None:
        await self._conn.execute(sqlalchemy.text(DELETE_RELEASED_TASK), {"p1": id})

    async def get_released_task_by_id(self, *, id: int) -> Optional[models.ReleasedTask]:
        row = (await self._conn.execute(sqlalchemy.text(GET_RELEASED_TASK_BY_ID), {"p1": id})).first()
        if row is None:
            return None
        return models.ReleasedTask(
            id=row[0],
            benchmark_id=row[1],
            name=row[2],
            tag=row[3],
            category=row[4],
            question=row[5],
            data=row[6],
            is_public=row[7],
            created_at=row[8],
        )

    async def get_released_task_by_name(self, *, benchmark_id: int, name: str) -> AsyncIterator[models.ReleasedTask]:
        result = await self._conn.stream(sqlalchemy.text(GET_RELEASED_TASK_BY_NAME), {"p1": benchmark_id, "p2": name})
        async for row in result:
            yield models.ReleasedTask(
                id=row[0],
                benchmark_id=row[1],
                name=row[2],
                tag=row[3],
                category=row[4],
                question=row[5],
                data=row[6],
                is_public=row[7],
                created_at=row[8],
            )

    async def get_released_task_by_name_and_tag(self, *, benchmark_id: int, name: str, tag: str) -> Optional[models.ReleasedTask]:
        row = (await self._conn.execute(sqlalchemy.text(GET_RELEASED_TASK_BY_NAME_AND_TAG), {"p1": benchmark_id, "p2": name, "p3": tag})).first()
        if row is None:
            return None
        return models.ReleasedTask(
            id=row[0],
            benchmark_id=row[1],
            name=row[2],
            tag=row[3],
            category=row[4],
            question=row[5],
            data=row[6],
            is_public=row[7],
            created_at=row[8],
        )

    async def get_released_task_configs(self, *, benchmark_id: int) -> AsyncIterator[Optional[str]]:
        result = await self._conn.stream(sqlalchemy.text(GET_RELEASED_TASK_CONFIGS), {"p1": benchmark_id})
        async for row in result:
            yield row[0]

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