"""did the registration form get better?

Generated from src/content/docs/reference/compare.md.
Run with the documented Fieldwork release in a writable directory.
Exports are saved there using the filenames shown in the walkthrough.
"""

import pandas as pd
import fieldwork as fw

columns = ["registration", "channel", "email", "phone"]
before_df = pd.DataFrame(
    [
        ("B01", "online", "b01@example.test", None),
        ("B02", "online", "b02@example.test", None),
        ("B03", "walk-in", None, "555-0103"),
        ("B04", "walk-in", None, "555-0104"),
        ("B05", "walk-in", None, "555-0105"),
        ("B06", "walk-in", None, "555-0106"),
        ("B07", "walk-in", None, "555-0107"),
        ("B08", "walk-in", None, "555-0108"),
    ],
    columns=columns,
)
after_df = pd.DataFrame(
    [
        ("A01", "online", "a01@example.test", None),
        ("A02", "online", "a02@example.test", None),
        ("A03", "online", "a03@example.test", None),
        ("A04", "online", "a04@example.test", None),
        ("A05", "online", "a05@example.test", None),
        ("A06", "online", "a06@example.test", None),
        ("A07", "walk-in", None, "555-0207"),
        ("A08", "walk-in", None, "555-0208"),
    ],
    columns=columns,
)
after_df["sms_opt_in"] = [
    False,
    False,
    False,
    False,
    False,
    False,
    True,
    False,
]

before = fw.missingness(
    before_df,
    features=["email", "phone"],
    table_id="registrations-before",
)
after = fw.missingness(
    after_df,
    features=["email", "phone", "sms_opt_in"],
    table_id="registrations-after",
)
change = fw.compare(before, after)
changes = change.to_frame("changes")
print(
    changes[["feature", "populated_fraction_delta"]].to_string(
        index=False
    )
)

report = fw.render_plaintext(change)
print("\n".join(report.splitlines()[:5]))

before_positions = []
for position, channel in enumerate(before_df["channel"]):
    if channel == "online":
        before_positions.append(position)

after_positions = []
for position, channel in enumerate(after_df["channel"]):
    if channel == "online":
        after_positions.append(position)

before_scope = fw.Scope.from_positions(
    before_df, before_positions, name="Online before"
)
after_scope = fw.Scope.from_positions(
    after_df, after_positions, name="Online after"
)

before_online = fw.missingness(
    before_df, features=["email", "phone"], scope=before_scope
)
after_online = fw.missingness(
    after_df, features=["email", "phone"], scope=after_scope
)
online_change = fw.compare(before_online, after_online)
online_changes = online_change.to_frame("changes")
print(
    online_changes[["feature", "populated_fraction_delta"]].to_string(
        index=False
    )
)

after_findings = after.to_frame("findings")
print(after_findings[["id", "statement"]].to_string(index=False))

missing_email = after.inspect(
    after_df, "f0", exceptions=True, all_matches=True
)
print(
    missing_email[["registration", "channel", "phone"]].to_string(
        index=False
    )
)
