import json

from funai.llm import get_model
from fundb.sqlalchemy.table import BaseTable
from fundrive.drives.baidu.drive import BaiDuDrive
from funsecret import read_secret
from funutil import getLogger
from sqlalchemy import Engine, String
from sqlalchemy.orm import Mapped, Session, mapped_column

from .sketch import Sketch

logger = getLogger("funsketch")

model = get_model("deepseek")


class Episode(BaseTable):
    __tablename__ = "episode"
    sketch_id: Mapped[str] = mapped_column(String(64), comment="文件唯一ID", default="")
    index: Mapped[int] = mapped_column(comment="集", default="")
    name: Mapped[str] = mapped_column(String(128), comment="资源名称")
    size: Mapped[int] = mapped_column(comment="大小", default=0)
    fid: Mapped[str] = mapped_column(String(64), comment="文件唯一ID", default="")

    def _get_uid(self):
        return f"{self.sketch_id}:{self.index}"

    def _child(self):
        return Episode

    def _to_dict(self) -> dict:
        return {
            "sketch_id": self.sketch_id,
            "index": self.index,
            "name": self.name,
            "size": self.size,
            "fid": self.fid,
        }


def update_episode(
    engine: Engine, bduss=None, stoken=None, ptoken=None, *args, **kwargs
):
    bduss = bduss or read_secret("fundrive", "baidu", "bduss")
    stoken = stoken or read_secret("fundrive", "baidu", "stoken")
    ptoken = ptoken or read_secret("fundrive", "baidu", "ptoken")
    drive = BaiDuDrive()
    drive.login(bduss=bduss, stoken=stoken, ptoken=ptoken)

    with Session(engine) as session:
        res = BaseTable.select_all(session=session, table=Sketch)
        for sketch in res:
            print(sketch.uid)
            data = [
                data["fid"]
                for data in drive.get_file_list(sketch.fid)
                if data["fid"].endswith(".mp4")
            ]
            data = "\n".join([i for i in data])
            prompt = f"""
                下面是一部电视剧的所有文件路径，请分析这些文件名，返回视频顺序，要求返回结果是json的列表，包含path，index, name,只返回json，不要用```包含
                {data}
                """
            res = model.chat(prompt)
            res = json.loads(res)

            for data in res:
                print(data)
                episode = Episode(
                    fid=data["path"],
                    name=data["name"],
                    index=data["index"],
                    sketch_id=sketch.id,
                )
                episode.upsert(session=session)
            session.commit()
