"""Reproduce the published pricing-disclosure counts from frozen inputs. Offline, standard library only."""

import argparse
import csv
import hashlib
import io
import json
from collections import Counter, defaultdict
from pathlib import Path

BASE = Path(__file__).resolve().parent
REPO_SOURCE = BASE.joinpath("../../../resources/ai-receptionist-cost-australia/data").resolve()
DEFAULT_SOURCE = REPO_SOURCE if REPO_SOURCE.is_dir() else BASE / "cost-data"
SNAPSHOT = "2026-11-17"
PROVIDERS = {
    "valory": "Valory AI",
    "johnni": "Johnni AI",
    "nexwin": "Nexwin",
    "smith": "Smith.ai",
    "ringcentral": "RingCentral",
    "botbloke": "BotBloke",
    "transfertoai": "TransferToAI",
}
PLANS_BY_PROVIDER = {
    "valory": ("valory-intro", "valory-core", "valory-premium", "valory-enterprise"),
    "johnni": ("johnni-monthly", "johnni-annual"),
    "nexwin": ("nexwin-business",),
    "smith": ("smith-free", "smith-pro"),
    "ringcentral": ("ringcentral-air",),
    "botbloke": ("botbloke-quote",),
    "transfertoai": ("transfertoai-quote",),
}
PROVIDER_UNIT = "PROVIDER_SOURCE_PACKAGE"
PLAN_UNIT = "PLAN_PAYMENT_ROW"
PROVIDER_FIELDS = (
    "source_access",
    "currency",
    "gst_basis",
    "setup_basis",
    "sms_charge_basis",
    "transfer_wait_charge_basis",
    "voice_rounding_increment",
    "voice_minimum_duration",
    "additional_scope_or_base_price",
)
TIME_FIELDS = {"voice_rounding_increment", "voice_minimum_duration"}
PLAN_FIELDS = {
    "headline_amount": "subscription",
    "voice_billing_unit": "voice_unit",
    "included_voice_allowance": "included_voice",
    "extra_voice_unit_rate": "voice_overage",
    "payment_basis": "billing_frequency",
}
STATUSES = ("ESTABLISHED", "UNKNOWN", "UNAVAILABLE", "QUOTE_REQUIRED", "NOT_APPLICABLE")
SOURCE_FILES = {"pricing-source-register.csv", "pricing-inputs.csv", "method.md", "manifest.json"}
PACKAGE_INPUTS = (
    "method.md", "frozen-inputs.json", "fact-coded.csv", "quote-questions.csv",
    "analyse.py", "test_analyse.py",
)
FACT_COLUMNS = (
    "assessment_id", "assessment_unit", "provider_id", "provider", "plan_row_id",
    "field_id", "eligibility", "status", "coded_value", "source_ids", "reason",
    "source_snapshot_date",
)


def require(condition, message):
    if not condition:
        raise ValueError(message)


def sha256(data):
    return hashlib.sha256(data).hexdigest()


def csv_rows(path):
    with path.open(encoding="utf-8", newline="") as file:
        reader = csv.DictReader(file)
        rows = list(reader)
        require(reader.fieldnames and len(reader.fieldnames) == len(set(reader.fieldnames)),
                f"Missing or duplicate column in {path.name}")
        require(all(None not in row and None not in row.values() for row in rows),
                f"Malformed CSV row in {path.name}")
        if path.name == "fact-coded.csv":
            require(tuple(reader.fieldnames) == FACT_COLUMNS, "Unexpected fact-coded schema")
        return rows


def json_bytes(data):
    return (json.dumps(data, indent=2, ensure_ascii=False) + "\n").encode("utf-8")


def csv_bytes(rows):
    file = io.StringIO(newline="")
    writer = csv.DictWriter(file, list(rows[0]), lineterminator="\n")
    writer.writeheader()
    writer.writerows(rows)
    return file.getvalue().encode("utf-8")


