"""Miroir de ~/Github sans dépendances, caches ni .env."""

from __future__ import annotations

from osx_backup.context import Context
from osx_backup.fsutil import DEPENDENCY_DIRS
from osx_backup.modules.base import Inventory, register
from osx_backup.modules.rsyncmodule import RsyncModule


@register
class GithubModule(RsyncModule):
    name = "github"
    description = "Projets ~/Github (sans node_modules, .venv, builds ; .env via secrets)"
    order = 80
    source_rel = "Github"
    excludes = DEPENDENCY_DIRS + [".DS_Store", ".env", ".env.*"]

    def inventory(self, ctx: Context) -> Inventory:
        inv = super().inventory(ctx)
        if not inv.available:
            return inv
        # Un dossier sans .git n'existe nulle part ailleurs : le signaler avant la copie.
        try:
            non_git = sorted(
                d.name
                for d in self.source(ctx).iterdir()
                if d.is_dir() and not (d / ".git").exists()
            )
        except OSError as exc:
            # Dossier illisible : le miroir reste possible, seul le contrôle git saute.
            inv.warnings.append(f"impossible de lister ~/{self.source_rel} : {exc.strerror or exc}")
            return inv
        if non_git:
            inv.warnings.append(f"{len(non_git)} dossier(s) sans dépôt git : {', '.join(non_git)}")
        return inv
