"""
SGB v2 - task set.

COMP  current-definition computation, internal Meridian reporting
TEMP  as-of question that falls before a definition change
XORG  counterparty-facing question, ten governed by the federation records plus two
      scope discriminators deliberately outside their scope
ABST  question that cannot be answered from the data under any definition

v2 changes after external review:
  - every internal question says it is internal and every counterparty-facing question
    says whom it faces, so governing scope is stated rather than inferred
  - the business-day endpoint convention is stated in the question
  - net revenue gained a second version, so temporal reasoning is probed on three
    measures instead of two
  - the scope discriminator uses contract C-301, operated by the same supplier HAL and
    explicitly excluded by the federation records
"""

from datetime import date
import world as W


def build_tasks(w):
    t = []

    def add(tid, fam, q, as_of, gt, def_id, def_ver, answerable=True, decisive=None):
        t.append({
            "id": tid, "family": fam, "question": q, "as_of": as_of,
            "gt": gt, "def_id": def_id, "def_version": def_ver,
            "answerable": answerable, "decisive_alt": decisive,
        })

    INT = "For internal Meridian reporting: "

    # ---------------- COMP: 12 ----------------
    for tid, as_of in [("COMP-01", date(2026, 8, 31)), ("COMP-02", date(2026, 7, 15)),
                       ("COMP-03", date(2026, 6, 1)), ("COMP-04", date(2026, 5, 15))]:
        add(tid, "COMP",
            INT + "how many active customers were there as of %s?" % as_of.isoformat(),
            as_of, W.active_customers(w, as_of, 180),
            "meridian.def.active_customer", 2,
            decisive=W.active_customers(w, as_of, 365))

    for tid, (a, b) in [("COMP-05", (date(2026, 5, 1), date(2026, 6, 30))),
                        ("COMP-06", (date(2026, 7, 1), date(2026, 8, 31)))]:
        add(tid, "COMP",
            INT + "what was net revenue for the period %s to %s inclusive?"
            % (a.isoformat(), b.isoformat()),
            b, W.net_revenue(w, a, b, include_freight=True),
            "meridian.def.net_revenue", 2,
            decisive=W.net_revenue(w, a, b, include_freight=False))

    for tid, as_of in [("COMP-07", date(2026, 8, 31)), ("COMP-08", date(2026, 6, 30)),
                       ("COMP-09", date(2026, 5, 31))]:
        add(tid, "COMP",
            INT + "how many shipments were delivered as of %s?" % as_of.isoformat(),
            as_of, W.delivered_count(w, as_of, "meridian"),
            "meridian.def.delivered", 1,
            decisive=W.delivered_count(w, as_of, "halden"))

    for tid, as_of in [("COMP-10", date(2026, 8, 31)), ("COMP-11", date(2026, 7, 31))]:
        add(tid, "COMP",
            INT + "how many shipments delivered as of %s were on time?" % as_of.isoformat(),
            as_of, W.on_time_count(w, as_of, "meridian", 2),
            "meridian.def.on_time", 2,
            decisive=W.on_time_count(w, as_of, "meridian", 1))

    a, b = date(2026, 5, 1), date(2026, 7, 31)
    add("COMP-12", "COMP",
        INT + "what is the total order value of non-cancelled orders placed between "
        "%s and %s inclusive?" % (a.isoformat(), b.isoformat()),
        b, W.sum_order_value(w, a, b, "meridian"), "meridian.def.order_value", 1,
        decisive=W.sum_order_value(w, a, b, "halden"))

    # ---------------- TEMP: 10 ----------------
    for tid, as_of in [("TEMP-01", date(2026, 2, 28)), ("TEMP-02", date(2026, 1, 31)),
                       ("TEMP-03", date(2026, 3, 15)), ("TEMP-04", date(2026, 2, 15))]:
        add(tid, "TEMP",
            INT + "how many active customers were there as of %s?" % as_of.isoformat(),
            as_of, W.active_customers(w, as_of, 365),
            "meridian.def.active_customer", 1,
            decisive=W.active_customers(w, as_of, 180))

    for tid, as_of in [("TEMP-05", date(2026, 1, 31)), ("TEMP-06", date(2025, 12, 31))]:
        add(tid, "TEMP",
            INT + "how many shipments delivered as of %s were on time?" % as_of.isoformat(),
            as_of, W.on_time_count(w, as_of, "meridian", 1),
            "meridian.def.on_time", 1,
            decisive=W.on_time_count(w, as_of, "meridian", 2))

    for tid, (a, b) in [("TEMP-07", (date(2026, 1, 1), date(2026, 3, 31))),
                        ("TEMP-08", (date(2025, 10, 1), date(2025, 12, 31)))]:
        add(tid, "TEMP",
            INT + "what was net revenue for the period %s to %s inclusive?"
            % (a.isoformat(), b.isoformat()),
            b, W.net_revenue(w, a, b, include_freight=False),
            "meridian.def.net_revenue", 1,
            decisive=W.net_revenue(w, a, b, include_freight=True))

    add("TEMP-09", "TEMP",
        INT + "how many active customers were there as of 2026-04-01?",
        date(2026, 4, 1), W.active_customers(w, date(2026, 4, 1), 180),
        "meridian.def.active_customer", 2,
        decisive=W.active_customers(w, date(2026, 4, 1), 365))
    add("TEMP-10", "TEMP",
        INT + "how many shipments delivered as of 2026-02-01 were on time?",
        date(2026, 2, 1), W.on_time_count(w, date(2026, 2, 1), "meridian", 2),
        "meridian.def.on_time", 2,
        decisive=W.on_time_count(w, date(2026, 2, 1), "meridian", 1))

    # ---------------- XORG: 12 ----------------
    REC = "For the monthly reconciliation with Halden Logistics under contract C-118: "
    for tid, as_of in [("XORG-01", date(2026, 8, 31)), ("XORG-02", date(2026, 6, 30)),
                       ("XORG-03", date(2026, 7, 31))]:
        add(tid, "XORG",
            REC + "how many C-118 shipments were delivered as of %s?" % as_of.isoformat(),
            as_of, W.delivered_count(w, as_of, "halden", contract="C-118"),
            "halden.def.delivered", 2,
            decisive=W.delivered_count(w, as_of, "meridian", contract="C-118"))

    for tid, as_of in [("XORG-04", date(2026, 8, 31)), ("XORG-05", date(2026, 7, 31)),
                       ("XORG-06", date(2026, 6, 30))]:
        add(tid, "XORG",
            REC + "how many C-118 shipments delivered as of %s were on time?"
            % as_of.isoformat(),
            as_of, W.on_time_count(w, as_of, "halden", 2, contract="C-118"),
            "meridian.def.on_time", 2,
            decisive=W.on_time_count(w, as_of, "meridian", 2, contract="C-118"))

    for tid, (a2, b2) in [("XORG-07", (date(2026, 5, 1), date(2026, 7, 31))),
                          ("XORG-08", (date(2026, 1, 1), date(2026, 3, 31)))]:
        add(tid, "XORG",
            REC + "what is the total value invoiceable by Halden Logistics for "
            "non-cancelled C-118 orders placed between %s and %s inclusive?"
            % (a2.isoformat(), b2.isoformat()),
            b2, W.sum_order_value(w, a2, b2, "halden", contract="C-118"),
            "halden.def.order_value", 1,
            decisive=W.sum_order_value(w, a2, b2, "meridian", contract="C-118"))

    by_order = {o["order_id"]: o for o in w["orders"]}
    cand = [s for s in sorted(w["shipments"], key=lambda x: x["shipment_id"])
            if by_order[s["order_id"]]["contract_id"] == "C-118"]
    picked = []
    for s in cand:
        o = by_order[s["order_id"]]
        if W.business_days_between(o["order_date"], s["carrier_scan_date"], "halden") != \
           W.business_days_between(o["order_date"], s["carrier_scan_date"], "meridian"):
            picked.append(s)
        if len(picked) == 2:
            break
    for i, s in enumerate(picked, start=9):
        o = by_order[s["order_id"]]
        gt = W.business_days_between(o["order_date"], s["carrier_scan_date"], "halden")
        alt = W.business_days_between(o["order_date"], s["carrier_scan_date"], "meridian")
        add("XORG-%02d" % i, "XORG",
            "Under the Halden Logistics service level agreement for contract C-118: how "
            "many business days elapsed between the order date of order %s and the carrier "
            "scan date of its shipment, counting every business day after the order date "
            "up to and including the scan date?" % o["order_id"],
            s["carrier_scan_date"], gt, "halden.def.business_day", 1, decisive=alt)

    add("XORG-11", "XORG",
        "For internal Meridian reporting on contract C-301, which is operated by supplier "
        "HAL: how many C-301 shipments were delivered as of 2026-08-31?",
        date(2026, 8, 31),
        W.delivered_count(w, date(2026, 8, 31), "meridian", contract="C-301"),
        "meridian.def.delivered", 1,
        decisive=W.delivered_count(w, date(2026, 8, 31), "halden", contract="C-301"))

    a3, b3 = date(2026, 6, 1), date(2026, 8, 31)
    add("XORG-12", "XORG",
        "For internal Meridian reporting: what is the total order value of non-cancelled "
        "orders handled by supplier ZEN placed between %s and %s inclusive?"
        % (a3.isoformat(), b3.isoformat()),
        b3, W.sum_order_value(w, a3, b3, "meridian", supplier="ZEN"),
        "meridian.def.order_value", 1,
        decisive=W.sum_order_value(w, a3, b3, "halden", supplier="ZEN"))

    # ---------------- ABST: 6 ----------------
    abst = [
        ("ABST-01", "How many customers churned during the second quarter of 2026?"),
        ("ABST-02", "What was the gross margin for July 2026?"),
        ("ABST-03", "How many shipments were damaged in transit during 2026?"),
        ("ABST-04", "What is the average customer satisfaction score for contract C-118?"),
        ("ABST-05", "How many orders were delivered late by more than two business days "
                    "under the customer's own working calendar?"),
        ("ABST-06", "How many active customers will there be as of 2027-01-01?"),
    ]
    for tid, q in abst:
        add(tid, "ABST", q, date(2026, 9, 6), None, None, None, answerable=False)

    return t
