from collections.abc import Iterable, Iterator

from fastapi import HTTPException
from folkugat_web.dal.sql.temes import scores as scores_dal
from folkugat_web.model import temes as model
from folkugat_web.model.lilypond import score as lilypond_model
from folkugat_web.services import lilypond
from folkugat_web.services.temes import lyrics as lyrics_service
from folkugat_web.services.temes import properties as properties_service
from folkugat_web.services.temes import query as temes_q
from folkugat_web.utils import FnChain


def add_scores_to_tema(tema: model.Tema) -> model.Tema:
    if tema.id is not None:
        tema.scores = list(scores_dal.get_scores(tema_id=tema.id))
    return tema


def add_scores_to_temes(temes: Iterable[model.Tema]) -> Iterator[model.Tema]:
    return map(add_scores_to_tema, temes)


def get_score_by_id(score_id: int, tema_id: int | None = None) -> model.Score | None:
    return next(iter(scores_dal.get_scores(score_id=score_id, tema_id=tema_id)), None)


def update_score(score: model.Score):
    scores_dal.update_score(score=score)


def delete_score(score_id: int, tema_id: int | None = None):
    scores_dal.delete_score(score_id=score_id, tema_id=tema_id)


def create_score(tema_id: int) -> model.Score:
    new_score = model.Score(
        id=None,
        tema_id=tema_id,
        title="",
        source="",
        errors=[],
        img_url=None,
        pdf_url=None,
        hidden=True,
    )
    return scores_dal.insert_score(score=new_score)


def build_tune_set_full_source(tema_ids: list[int]) -> str:
    temes = (
        FnChain.transform(temes_q.get_temes_by_ids(tema_ids=tema_ids)) |
        properties_service.add_properties_to_temes |
        lyrics_service.add_lyrics_to_temes |
        add_scores_to_temes |
        list
    ).result()
    if not temes:
        return ""
    set_title = " i ".join(filter(bool, [
        ", ".join([tema.title for tema in temes[:-1]]),
        temes[-1].title
    ]))
    tune_set = lilypond_model.LilypondSet(
        title=set_title,
        tunes=[
            lilypond_model.LilypondTune(
                header=lilypond_model.HeaderData.from_tema(tema=tema),
                score_source=tema.scores[0].source if tema.scores else None,
                lyrics=lilypond_model.LyricsText.from_lyrics(lyrics=tema.lyrics[0]) if tema.lyrics else None,
            ) for tema in temes
        ]
    )
    print("TUNE SET HASH: ", tune_set.hash().hex())
    return lilypond.tune_set_score(tune_set=tune_set)
