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

import sqlalchemy
import sqlalchemy.ext.asyncio

from mcpuniverse.app.db.sqlc import models


CREATE_PROJECT = """-- name: create_project \\:one
INSERT INTO "project" (owner_id,
                       name,
                       description,
                       configuration)
VALUES (:p1, :p2, :p3, :p4)
RETURNING id, owner_id, name, description, configuration, created_at, updated_at
"""


DELETE_PROJECT = """-- name: delete_project \\:exec
DELETE
FROM "project"
WHERE owner_id = :p1 AND name = :p2
"""


GET_PROJECT_BY_ID = """-- name: get_project_by_id \\:one
SELECT id, owner_id, name, description, configuration, created_at, updated_at
FROM "project"
WHERE id = :p1
LIMIT 1
"""


GET_PROJECT_BY_NAME = """-- name: get_project_by_name \\:one
SELECT id, owner_id, name, description, configuration, created_at, updated_at
FROM "project"
WHERE owner_id = :p1 AND name = :p2
LIMIT 1
"""


GET_PROJECT_ID = """-- name: get_project_id \\:one
SELECT id
FROM "project"
WHERE owner_id = :p1 AND name = :p2
LIMIT 1
"""


LIST_PROJECTS = """-- name: list_projects \\:many
SELECT name
FROM "project"
WHERE owner_id = :p1
LIMIT :p2
OFFSET :p3
"""


UPDATE_PROJECT = """-- name: update_project \\:one
UPDATE "project"
SET description   = COALESCE(:p1, description),
    configuration = COALESCE(:p2, configuration),
    updated_at    = COALESCE(:p3, updated_at)
WHERE owner_id = :p4 AND name = :p5
RETURNING id, owner_id, name, description, configuration, created_at, updated_at
"""


@dataclasses.dataclass()
class UpdateProjectParams:
    description: Optional[str]
    configuration: Optional[str]
    updated_at: Optional[datetime.datetime]
    owner_id: int
    name: str


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

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

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

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

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

    def get_project_id(self, *, owner_id: int, name: str) -> Optional[int]:
        row = self._conn.execute(sqlalchemy.text(GET_PROJECT_ID), {"p1": owner_id, "p2": name}).first()
        if row is None:
            return None
        return row[0]

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

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


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

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

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

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

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

    async def get_project_id(self, *, owner_id: int, name: str) -> Optional[int]:
        row = (await self._conn.execute(sqlalchemy.text(GET_PROJECT_ID), {"p1": owner_id, "p2": name})).first()
        if row is None:
            return None
        return row[0]

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

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