from collections.abc import Generator
from pathlib import Path

from pydantic import BaseModel, Field

from huesoporro.libs.db import MarkovDatabase
from huesoporro.settings import Settings
from huesoporro.svc.clean_cc_svc import CleanCCSvc
from huesoporro.svc.download_closed_captions import DownloadClosedCaptionsSvc
from huesoporro.svc.store import SentenceStorerSvc


class ImportFromVODAction(BaseModel):
    download_closed_captions_svc: DownloadClosedCaptionsSvc

    clean_cc_svc: CleanCCSvc
    s: Settings = Field(default_factory=Settings.get)

    ignore_lines: set[str] = {
        "WEBVTT",
        "Kind: captions",
        "Language: en",
        "Language: es",
    }

    def run(self, channel_name: str, youtube_url: str) -> Generator[Path, None, None]:
        for cc_filepath in self.download_closed_captions_svc.run(youtube_url):
            storer_svc = SentenceStorerSvc(
                db=MarkovDatabase(channel=channel_name, settings=self.s)
            )
            for line in self.clean_cc_svc.run(cc_filepath):
                if line and line not in self.ignore_lines:
                    storer_svc.store_sentence(line.strip())
            yield cc_filepath
