# Load the table analysis library.
import pandas as pd
# Read week 1 with identifiers stored as text.
w1 = pd.read_csv("week_1.csv", dtype="string")
# Read week 2 with identifiers stored as text.
w2 = pd.read_csv("week_2.csv", dtype="string")
# Load the tariff lookup and preserve the zone key.
rates = pd.read_csv("tariffs.csv", dtype={"zone": "string"})
# Check that the weekly headers agree in order.
assert w1.columns.equals(w2.columns)
# Stack the two manifests into one parcel ledger.
parcels = pd.concat([w1, w2], ignore_index=True)
# Require an identifier for every parcel.
assert parcels["parcel_id"].notna().all()
# Reject parcel IDs repeated across either week.
assert parcels["parcel_id"].is_unique
# Require a zone key on both sides of the lookup.
assert parcels["zone"].notna().all() and rates["zone"].notna().all()
# Check that every listed tariff is present and nonnegative.
assert rates["fee_bdt"].notna().all() and rates["fee_bdt"].ge(0).all()
# Attach one tariff per zone and retain unmatched parcels.
ledger = parcels.merge(rates, on="zone", how="left", validate="many_to_one", indicator=True)
# Reconcile the joined row count with the intake.
assert len(ledger) == len(parcels)
# Flag parcels whose zone has a tariff.
matched = ledger["_merge"].eq("both")
# Select the rows that finance can price.
priced = ledger.loc[matched]
# Build the exception list with parcel IDs and zones.
pending = ledger.loc[~matched, ["parcel_id", "zone"]]
# Total the supported charges separately for each zone.
zone_totals = priced.groupby("zone")["fee_bdt"].sum().sort_index()
# Add the supported parcel fees in BDT.
subtotal = int(priced["fee_bdt"].sum())
# Measure the percentage of all parcels with a rate.
coverage = 100 * len(priced) / len(ledger)
# Confirm that every incoming parcel has a disposition.
assert len(priced) + len(pending) == len(parcels)
# Display the parcel reconciliation.
print(f"Parcels {len(ledger)} | Priced {len(priced)} | Pending {len(pending)}")
# Display the supported subtotal and coverage.
print(f"Supported BDT {subtotal} | Coverage {coverage:.2f}%")
# Display the priced subtotal for each zone.
print(zone_totals.to_string())
# Display the parcel-level handoff for tariff review.
print(pending.to_string(index=False))
