from __future__ import annotations

from pathlib import Path
from typing import List

from pydantic import BaseModel, field_validator
from pydantic_yaml import parse_yaml_file_as

from srs2std.srs.structure import SectionTitle, TableOfContents


class Template(BaseModel):
    section_templates: List[SectionTemplate]

    def to_toc(self) -> TableOfContents:
        return TableOfContents(
            titles=[
                section_template.title for section_template in self.section_templates
            ]
        )

    def overall_structure(self) -> str:
        return self.to_toc().overall_structure()


class SectionTemplate(BaseModel):
    title: SectionTitle
    description: str

    @field_validator("description", mode="before")
    def strip_description(cls, v: str) -> str:
        return v.strip()


def load_template_yaml(path: Path) -> Template:
    return parse_yaml_file_as(Template, path)
