"""
Hatchling build hook — copies docs/content into src/django_cfg/docs/ before build,
removes it after. This ensures RECORD is correct without manual wheel patching.
"""

import shutil
from pathlib import Path

from hatchling.builders.hooks.plugin.interface import BuildHookInterface


class CustomBuildHook(BuildHookInterface):
    PLUGIN_NAME = "custom"

    def initialize(self, version: str, build_data: dict) -> None:
        root = Path(self.root)
        docs_src = root / "../../solution/frontend/apps/docs/content"
        docs_dst = root / "src/django_cfg/docs"

        if docs_src.exists():
            if docs_dst.exists():
                shutil.rmtree(docs_dst)
            shutil.copytree(
                docs_src,
                docs_dst,
                ignore=shutil.ignore_patterns("*.png", "*.jpg", "*.jpeg", "*.gif", "*.ico", "*.svg"),
            )
            self._copied = True
        else:
            self._copied = False

    def finalize(self, version: str, build_data: dict, artifact_path: str) -> None:
        if getattr(self, "_copied", False):
            docs_dst = Path(self.root) / "src/django_cfg/docs"
            if docs_dst.exists():
                shutil.rmtree(docs_dst)
