from pathlib import Path

from jinja2 import Template
from pydantic2ts import generate_typescript_defs
from rich import print

# ============================================================
# Global Compoenents Generator

template = """// This Code is auto generated by gen_global_components.py
{% for name in global %} import {{ name }} from "@/components/global/{{ name }}.vue";
{% endfor %}
{% for name in layout %} import {{ name }} from "@/components/layout/{{ name }}.vue";
{% endfor %}

declare module "vue" {
  export interface GlobalComponents {
    // Global Components
    {% for name in global %} {{ name }}: typeof {{ name }};
    {% endfor %} // Layout Components
    {% for name in layout %} {{ name }}: typeof {{ name }};
    {% endfor %}
  }
}

export {};
"""

CWD = Path(__file__).parent
PROJECT_DIR = Path(__file__).parent.parent.parent


def generate_global_components_types() -> None:
    destination_file = PROJECT_DIR / "frontend" / "types" / "components.d.ts"

    component_paths = {
        "global": PROJECT_DIR / "frontend" / "components" / "global",
        "layout": PROJECT_DIR / "frontend" / "components" / "Layout",
    }

    def render_template(template: str, data: dict) -> str | None:
        tmpl = Template(template)
        return tmpl.render(**data)

    def build_data() -> dict:
        data = {}
        for name, path in component_paths.items():
            components = [component.stem for component in path.glob("*.vue")]
            data[name] = components

        return data

    def write_template(text: str) -> None:
        destination_file.write_text(text)

    text = render_template(template, build_data())

    if text:
        write_template(text)


# ============================================================
# Pydantic To Typescript Generator


def generate_typescript_types() -> None:
    def path_to_module(path: Path):
        str_path: str = str(path)

        str_path = str_path.removeprefix(str(PROJECT_DIR))
        str_path = str_path.removeprefix("/")
        str_path = str_path.replace("/", ".")

        return str_path

    schema_path = PROJECT_DIR / "mealie" / "schema"
    types_dir = PROJECT_DIR / "frontend" / "types" / "api-types"

    ignore_dirs = ["__pycache__", "static", "_mealie"]

    skipped_files: list[Path] = []
    skipped_dirs: list[Path] = []
    failed_modules: list[Path] = []

    for module in schema_path.iterdir():
        if module.is_dir() and module.stem in ignore_dirs:
            skipped_dirs.append(module)
            continue

        if not module.is_dir() or not module.joinpath("__init__.py").is_file():
            skipped_files.append(module)
            continue

        ts_out_name = module.name.replace("_", "-") + ".ts"
        out_path = types_dir.joinpath(ts_out_name)

        try:
            path_as_module = path_to_module(module)
            generate_typescript_defs(path_as_module, str(out_path), exclude=("MealieModel"))  # type: ignore
        except Exception as e:
            failed_modules.append(module)
            print("\nModule Errors:", module, "-----------------")  # noqa
            print(e)  # noqa
            print("Finished Module Errors:", module, "-----------------\n")  # noqa

    print("\n📁 Skipped Directories:")  # noqa
    for skipped_dir in skipped_dirs:
        print("   📁", skipped_dir.name)  # noqa

    print("📄 Skipped Files:")  # noqa
    for f in skipped_files:
        print("   📄", f.name)  # noqa

    print("❌ Failed Modules:")  # noqa
    for f in failed_modules:
        print("   ❌", f.name)  # noqa


if __name__ == "__main__":
    print("\n-- Starting Global Components Generator --")  # noqa
    generate_global_components_types()

    print("\n-- Starting Pydantic To Typescript Generator --")  # noqa
    generate_typescript_types()
