"""Worker-local plugin selection and code publication. No application dependency imports.

Requests carry proposed data; discovery, snapshots and publication happen only
while the install lock is held. The stdlib boot journal owns crash recovery.
"""
from __future__ import annotations

import base64
import hashlib
import io
import json
import threading
from pathlib import Path

from pm.environments import dependency_home_root, install_state_dir, runtime_facts_path
from pm.filesystem import durable_write_bytes, file_digest, inherit_parent_acl, read_bytes_or_none
from pm.workspace import enabled_plugin_dirs, select_workspace_members


_METADATA_LOCK_HOLDER = threading.local()


def _metadata_records(data: bytes | None) -> dict:
    if data is None:
        return {}
    try:
        records = json.loads(data)
    except (UnicodeDecodeError, json.JSONDecodeError) as exc:
        raise ValueError("Plugin install metadata changed while preparing the update; retry.") from exc
    if not isinstance(records, dict):
        raise ValueError("Plugin install metadata changed while preparing the update; retry.")
    return records


def candidate_members(extra_dirs=(), *, python: Path | None = None, target_env=None, **selection):
    selected = enabled_plugin_dirs(**selection)
    enabled = {path.resolve() for path in (enabled_plugin_dirs() if selection else selected)}
    admitted = [source for source in selected if _admitted(source, new=source.resolve() not in enabled)]
    for directory in extra_dirs:
        directory = Path(directory)
        if directory not in admitted:
            admitted.append(directory)
    return select_workspace_members(admitted, python=python, target_env=target_env)


class SelectionChanged(ValueError):
    """Another writer changed a home's plugin selection while this change was being prepared: the
    caller re-reads the selection and proposes again (nothing was published)."""


def selection_projection(data: bytes | None) -> bytes | None:
    """The part of a config.yaml document the plugin selection and dependency build read: the whole
    ``plugins`` section and ``memory.provider``. Every other key (a dashboard prompt's answer, a
    model switch) leaves it unchanged, so such an edit never refuses a publication. A document that
    does not parse as a mapping projects to its bytes: any change to it counts."""
    from hermes_yaml import safe_load
    from ruamel.yaml.error import YAMLError

    if data is None:
        return None
    try:
        config = safe_load(data.decode("utf-8-sig"))
    except (UnicodeError, YAMLError):
        return data
    if config is None:
        config = {}
    if not isinstance(config, dict):
        return data
    memory = config.get("memory")
    provider = memory.get("provider") if isinstance(memory, dict) else memory
    return json.dumps({"plugins": config.get("plugins"), "memory.provider": provider},
                      sort_keys=True, default=repr).encode()


def selection_snapshot() -> dict[Path, bytes | None]:
    """Every dependency home's plugin selection: a change to anything else in a config.yaml is not a
    change to what a publication decided from."""
    from pm.plugins_state import dependency_homes
    return {home / "config.yaml": selection_projection(read_bytes_or_none(home / "config.yaml"))  # config-reader: ok — pm publication: byte-level compare-and-swap rewrite of the local file
            for home in dependency_homes()}


def validate_manifest(source: Path) -> dict:
    from pm.plugin_declarations import read_python_declaration, manifest_version_error
    from pm.workspace import core_requirement_error

    declaration = read_python_declaration(source)
    reason = manifest_version_error(declaration.manifest, source.name)
    if not reason and (core := core_requirement_error(declaration)):
        reason = f"Plugin '{source.name}' {core}"
    if reason:
        raise ValueError(reason)
    return declaration.manifest


def _admitted(source: Path, *, new: bool) -> bool:
    """Refuse a change that newly enables or stages a plugin which does not fit; one enabled
    before it whose ``hermes-agent`` requirement misses this Hermes sits out (False), as the
    build leaves it out, rather than refusing every other plugin's change."""
    from pm.plugin_declarations import read_python_declaration, manifest_version_error
    from pm.workspace import core_requirement_error

    if new:
        validate_manifest(source)
        return True
    declaration = read_python_declaration(source)
    if reason := manifest_version_error(declaration.manifest, source.name):
        raise ValueError(reason)
    return not core_requirement_error(declaration)


