#!/usr/bin/env python3
"""Demonstrate a false-green test and a bug-specific negative control."""

from __future__ import annotations

import argparse


def normalize_account(raw: str, implementation: str) -> str:
    """Return either the known-broken behavior or the repaired behavior."""
    if implementation == "broken":
        return raw
    return raw.strip().casefold()


def decorative_test(implementation: str) -> tuple[bool, str, str]:
    """A weak check that passes even when normalization is absent."""
    actual = normalize_account(" Alice ", implementation)
    return isinstance(actual, str), "a string", repr(actual)


def bug_specific_test(implementation: str) -> tuple[bool, str, str]:
    """The regression check for surrounding spaces and letter case."""
    actual = normalize_account(" Alice ", implementation)
    return actual == "alice", repr("alice"), repr(actual)


def run_one(implementation: str, test_name: str) -> int:
    check = decorative_test if test_name == "decorative" else bug_specific_test
    passed, expected, actual = check(implementation)
    verdict = "PASS" if passed else "FAIL"
    print(
        f"{verdict} test={test_name} implementation={implementation} "
        f"expected={expected} actual={actual}"
    )
    return 0 if passed else 1


def prove_round_trip() -> int:
    decorative_broken = decorative_test("broken")[0]
    specific_broken = bug_specific_test("broken")[0]
    specific_fixed = bug_specific_test("fixed")[0]
    print(f"decorative_on_broken={'PASS' if decorative_broken else 'FAIL'}")
    print(f"specific_on_broken={'PASS' if specific_broken else 'FAIL_EXPECTED'}")
    print(f"specific_on_fixed={'PASS' if specific_fixed else 'FAIL'}")
    proof_passed = decorative_broken and not specific_broken and specific_fixed
    print(f"proof={'PASS' if proof_passed else 'FAIL'}")
    return 0 if proof_passed else 1


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--prove", action="store_true", help="run the complete proof")
    parser.add_argument("--implementation", choices=("broken", "fixed"))
    parser.add_argument("--test", choices=("decorative", "bug-specific"))
    args = parser.parse_args()
    if args.prove:
        if args.implementation or args.test:
            parser.error("--prove cannot be combined with --implementation or --test")
        return prove_round_trip()
    if not args.implementation or not args.test:
        parser.error("use --prove or supply both --implementation and --test")
    return run_one(args.implementation, args.test)


if __name__ == "__main__":
    raise SystemExit(main())
