"""
The Eleven Dimensional Spreadsheet: a tiny proof of concept (v1 POC).

For production use (Ed25519 signatures, encrypted key backup ritual,
SQLite WAL durability, freeze-at-posting immutability, integer minor units,
and partial+full settlement conservation), use ``ledger_v2.py``.

Canonical v2 axis numbering matches the Entry record layout (`Axis 11 = forgiveness`,
closing the book). An entry is not real until all eleven axes agree:

   1. debit_account        where the value leaves
   2. credit_account       where the value arrives
   3. amount               how much moved (> 0)
   4. posted_at            UTC timestamp when posted
   5. provenance           the earlier entry this one rests on, or GENESIS
   6. author               who posted it, as a matter of record
   7. reason               why it exists, stated at posting time
   8. consent              every party who agreed, and under what mandate
   9. definition_version   what the words meant on the day it was posted
  10. momentum             where the arrangement is heading: the projected
                           size of the next movement along this chain
                           (Ijiri's dimension)
  11. forgiveness          the entry's grace standing, closing the book:
                           "owed" (it creates an obligation), "settled"
                           (complete in itself, or discharging one), or
                           "forgiven" (one-time bilateral release without
                           settlement).

  Envelope seal (receipt + detached signatures):
    One countersigned SHA-256 record held in common so neither side keeps
    a private version that drifts (Grigg's dimension).

  LEGACY AXIS ALIAS NOTICE (Oct 24 / Oct 30 prose vs. code):
    Earlier whitepaper prose printed forgiveness as Axis 3 and amount as Axis 4
    with posted_at as an unnumbered book index. Data outlives prose, so
    canonical Axis 11 = forgiveness is enforced here and in `ledger_v2.py`;
    legacy Axis-3 prose documents must resolve loudly via `resolve_legacy_axis_doc`.
"""

import hashlib
import json
import warnings
from dataclasses import dataclass, field


CANONICAL_AXES = {
    1: "debit_account",
    2: "credit_account",
    3: "amount",
    4: "posted_at",
    5: "provenance",
    6: "author",
    7: "reason",
    8: "consent",
    9: "definition_version",
    10: "momentum",
    11: "forgiveness",
}

LEGACY_PROSE_AXIS_ALIASES = {
    "3. forgiveness": "11. forgiveness",
    "4. amount": "3. amount",
    "index. posted_at": "4. posted_at",
    "11. receipt": "envelope.receipt",
}


class LegacyAxisAliasError(Exception):
    pass


class LegacyAxisAliasWarning(UserWarning):
    pass


def resolve_legacy_axis_doc(doc, allow_migration=False):
    """Fail loudly on legacy Axis-3 forgiveness documents unless migration is opted in."""
    legacy_hits = [k for k in LEGACY_PROSE_AXIS_ALIASES if k in doc]
    if doc.get("axis_map") == "legacy_prose_axis3":
        legacy_hits.append("axis_map='legacy_prose_axis3'")
    if legacy_hits and not allow_migration:
        raise LegacyAxisAliasError(
            f"Legacy Axis-3 forgiveness mapping detected ({legacy_hits}); "
            "canonical schema uses Axis 11 = forgiveness. "
            "Pass allow_migration=True to migrate loudly."
        )
    out = dict(doc)
    if legacy_hits:
        warnings.warn(
            f"Loudly migrating legacy Axis-3 keys {legacy_hits} to canonical Axis 11 = forgiveness",
            LegacyAxisAliasWarning,
            stacklevel=2,
        )
        if "3. forgiveness" in out:
            out["forgiveness"] = out.pop("3. forgiveness")
        if "4. amount" in out:
            out["amount"] = out.pop("4. amount")
        if "index. posted_at" in out:
            out["posted_at"] = out.pop("index. posted_at")
        if "11. receipt" in out:
            out.pop("11. receipt")
        out.pop("axis_map", None)
    return out


# ---------------------------------------------------------------- registry

