#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""复核指定计算题的数值。"""
from fractions import Fraction


def main():
    results = {}

    # 第5章题24：已知 TE=9、σ=2、M=8，反推极差与两端之和
    te, sigma, mode = 9, 2, 8
    spread = 6 * sigma
    ends = 6 * te - 4 * mode
    results["ch5_q24"] = dict(spread=spread, ends=ends)
    assert spread == 12
    assert ends == 22

    # 第5章题8：ES=2、历时 6、完成-开始滞后 2 → 紧后 ES=10
    succ_es = 2 + 6 + 2
    results["ch5_q8_succ_es"] = succ_es
    assert succ_es == 10

    # 第5章题26：ES=6、EF=14、LS=11、紧后 ES=18
    tf_m = 11 - 6
    ff_m = 18 - 14
    results["ch5_q26"] = dict(TF=tf_m, FF=ff_m)
    assert tf_m == 5 and ff_m == 4

    # 第5章题47：已知 σ=1.5，乐观与悲观相差 9 天
    results["ch5_q47_spread"] = 6 * 1.5
    assert results["ch5_q47_spread"] == 9

    # 第5章网络 时刻从0，EF=ES+dur
    dur = {"P": 4, "Q": 7, "R": 5, "S": 6, "T": 3}
    ES, EF = {}, {}
    ES["P"] = 0
    EF["P"] = ES["P"] + dur["P"]
    ES["Q"] = EF["P"]
    EF["Q"] = ES["Q"] + dur["Q"]
    ES["R"] = EF["P"]
    EF["R"] = ES["R"] + dur["R"]
    ES["S"] = EF["R"]
    EF["S"] = ES["S"] + dur["S"]
    ES["T"] = max(EF["Q"], EF["S"])
    EF["T"] = ES["T"] + dur["T"]
    T_total = EF["T"]
    results["ch5_network"] = {
        "ES": ES,
        "EF": EF,
        "total": T_total,
        "path_PQT": dur["P"] + dur["Q"] + dur["T"],
        "path_PRST": dur["P"] + dur["R"] + dur["S"] + dur["T"],
    }
    assert T_total == 18
    assert ES["T"] == 15
    assert results["ch5_network"]["path_PRST"] == 18
    assert results["ch5_network"]["path_PQT"] == 14
    # Q 的 TF：LS_Q = ES_T - dur_Q = 15-7=8, ES_Q=4, TF=4
    LS_T = ES["T"]  # 关键，LS_T=15
    LF_T = T_total
    LS_S = LS_T - dur["S"]  # 9
    LF_S = LS_T
    LS_Q = LS_T - dur["Q"]  # 8
    TF_Q = LS_Q - ES["Q"]  # 8-4=4
    FF_Q = ES["T"] - EF["Q"]  # 15-11=4
    results["ch5_Q_TF"] = TF_Q
    results["ch5_Q_FF"] = FF_Q

    # 第6章挣值
    BAC, PV, EV, AC = 450, 180, 165, 198
    CV = EV - AC
    SV = EV - PV
    CPI = EV / AC
    SPI = EV / PV
    EAC_atyp = AC + (BAC - EV)
    ETC_atyp = EAC_atyp - AC
    EAC_typ = BAC * AC / EV  # BAC/CPI
    ETC_typ = EAC_typ - AC
    results["ch6"] = dict(
        CV=CV,
        SV=SV,
        CPI=round(CPI, 2),
        SPI=round(SPI, 2),
        EAC_atyp=EAC_atyp,
        ETC_atyp=ETC_atyp,
        EAC_typ=EAC_typ,
        ETC_typ=ETC_typ,
    )
    assert CV == -33
    assert SV == -15
    assert EAC_atyp == 483
    assert ETC_atyp == 285
    assert EAC_typ == 540
    assert ETC_typ == 342

    fp = 10 * 4 + 6 * 5 + 9 * 4 + 4 * 10 + 2 * 7
    cost = fp * 800
    reserve = int(cost * 0.10)
    baseline = cost + reserve
    results["ch6_fp"] = dict(fp=fp, cost=cost, reserve=reserve, baseline=baseline)
    assert fp == 160
    assert cost == 128000
    assert reserve == 12800
    assert baseline == 140800

    # 第9章渠道
    def ch(n):
        return n * (n - 1) // 2

    results["ch9"] = {7: ch(7), 8: ch(8), "delta": ch(8) - ch(7)}
    assert ch(7) == 21
    assert ch(8) == 28

    # 第10章 EMV
    emv_a = 0.7 * 40 + 0.3 * (-20)
    emv_b = -8 + 0.9 * 30 + 0.1 * 0
    results["ch10"] = dict(emv_a=emv_a, emv_b=emv_b)
    assert emv_a == 22
    assert emv_b == 19

    # 第10章题23：另一组决策树，避免与案例 22/19 相同
    emv_c = 0.6 * 30 + 0.4 * (-15)
    emv_d = -6 + 0.8 * 25 + 0.2 * 0
    results["ch10_q23"] = dict(emv_c=emv_c, emv_d=emv_d)
    assert emv_c == 12
    assert emv_d == 14

    # 第6章题48：200 FP × 500 元；20000 为管理储备，不进基准
    fp_total = 200 * 500
    results["ch6_q48"] = dict(cost=fp_total, mgmt_reserve=20000, in_baseline=False)
    assert fp_total == 100000

    # 第7章帕累托累计
    freq = [45, 27, 12, 8, 8]
    cum = []
    s = 0
    for x in freq:
        s += x
        cum.append(s)
    results["ch7_pareto"] = cum[:3]
    assert cum[:3] == [45, 72, 84]

    print("ALL_CALCS_OK")
    for k, v in results.items():
        print(k, v)


if __name__ == "__main__":
    main()
