#!/usr/bin/env python3
"""Build an FOB/CIF oil price from a published benchmark (Platts/Argus/ICE).

Examples:
  EN590 CIF Lome:
    python3 oil_price_calc.py --benchmark 1130 --unit mt --differential -8 \
      --density 0.840 --freight-lumpsum 1600000 --cargo 37000 \
      --insurance-rate 0.0015 --buffer 3

  Crude FOB offshore Ghana:
    python3 oil_price_calc.py --benchmark 92.22 --unit bbl --differential 1.50 \
      --density 0.835
"""
import argparse


def parse_args():
    p = argparse.ArgumentParser(
        description="Compute FOB/CIF price build-up from a published benchmark."
    )
    p.add_argument("--benchmark", type=float, required=True,
                   help="Published benchmark flat price (ICE/Platts/Argus)")
    p.add_argument("--unit", choices=["bbl", "mt"], default="mt",
                   help="Unit of the benchmark quote (default: mt)")
    p.add_argument("--differential", type=float, default=0.0,
                   help="Negotiated +/- differential, same unit as benchmark")
    p.add_argument("--density", type=float, default=0.840,
                   help="Product density in t/m3 (EN590 ~0.835-0.845)")
    p.add_argument("--freight-mt", type=float,
                   help="Ocean freight in $/MT")
    p.add_argument("--freight-lumpsum", type=float,
                   help="Chartered lumpsum freight in USD (divided by cargo size)")
    p.add_argument("--cargo", type=float, default=50000,
                   help="Cargo size in MT (default: 50000)")
    p.add_argument("--insurance-rate", type=float, default=0.0015,
                   help="Insurance premium rate as decimal, applied to 110%% of CIF (default: 0.0015)")
    p.add_argument("--buffer", type=float, default=0.0,
                   help="Seller demurrage/financing buffer in $/MT")
    return p.parse_args()


def main():
    a = parse_args()
    bpm = 1000 / (a.density * 158.987)

    if a.unit == "mt":
        bench_mt = a.benchmark
        bench_bbl = a.benchmark / bpm
        diff_mt = a.differential
    else:
        bench_bbl = a.benchmark
        bench_mt = a.benchmark * bpm
        diff_mt = a.differential * bpm

    fob_mt = bench_mt + diff_mt

    if a.freight_lumpsum:
        freight_mt = a.freight_lumpsum / a.cargo
        freight_src = f"lumpsum ${a.freight_lumpsum:,.0f} / {a.cargo:,.0f} MT"
    elif a.freight_mt:
        freight_mt = a.freight_mt
        freight_src = "flat rate quoted $/MT"
    else:
        freight_mt = 0.0
        freight_src = "none (FOB terms)"

    pre_ins = fob_mt + freight_mt
    insurance_mt = pre_ins * 1.10 * a.insurance_rate
    cif_mt = pre_ins + insurance_mt + a.buffer
    cif_bbl = cif_mt / bpm

    w = 52
    print("=" * w)
    print("OIL PRICE BUILD-UP".center(w))
    print("=" * w)
    print(f"Benchmark            : ${bench_mt:>12,.2f} /MT   (${bench_bbl:,.2f} /bbl)")
    print(f"Density              : {a.density:>12,.3f} t/m3  ({bpm:,.3f} bbl/MT)")
    print(f"Differential         : ${diff_mt:>+12,.2f} /MT")
    print("-" * w)
    print(f"FOB load port        : ${fob_mt:>12,.2f} /MT")
    print(f"Freight              : ${freight_mt:>12,.2f} /MT   ({freight_src})")
    print(f"Subtotal (C+I basis) : ${pre_ins:>12,.2f} /MT")
    print(f"Insurance ICC(A)+war : ${insurance_mt:>12,.2f} /MT   "
          f"(110% x ${pre_ins:,.2f} x {a.insurance_rate*100:.3f}%)")
    print(f"Buffer               : ${a.buffer:>12,.2f} /MT")
    print("-" * w)
    print(f"CIF delivery port    : ${cif_mt:>12,.2f} /MT   (${cif_bbl:,.2f} /bbl)")
    print(f"Total cargo value    : ${cif_mt * a.cargo:>16,.0f}  ({a.cargo:,.0f} MT)")
    print("=" * w)


if __name__ == "__main__":
    main()