DEFINITIONS = {
    "v1": {
        "share": "a transfer without payment",
        "surplus": "food beyond a site's own need",
    },
    "v2": {
        "share": "a transfer without payment",
        "surplus": "food beyond a site's own need, including projected demand",
    },
}

ACCOUNTS = {  # account -> the party who must consent to movements on it
    "Harvest": "Harvest",
    "NorWest": "NorWest",
    "Siloam": "Siloam",
}


# ------------------------------------------------------------------- entry

@dataclass
class Entry:
    entry_id: str
    debit_account: str          # 1
    credit_account: str         # 2
    amount: float               # 3
    posted_at: str              # 4
    provenance: str             # 5: an earlier entry_id, or "GENESIS"
    author: str                 # 6
    reason: str                 # 7
    consent: tuple              # 8: parties who agreed
    definition_version: str     # 9
    momentum: float = None      # 10: projected size of the next movement
    forgiveness: str = "settled"  # 11: owed / settled / forgiven (closes the book)
    signatures: dict = field(default_factory=dict)  # Envelope seal, filled by sign()

    def canonical(self):
        """The eleven axes + entry_id the common receipt is computed over."""
        return json.dumps({
            "entry_id": self.entry_id,
            "debit_account": self.debit_account,
            "credit_account": self.credit_account,
            "amount": self.amount,
            "posted_at": self.posted_at,
            "provenance": self.provenance,
            "author": self.author,
            "reason": self.reason,
            "consent": list(self.consent),
            "definition_version": self.definition_version,
            "momentum": self.momentum,
            "forgiveness": self.forgiveness,
        }, sort_keys=True)

    def receipt(self):
        """Common record held by both sides (Envelope seal)."""
        return hashlib.sha256(self.canonical().encode()).hexdigest()

    def sign(self, party):
        """A party countersigns by hashing the shared receipt with its name.
        Toy signatures for a proof of concept; production `ledger_v2.py` uses
        party-held Ed25519 keys. The signature is over the COMMON record, so a
        privately altered copy on either side stops verifying."""
        self.signatures[party] = hashlib.sha256(
            (self.receipt() + "|" + party).encode()).hexdigest()
        return self


# ------------------------------------------------------------------- book

class Rejected(Exception):
    pass


