import pathlib
import shutil
import tempfile

from rendercv.cli.render_command.progress_panel import ProgressPanel
from rendercv.cli.render_command.run_rendercv import run_rendercv
from rendercv.schema.models.design.built_in_design import available_themes
from rendercv.schema.sample_generator import create_sample_yaml_input_file

repository_root = pathlib.Path(__file__).parent.parent
rendercv_path = repository_root / "rendercv"
image_assets_directory = repository_root / "docs" / "assets" / "images" / "examples"
rendercv_typst_examples_directory = (
    repository_root / "src" / "rendercv" / "renderer" / "rendercv_typst" / "examples"
)

examples_directory_path = pathlib.Path(__file__).parent.parent / "examples"

# Check if examples directory exists. If not, create it
if not examples_directory_path.exists():
    examples_directory_path.mkdir()

for theme in available_themes:
    yaml_file_path = (
        examples_directory_path / f"John_Doe_{theme.capitalize()}Theme_CV.yaml"
    )
    create_sample_yaml_input_file(
        file_path=yaml_file_path,
        name="John Doe",
        theme=theme,
        locale="english",
    )

    with tempfile.TemporaryDirectory() as temp_directory:
        temp_directory_path = pathlib.Path(temp_directory)
        run_rendercv(
            yaml_file_path,
            progress=ProgressPanel(),
            typst_path=temp_directory_path / f"{yaml_file_path.stem}.typ",
            pdf_path=examples_directory_path / f"{yaml_file_path.stem}.pdf",
            png_path=temp_directory_path / f"{yaml_file_path.stem}.png",
            dont_generate_html=True,
            dont_generate_markdown=True,
        )

        image_assets_directory.mkdir(parents=True, exist_ok=True)
        shutil.copy(
            temp_directory_path / f"{yaml_file_path.stem}_1.png",
            image_assets_directory / f"{theme}.png",
        )

        rendercv_typst_examples_directory.mkdir(parents=True, exist_ok=True)
        shutil.copy(
            temp_directory_path / f"{yaml_file_path.stem}.typ",
            rendercv_typst_examples_directory / f"{theme}.typ",
        )


print("Examples generated successfully.")  # NOQA: T201