def load_frozen_sources(source_dir, report_dir):
    frozen = json.loads((report_dir / "frozen-inputs.json").read_text(encoding="utf-8"))
    hashes = frozen["inputs"]
    require(frozen["source_snapshot_date"] == SNAPSHOT and set(hashes) == SOURCE_FILES,
            "Unexpected frozen input inventory or date")
    for name, expected in hashes.items():
        require(sha256((source_dir / name).read_bytes()) == expected, f"Frozen input changed: {name}")

    original = json.loads((source_dir / "manifest.json").read_text(encoding="utf-8"))
    require(original["source_review_date"] == SNAPSHOT and original["hash_algorithm"] == "SHA-256",
            "Unexpected original manifest date or hash algorithm")
    for name in SOURCE_FILES - {"manifest.json"}:
        require(original["files"][name] == hashes[name], f"Original and frozen manifests differ: {name}")

    return csv_rows(source_dir / "pricing-source-register.csv"), csv_rows(source_dir / "pricing-inputs.csv")


def index_unique(rows, column):
    indexed = {row[column]: row for row in rows}
    require(len(rows) == len(indexed) and "" not in indexed, f"Duplicate or missing {column}")
    return indexed


def validate(rows, source_rows, input_rows):
    sources = index_unique(source_rows, "source_id")
    plans = index_unique(input_rows, "plan_id")
    facts = index_unique(rows, "assessment_id")

    expected = {}
    plan_owners = {}
    for provider_id, plan_ids in PLANS_BY_PROVIDER.items():
        for field in PROVIDER_FIELDS:
            expected[f"provider_source_package:{provider_id}:{field}"] = (PROVIDER_UNIT, provider_id, "", field)
        for plan_id in plan_ids:
            plan_owners[plan_id] = provider_id
            for field in PLAN_FIELDS:
                expected[f"plan_payment_row:{plan_id}:{field}"] = (PLAN_UNIT, provider_id, plan_id, field)

    require(set(plans) == set(plan_owners), "Unexpected worksheet row inventory")
    require(set(facts) == set(expected), "Missing or unexpected assessment ID")
    require({row["provider"] for row in source_rows} == set(PROVIDERS.values()), "Unexpected source roster")
    sources_by_provider = defaultdict(set)
    readable = set()
    for source_id, source in sources.items():
        require(source["retrieved_at_utc"] == SNAPSHOT and source["review_date"] == SNAPSHOT,
                f"Unexpected date for {source_id}")
        require(source["source_url"].startswith("https://") and source["source_locator"] and source["published_value"],
                f"Missing provenance for {source_id}")
        require(source["evidence_status"] in {"VERIFIED", "UNKNOWN", "QUOTE_REQUIRED", "UNAVAILABLE"},
                f"Unexpected register status for {source_id}")
        sources_by_provider[source["provider"]].add(source_id)
        if source["evidence_status"] == "VERIFIED":
            readable.add(source["provider"])

    evidence_by_provider = defaultdict(set)
    for plan_id, plan in plans.items():
        provider = PROVIDERS[plan_owners[plan_id]]
        plan_sources = set(plan["source_ids"].split(";"))
        require(plan["provider"] == provider and plan_sources <= sources_by_provider[provider],
                f"Wrong worksheet source or provider for {plan_id}")
        if provider in readable:
            require(any(sources[link]["evidence_status"] == "VERIFIED" for link in plan_sources),
                    f"Missing readable plan source for {plan_id}")
            voice = facts[f"plan_payment_row:{plan_id}:voice_billing_unit"]
            if voice["status"] == "ESTABLISHED":
                require(voice["coded_value"] in {"CALL", "MINUTE"}, f"Unexpected voice unit for {plan_id}")
                evidence_by_provider[provider].add(voice["coded_value"])
        else:
            require(all(sources[link]["evidence_status"] == "UNAVAILABLE" for link in plan_sources),
                    f"Unverified worksheet placeholder: {plan_id}")

    for assessment_id, (unit, provider_id, plan_id, field) in expected.items():
        row = facts[assessment_id]
        provider = PROVIDERS[provider_id]
        require((row["assessment_unit"], row["provider_id"], row["provider"], row["plan_row_id"],
                 row["field_id"], row["source_snapshot_date"]) ==
                (unit, provider_id, provider, plan_id, field, SNAPSHOT),
                f"Assessment metadata disagrees with ID: {assessment_id}")
        status = row["status"]
        value = row["coded_value"]
        links = row["source_ids"].split(";")
        require(row["reason"].strip() and len(links) == len(set(links))
                and set(links) <= sources_by_provider[provider],
                f"Missing, duplicate or wrong-provider source link: {assessment_id}")
        linked_statuses = {sources[link]["evidence_status"] for link in links}
        require(status in STATUSES, f"Unexpected code: {assessment_id}")

        if field == "source_access":
            eligibility = "ALL_SELECTED"
            allowed = {"ESTABLISHED"} if provider in readable else {"UNAVAILABLE"}
            require(set(links) == sources_by_provider[provider], f"Incomplete source access trail: {assessment_id}")
            expected_value = "READABLE_RETAINED_PRICING_EVIDENCE" if provider in readable else "PUBLIC_ACCESS_FAILED"
            require(value == expected_value, f"Wrong source access value: {assessment_id}")
        elif provider not in readable:
            eligibility, allowed = "SOURCE_UNAVAILABLE", {"UNAVAILABLE"}
        elif unit == PLAN_UNIT:
            eligibility, allowed = "READABLE_SELECTED_PLAN", {"ESTABLISHED", "UNKNOWN"}
            require(set(links) <= set(plans[plan_id]["source_ids"].split(";")),
                    f"Source link is outside the worksheet plan: {assessment_id}")
            if status == "ESTABLISHED":
                require(value == plans[plan_id][PLAN_FIELDS[field]], f"Value disagrees with worksheet: {assessment_id}")
        elif field in TIME_FIELDS:
            voice_units = evidence_by_provider[provider]
            require(voice_units, f"No evidenced billing unit to determine eligibility: {provider}")
            if "MINUTE" in voice_units:
                eligibility, allowed = "READABLE_MINUTE_BASED_SOURCE", {"ESTABLISHED", "UNKNOWN"}
            else:
                eligibility, allowed = "NOT_APPLICABLE_SELECTED_VOICE_UNIT", {"NOT_APPLICABLE"}
                require(value == "SELECTED_PLANS_BILL_PER_CALL", f"Wrong time exclusion basis: {assessment_id}")
        else:
            eligibility = "READABLE_SOURCE"
            allowed = {"QUOTE_REQUIRED", "UNKNOWN"} if field == "additional_scope_or_base_price" else {"ESTABLISHED", "UNKNOWN"}

        require(row["eligibility"] == eligibility and status in allowed,
                f"Wrong eligibility or code: {assessment_id}")
        if status in {"ESTABLISHED", "NOT_APPLICABLE", "QUOTE_REQUIRED"}:
            accepted = {"VERIFIED", "QUOTE_REQUIRED"} if status == "QUOTE_REQUIRED" else {"VERIFIED"}
            require(value and linked_statuses & accepted and "UNAVAILABLE" not in linked_statuses,
                    f"No accepted evidence for a positive classification: {assessment_id}")
        elif status == "UNAVAILABLE":
            require(linked_statuses == {"UNAVAILABLE"} and (field == "source_access" or not value),
                    f"Non-access evidence or value on unavailable row: {assessment_id}")
        else:
            require(not value and "UNAVAILABLE" not in linked_statuses, f"Unexpected unknown value: {assessment_id}")

    return plans, facts


