from typing import List, Optional, Any
from psycopg import AsyncConnection


class SchemeRepository:

    @staticmethod
    async def get_schemes(conn: AsyncConnection) -> List[dict]:
        """
        查询pg数据库中, scheme_list 的所有记录
        :param conn: 异步数据库连接
        :return: 包含所有记录的列表, 每条记录为一个字典
        """
        async with conn.cursor() as cur:
            await cur.execute(
                """
                SELECT scheme_id, scheme_name, scheme_type, username, create_time, scheme_start_time, scheme_detail
                FROM public.scheme_list
                """
            )
            records = await cur.fetchall()

            scheme_list = []
            for record in records:
                scheme_list.append(
                    {
                        "scheme_id": record["scheme_id"],
                        "scheme_name": record["scheme_name"],
                        "scheme_type": record["scheme_type"],
                        "username": record["username"],
                        "create_time": record["create_time"],
                        "scheme_start_time": record["scheme_start_time"],
                        "scheme_detail": record["scheme_detail"],
                    }
                )

            return scheme_list

    @staticmethod
    async def get_burst_locate_results(conn: AsyncConnection) -> List[dict]:
        """
        查询pg数据库中, burst_locate_result 的所有记录
        :param conn: 异步数据库连接
        :return: 包含所有记录的列表, 每条记录为一个字典
        """
        async with conn.cursor() as cur:
            await cur.execute(
                """
                SELECT id, type, burst_incident, leakage, detect_time, locate_result
                FROM public.burst_locate_result
                """
            )
            records = await cur.fetchall()

            results = []
            for record in records:
                results.append(
                    {
                        "id": record["id"],
                        "type": record["type"],
                        "burst_incident": record["burst_incident"],
                        "leakage": record["leakage"],
                        "detect_time": record["detect_time"],
                        "locate_result": record["locate_result"],
                    }
                )

            return results

    @staticmethod
    async def get_burst_locate_result_by_incident(
        conn: AsyncConnection, burst_incident: str
    ) -> List[dict]:
        """
        根据 burst_incident 查询爆管定位结果
        :param conn: 异步数据库连接
        :param burst_incident: 爆管事件标识
        :return: 包含匹配记录的列表
        """
        async with conn.cursor() as cur:
            await cur.execute(
                """
                SELECT id, type, burst_incident, leakage, detect_time, locate_result
                FROM public.burst_locate_result
                WHERE burst_incident = %s
                """,
                (burst_incident,),
            )
            records = await cur.fetchall()

            results = []
            for record in records:
                results.append(
                    {
                        "id": record["id"],
                        "type": record["type"],
                        "burst_incident": record["burst_incident"],
                        "leakage": record["leakage"],
                        "detect_time": record["detect_time"],
                        "locate_result": record["locate_result"],
                    }
                )

            return results