class PluginSelection:
    def __init__(self, selection: dict):
        self.configs = selection_snapshot()
        self.selection = selection
        self.home = Path(selection["home"]).resolve()
        if not self.home.is_relative_to(dependency_home_root().resolve()):
            raise ValueError("config path is outside Hermes state")
        self.path = self.home / "config.yaml"
        self.previous = read_bytes_or_none(self.path)  # config-reader: ok — pm publication: byte-level compare-and-swap rewrite of the local file
        expected = selection.get("expected_config")
        actual = hashlib.sha256(self.previous).hexdigest() if self.previous is not None else "missing"
        if expected is not None and expected != actual:
            raise SelectionChanged("Plugin configuration changed since this selection was read; retry.")
        self.proposed = self._render(self.previous)
        self.members = candidate_members(selection.get("extra_dirs", ()), proposed_home=self.home,
                                         enabled=selection["enabled"], disabled=selection["disabled"])

    def _render(self, previous: bytes | None) -> bytes:
        """*previous* with this selection written into its ``plugins`` section, every other key as it is."""
        from hermes_yaml import roundtrip_yaml
        from pm.plugin_eviction import settle_record

        yaml = roundtrip_yaml()
        config = yaml.load(previous.decode("utf-8-sig")) if previous else {}
        if config is None:
            config = {}
        if not isinstance(config, dict):
            raise ValueError(f"configuration must be a mapping: {self.path}")
        plugins = config.setdefault("plugins", {})
        if not isinstance(plugins, dict):
            raise ValueError(f"plugins must be a mapping in {self.path}")
        plugins["enabled"] = sorted(self.selection["enabled"])
        plugins["disabled"] = sorted(self.selection["disabled"])
        settle_record(plugins, self.selection.get("settled", ()))
        output = io.StringIO()
        yaml.dump(config, output)
        return output.getvalue().encode("utf-8")

    def publish(self, project: Path) -> None:
        if selection_snapshot() != self.configs:
            raise SelectionChanged("plugin configuration changed while preparing publication; retry")
        current = read_bytes_or_none(self.path)  # config-reader: ok — pm publication: byte-level compare-and-swap rewrite of the local file
        if current != self.previous:
            # Only keys outside the selection moved meanwhile: keep them, and journal them as the
            # bytes a crash restores.
            self.previous, self.proposed = current, self._render(current)
        row = {"config": str(self.path),
               "previous": base64.b64encode(self.previous).decode() if self.previous is not None else None,
               "facts_before": file_digest(runtime_facts_path(project)),
               "config_after": hashlib.sha256(self.proposed).hexdigest()}
        durable_write_bytes(install_state_dir(project) / "publication.json", json.dumps(row).encode())
        durable_write_bytes(self.path, self.proposed)


def _warn_acl_not_inherited(target: Path) -> None:
    """Best effort stays best effort, but not silent: the plugin keeps the staging folder's DACL,
    which a non-elevated Hermes may be unable to read. The worker's stderr reaches the user."""
    import sys

    from pm import receipt

    message = (f"Could not give {target} the Windows permissions of {target.parent}; a Hermes running "
               f"without elevation may be unable to read this plugin. To repair, from an elevated prompt "
               f'run: icacls "{target}" /reset /t')
    print(f"⚠ {message}", file=sys.stderr, flush=True)
    receipt.record_warning(message)