class Book:
    def __init__(self):
        self.entries = {}
        self.discharged = {}  # obligation_entry_id -> discharging_entry_id

    # -- the checker: every axis, every time ------------------------------
    def check(self, e):
        if e.entry_id in self.entries:
            raise Rejected("id already posted; the book is append only, "
                           "corrections are new entries, never edits")
        if e.debit_account not in ACCOUNTS or e.credit_account not in ACCOUNTS:
            raise Rejected("unknown account")
        if e.amount <= 0:
            raise Rejected("amount must be positive; forgiveness is an "
                           "axis, not a zero")
        # 11. forgiveness: the grace standing must be one of the three, and
        # a forgiveness entry must actually release a still-open standing
        # obligation: resting on it, reversed against it, once only.
        if e.forgiveness not in ("owed", "settled", "forgiven"):
            raise Rejected(f"grace standing '{e.forgiveness}' is not "
                           "owed, settled, or forgiven")
        if e.forgiveness == "forgiven":
            if e.provenance == "GENESIS" or e.provenance not in self.entries:
                raise Rejected("forgiveness must rest on the obligation "
                               "it releases")
            o = self.entries[e.provenance]
            if o.forgiveness != "owed":
                raise Rejected("the entry it rests on is not standing as "
                               "owed; you cannot forgive what was never "
                               "an obligation")
            if o.entry_id in self.discharged:
                raise Rejected(
                    f"obligation '{o.entry_id}' already discharged by "
                    f"'{self.discharged[o.entry_id]}'; forgiveness on axis 11 "
                    "cannot be double-spent"
                )
            if (e.debit_account, e.credit_account, e.amount) != \
                    (o.credit_account, o.debit_account, o.amount):
                raise Rejected("forgiveness must reverse the obligation "
                               "exactly; anything else is a new movement "
                               "wearing grace's clothes")
        # 5. provenance: the chain must walk back and TERMINATE.
        if e.provenance == e.entry_id:
            raise Rejected("provenance is the entry itself; a chain that "
                           "rests on itself proves nothing (you cannot "
                           "define a word with itself)")
        seen, cursor = set(), e.provenance
        while cursor != "GENESIS":
            if cursor in seen:
                raise Rejected("provenance chain is cyclic; a chain that "
                               "never reaches a first entry proves nothing "
                               "(you cannot define a word with itself)")
            if cursor not in self.entries:
                raise Rejected(f"provenance '{cursor}' is not in the book; "
                               "the chain dangles")
            seen.add(cursor)
            cursor = self.entries[cursor].provenance
        # 8. consent: everyone the movement touches must have agreed.
        needed = {ACCOUNTS[e.debit_account], ACCOUNTS[e.credit_account]}
        if not needed.issubset(set(e.consent)):
            raise Rejected(f"consent missing: {sorted(needed - set(e.consent))}")
        # 9. definitions are pinned to a registered version.
        if e.definition_version not in DEFINITIONS:
            raise Rejected(f"definition version '{e.definition_version}' "
                           "is not registered; unpinned meanings are how "
                           "ledgers fill with fluent nonsense")
        # 10. momentum: a declared trajectory is checked when the next
        # movement along the chain actually arrives.
        if e.provenance != "GENESIS" and e.forgiveness != "forgiven":
            parent = self.entries[e.provenance]
            if parent.momentum is not None and parent.momentum != e.amount:
                raise Rejected(f"trajectory broken: the parent entry "
                               f"projected a next movement of "
                               f"{parent.momentum}, this entry posts "
                               f"{e.amount}")
        # Envelope seal: both parties hold the SAME record, countersigned.
        for party in needed:
            want = hashlib.sha256(
                (e.receipt() + "|" + party).encode()).hexdigest()
            if e.signatures.get(party) != want:
                raise Rejected(f"receipt does not verify for {party}; one "
                               "side's copy has drifted from the record "
                               "held in common")
        return True

    def post(self, e):
        self.check(e)
        self.entries[e.entry_id] = e
        if e.forgiveness == "forgiven":
            self.discharged[e.provenance] = e.entry_id
        return e

    # -- reading an old entry under newer meanings -------------------------
    def read(self, entry_id, reader_version):
        e = self.entries[entry_id]
        if reader_version == e.definition_version:
            return f"{entry_id}: read under {reader_version}, the meanings " \
                   f"it was posted under. Clean."
        old = DEFINITIONS[e.definition_version]
        new = DEFINITIONS[reader_version]
        drift = sorted(
            {t for t in old if old[t] != new.get(t)}
            | {t for t in new if new[t] != old.get(t)}
        )
        return (f"{entry_id}: WARNING, posted under "
                f"{e.definition_version}, being read under {reader_version}. "
                f"Drifted terms: {drift}. The arithmetic is perfect and "
                f"the reading is wrong; this is why the version is an axis.")

    # -- grace: a reversal that keeps the provenance -----------------------
    def forgive(self, original_id, author, reason, posted_at,
                definition_version):
        """Forgiveness is a possible entry: a reversal, standing 'forgiven'
        on axis 11, that releases a standing obligation once only while keeping
        the original on the record. The undo that does not lie. The checker
        enforces the shape; this helper is just the convenient pen."""
        o = self.entries[original_id]
        e = Entry(
            entry_id=original_id + "-forgiven",
            debit_account=o.credit_account,
            credit_account=o.debit_account,
            amount=o.amount,
            posted_at=posted_at,
            provenance=original_id,
            author=author,
            reason=reason,
            consent=tuple(sorted({ACCOUNTS[o.debit_account],
                                  ACCOUNTS[o.credit_account]})),
            definition_version=definition_version,
            momentum=None,
            forgiveness="forgiven",
        )
        e.sign(ACCOUNTS[o.debit_account]).sign(ACCOUNTS[o.credit_account])
        return self.post(e)
