from abc import ABCMeta

import pandas as pd
from sqlalchemy import text

from src.db.database import Database
from src.model.db.base_db_model import BaseDBModel


class BaseRepository(metaclass=ABCMeta):

    _database: Database

    def __init__(self, db: Database) -> None:
        self._database = db

    def fetch_all(self) -> list[BaseDBModel]:
        """データ全件取得メソッド"""
        pass

    def fetch_one(self, parameter: dict) -> BaseDBModel:
        """データ1件取得メソッド"""
        pass

    def fetch_many(self, parameter: dict) -> list[BaseDBModel]:
        """条件付きデータ取得メソッド"""
        pass

    def fetch_as_data_frame(self, parameter: dict) -> pd.DataFrame:
        """DBの取得結果をデータフレームにして返す"""
        pass

    def _to_data_frame(self, query, parameter: BaseDBModel):
        """DBの取得結果をデータフレームに変換する"""
        params = params = parameter.dict()

        sql_query = pd.read_sql(
            text(query),
            con=self._database.connection,
            params=params)

        df = pd.DataFrame(
            sql_query,
            index=None
        )
        return df