class StagedPlugin:
    def __init__(self, plugin: dict):
        from pm.store import tree_digest
        from pm.workspace import enabled_plugin_dirs, member_sources

        self.configs = selection_snapshot()
        self.target = Path(plugin["target"]).absolute()
        self.staged = Path(plugin["staged"]).resolve()
        if (not self.target.resolve().is_relative_to(dependency_home_root().resolve())
                or self.target.parent.name != "plugins" or self.target.is_symlink()
                or self.staged == self.target.resolve() or self.staged.is_relative_to(self.target.resolve())
                or self.target.resolve().is_relative_to(self.staged)):
            raise ValueError("plugin publication paths escape or overlap their home")
        manifest = validate_manifest(self.staged)
        if manifest.get("name", self.target.name) != self.target.name:
            raise ValueError("The updated plugin changed its installed name; reinstall it explicitly.")
        self.staged_digest = tree_digest(self.staged)
        self.metadata = self.target.parent / ".install-metadata.json"
        previous = read_bytes_or_none(self.metadata)
        current = _metadata_records(previous)
        self.old_record = plugin["old_metadata"].get(self.target.name)
        if current.get(self.target.name) != self.old_record:
            raise ValueError("Plugin install metadata changed while preparing the update; retry.")
        self.new_record = plugin["new_metadata"].get(self.target.name)
        if self.new_record is None:
            raise ValueError("Plugin publication omitted its install metadata record.")
        self.target_digest = tree_digest(self.target) if self.target.exists() else None
        if self.target_digest != plugin["target_digest"]:
            raise ValueError("Plugin files changed while preparing the update; retry.")
        sources = member_sources(enabled_plugin_dirs(installing=self.target))
        self.active = self.target.resolve() in sources
        self.members = {}
        if self.active:
            sources[self.target.resolve()] = self.staged
            admitted = {identity: source for identity, source in sources.items()
                        if _admitted(source, new=source == self.staged)}
            kept = set(select_workspace_members(admitted.values()))
            self.members = {identity: source for identity, source in admitted.items() if source in kept}

    def publish(self, project: Path) -> None:
        import os
        import uuid
        from hermes_cli.auth import _file_lock
        from pm.store import tree_digest

        if selection_snapshot() != self.configs:
            raise SelectionChanged("Plugin enablement changed while preparing the update; retry.")
        if tree_digest(self.staged) != self.staged_digest:
            raise ValueError("Staged plugin files changed while preparing the update; retry.")
        lock = self.metadata.with_name(f"{self.metadata.name}.lock")
        with _file_lock(lock, _METADATA_LOCK_HOLDER, 10.0,
                        "Timed out waiting for the plugin install metadata lock"):
            previous = read_bytes_or_none(self.metadata)
            metadata = _metadata_records(previous)
            if metadata.get(self.target.name) != self.old_record:
                raise ValueError("Plugin install metadata changed while preparing the update; retry.")
            metadata[self.target.name] = self.new_record
            proposed = (json.dumps(metadata, indent=2, sort_keys=True) + "\n").encode()
            current = tree_digest(self.target) if self.target.exists() else None
            if current != self.target_digest:
                raise ValueError("Plugin files changed while preparing the update; retry.")
            backup = self.target.parent / f".previous-{uuid.uuid4().hex}"
            row = {
                "kind": "plugin", "target": str(self.target), "backup": str(backup), "metadata": str(self.metadata),
                "target_existed": self.target.exists(), "facts_before": file_digest(runtime_facts_path(project)),
                "metadata_before": base64.b64encode(previous).decode() if previous is not None else None,
                "metadata_after": base64.b64encode(proposed).decode(),
            }
            durable_write_bytes(install_state_dir(project) / "publication.json", json.dumps(row).encode())
            if self.target.exists():
                os.replace(self.target, backup)
            os.replace(self.staged, self.target)
            # The staging folder's private Windows DACL must not travel with the plugin (#111804 is the
            # unreadable-at-install sibling); the plugin takes plugins/'s entries like any new file.
            if not inherit_parent_acl(self.target) and os.name == "nt":
                _warn_acl_not_inherited(self.target)
            durable_write_bytes(self.metadata, proposed)