def analyse(rows, sources, input_rows):
    plans, facts = validate(rows, sources, input_rows)
    counts = []
    for unit, fields in ((PROVIDER_UNIT, PROVIDER_FIELDS), (PLAN_UNIT, PLAN_FIELDS)):
        for field in fields:
            matching = [row for row in rows if row["assessment_unit"] == unit and row["field_id"] == field]
            codes = Counter(row["status"] for row in matching)
            unavailable_excluded = sum(row["eligibility"] == "SOURCE_UNAVAILABLE" for row in matching)
            applicable = len(matching) - unavailable_excluded - codes["NOT_APPLICABLE"]
            unavailable_in_denominator = codes["UNAVAILABLE"] - unavailable_excluded
            counted = codes["ESTABLISHED"] + codes["UNKNOWN"] + codes["QUOTE_REQUIRED"]
            require((field == "source_access" or unavailable_in_denominator == 0)
                    and applicable == counted + unavailable_in_denominator,
                    f"Count reconciliation failed: {field}")
            counts.append({
                "assessment_unit": unit,
                "field_id": field,
                "selected_units": len(matching),
                "eligible_denominator": applicable,
                **{status.lower(): codes[status] for status in STATUSES},
                "excluded_source_unavailable": unavailable_excluded,
                "excluded_not_applicable": codes["NOT_APPLICABLE"],
            })

    plan_index = []
    for provider_id, plan_ids in PLANS_BY_PROVIDER.items():
        for plan_id in plan_ids:
            assessments = {field: facts[f"plan_payment_row:{plan_id}:{field}"] for field in PLAN_FIELDS}
            voice = assessments["voice_billing_unit"]
            payment = assessments["payment_basis"]
            plan_index.append({
                "provider_id": provider_id,
                "provider": PROVIDERS[provider_id],
                "plan_row_id": plan_id,
                "worksheet_label": plans[plan_id]["plan"],
                "row_type": "UNAVAILABLE_PLACEHOLDER" if voice["status"] == "UNAVAILABLE" else "EVIDENCED_PLAN_PAYMENT_ROW",
                "voice_billing_unit": voice["coded_value"] or voice["status"],
                "payment_basis": payment["coded_value"] or payment["status"],
                "source_ids": plans[plan_id]["source_ids"],
            })

    observed = [row for row in plan_index if row["row_type"] == "EVIDENCED_PLAN_PAYMENT_ROW"]
    summary = {
        "source_snapshot_date": SNAPSHOT,
        "selected_providers": len(PROVIDERS),
        "readable_provider_packages": counts[0]["established"],
        "unavailable_provider_packages": counts[0]["unavailable"],
        "register_records": len(sources),
        "assessment_rows": len(rows),
        "selected_worksheet_rows": len(plan_index),
        "evidenced_plan_payment_rows": len(observed),
        "unavailable_worksheet_placeholders": len(plan_index) - len(observed),
        "voice_billing_unit_among_evidenced_rows": dict(sorted(Counter(row["voice_billing_unit"] for row in observed).items())),
        "payment_basis_among_evidenced_rows": dict(sorted(Counter(row["payment_basis"] for row in observed).items())),
        "counts": counts,
    }
    return counts, plan_index, summary


def generate(report_dir, source_dir):
    sources, input_rows = load_frozen_sources(source_dir, report_dir)
    rows = csv_rows(report_dir / "fact-coded.csv")
    counts, plan_index, summary = analyse(rows, sources, input_rows)
    method_hash = sha256((report_dir / "method.md").read_bytes())
    artifacts = {
        "counts.csv": csv_bytes(counts),
        "plan-row-index.csv": csv_bytes(plan_index),
        "summary.json": json_bytes(summary),
        "method.sha256": f"{method_hash}  method.md\n".encode("ascii"),
    }
    file_hashes = {name: sha256((report_dir / name).read_bytes()) for name in PACKAGE_INPUTS}
    file_hashes.update({name: sha256(data) for name, data in artifacts.items()})
    artifacts["manifest.json"] = json_bytes({
        "package_version": "1.0",
        "source_snapshot_date": SNAPSHOT,
        "analysis_date": "2026-11-18",
        "hash_algorithm": "SHA-256",
        "files": dict(sorted(file_hashes.items())),
    })
    return artifacts


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--source-dir", type=Path, default=DEFAULT_SOURCE, help="Directory of the four original inputs")
    parser.add_argument("--check", action="store_true", help="Compare bytes without writing output")
    args = parser.parse_args()

    try:
        for name, data in generate(BASE, args.source_dir).items():
            path = BASE / name
            if args.check:
                require(path.is_file() and path.read_bytes() == data, f"Missing or out-of-date output: {name}")
            else:
                path.write_bytes(data)
    except (OSError, ValueError, KeyError) as error:
        parser.exit(1, f"{error}\n")
    print(f"{'Verified' if args.check else 'Generated'} five outputs from the frozen {SNAPSHOT} inputs.")


if __name__ == "__main__":
    main()
