"""
Dip chart calibration data for all 4 tank types.
- LP92  (15000L): cylinder formula 0.5-200.0 cm + measured Group 9 (200.5-213.0)
- EURO3 (9000L):  measured Groups 1-4 (0.5-100.0) + formula Groups 5-7 + measured Group 8 (175.5-180.0)
- LAD   (22500L): fully measured, integer cm 1-244
- LADXM (9000L):  same calibration as EURO3
"""
import math


# ---------------------------------------------------------------------------
# Cylinder formula helpers
# ---------------------------------------------------------------------------

def _v9000(h: float) -> float:
    """Corrected horizontal cylinder formula: 9000L tank (r=90 cm, L=372.9 cm)."""
    r, L, k = 90.0, 372.9, 0.02241
    if h <= 0.0:
        return 0.0
    if h >= 180.0:
        return 9489.1
    arg = max(-1.0, min(1.0, (r - h) / r))
    vf = L * (r * r * math.acos(arg) - (r - h) * math.sqrt(max(0.0, 2 * r * h - h * h))) / 1000.0
    return round(vf * (1.0 - k * math.sin(math.pi * h / (2 * r))), 1)


def _v15000(h: float) -> float:
    """Horizontal cylinder formula: 15000L tank (r=106.5 cm, L=455.9 cm)."""
    r, L = 106.5, 455.9
    if h <= 0.0:
        return 0.0
    if h >= 213.0:
        return 16241.4
    arg = max(-1.0, min(1.0, (r - h) / r))
    vf = L * (r * r * math.acos(arg) - (r - h) * math.sqrt(max(0.0, 2 * r * h - h * h))) / 1000.0
    return round(vf, 1)


# ---------------------------------------------------------------------------
# 9000L — measured calibration data (Groups 1-4 and Group 8)
# ---------------------------------------------------------------------------

_9K_MEASURED: list[tuple[float, float]] = [
    # Group 1 (0.5-25.0)
    (0.5, 2.3), (1.0, 6.5), (1.5, 11.9), (2.0, 18.3), (2.5, 25.5), (3.0, 33.6), (3.5, 42.2),
    (4.0, 51.6), (4.5, 61.5), (5.0, 72.0), (5.5, 82.9), (6.0, 94.4), (6.5, 106.6), (7.0, 118.8),
    (7.5, 131.6), (8.0, 144.9), (8.5, 158.6), (9.0, 172.0), (9.5, 187.0), (10.0, 201.8),
    (10.5, 217.0), (11.0, 232.5), (11.5, 248.3), (12.0, 264.7), (12.5, 280.9), (13.0, 297.6),
    (13.5, 314.7), (14.0, 332.1), (14.5, 349.7), (15.0, 367.6), (15.5, 385.8), (16.0, 404.3),
    (16.5, 423.0), (17.0, 442.0), (17.5, 461.5), (18.0, 481.4), (18.5, 500.5), (19.0, 520.5),
    (19.5, 540.7), (20.0, 561.8), (20.5, 581.8), (21.0, 602.7), (21.5, 623.8), (22.0, 645.1),
    (22.5, 666.8), (23.0, 688.3), (23.5, 710.3), (24.0, 732.4), (24.5, 754.7), (25.0, 777.1),
    # Group 2 (25.5-50.0)
    (25.5, 800.0), (26.0, 822.9), (26.5, 846.0), (27.0, 869.3), (27.5, 892.7), (28.0, 916.3),
    (28.5, 940.0), (29.0, 964.1), (29.5, 988.2), (30.0, 1012.5), (30.5, 1037.0), (31.0, 1061.6),
    (31.5, 1086.5), (32.0, 1111.4), (32.5, 1136.5), (33.0, 1161.7), (33.5, 1187.1),
    (34.0, 1212.7), (34.5, 1238.4), (35.0, 1264.2), (35.5, 1290.2), (36.0, 1316.3),
    (36.5, 1342.6), (37.0, 1369.0), (37.5, 1395.5), (38.0, 1422.1), (38.5, 1448.9),
    (39.0, 1475.9), (39.5, 1502.8), (40.0, 1530.1), (40.5, 1557.4), (41.0, 1584.8),
    (41.5, 1612.3), (42.0, 1640.0), (42.5, 1667.7), (43.0, 1695.6), (43.5, 1723.6),
    (44.0, 1751.7), (44.5, 1779.9), (45.0, 1808.2), (45.5, 1836.7), (46.0, 1865.2),
    (46.5, 1893.8), (47.0, 1922.6), (47.5, 1951.5), (48.0, 1980.4), (48.5, 2009.4),
    (49.0, 2038.5), (49.5, 2067.8), (50.0, 2097.1),
    # Group 3 (50.5-75.0)
    (50.5, 2126.5), (51.0, 2156.0), (51.5, 2185.6), (52.0, 2215.3), (52.5, 2245.1),
    (53.0, 2274.9), (53.5, 2304.8), (54.0, 2334.9), (54.5, 2365.1), (55.0, 2395.2),
    (55.5, 2425.4), (56.0, 2455.8), (56.5, 2486.4), (57.0, 2516.7), (57.5, 2547.2),
    (58.0, 2577.9), (58.5, 2608.6), (59.0, 2639.4), (59.5, 2670.2), (60.0, 2701.1),
    (60.5, 2732.1), (61.0, 2763.1), (61.5, 2794.1), (62.0, 2825.4), (62.5, 2856.7),
    (63.0, 2888.0), (63.5, 2919.3), (64.0, 2950.7), (64.5, 2982.1), (65.0, 3013.7),
    (65.5, 3045.3), (66.0, 3077.0), (66.5, 3108.6), (67.0, 3140.4), (67.5, 3172.2),
    (68.0, 3204.0), (68.5, 3235.9), (69.0, 3267.9), (69.5, 3299.9), (70.0, 3331.9),
    (70.5, 3363.9), (71.0, 3396.1), (71.5, 3428.2), (72.0, 3460.4), (72.5, 3492.6),
    (73.0, 3524.9), (73.5, 3557.3), (74.0, 3589.6), (74.5, 3622.0), (75.0, 3654.4),
    # Group 4 (75.5-100.0)
    (75.5, 3686.9), (76.0, 3719.4), (76.5, 3751.9), (77.0, 3784.4), (77.5, 3817.0),
    (78.0, 3849.6), (78.5, 3882.3), (79.0, 3914.9), (79.5, 3947.6), (80.0, 3980.3),
    (80.5, 4013.1), (81.0, 4045.8), (81.5, 4078.6), (82.0, 4111.4), (82.5, 4144.3),
    (83.0, 4177.1), (83.5, 4210.0), (84.0, 4242.9), (84.5, 4275.7), (85.0, 4308.6),
    (85.5, 4341.6), (86.0, 4374.5), (86.5, 4407.5), (87.0, 4440.4), (87.5, 4473.4),
    (88.0, 4506.3), (88.5, 4539.3), (89.0, 4572.3), (89.5, 4605.2), (90.0, 4638.3),
    (90.5, 4671.3), (91.0, 4704.3), (91.5, 4737.3), (92.0, 4770.3), (92.5, 4803.3),
    (93.0, 4836.3), (93.5, 4869.3), (94.0, 4902.4), (94.5, 4935.4), (95.0, 4968.3),
    (95.5, 5001.3), (96.0, 5034.3), (96.5, 5067.3), (97.0, 5100.3), (97.5, 5133.3),
    (98.0, 5166.2), (98.5, 5199.1), (99.0, 5232.0), (99.5, 5264.9), (100.0, 5297.8),
    # Group 8 (175.5-180.0) — measured from page 2 of dip chart PDF
    (175.5, 9382.1), (176.0, 9395.7), (176.5, 9409.0), (177.0, 9421.8), (177.5, 9434.3),
    (178.0, 9446.2), (178.5, 9457.7), (179.0, 9468.7), (179.5, 9479.2), (180.0, 9489.1),
]


# ---------------------------------------------------------------------------
# 15000L — measured Group 9 (200.5-213.0), confirmed from PDF page 1
# ---------------------------------------------------------------------------

_15K_G9: list[tuple[float, float]] = [
    (200.5, 15856.4), (201.0, 15879.0), (201.5, 15901.2), (202.0, 15922.9),
    (202.5, 15944.1), (203.0, 15964.9), (203.5, 15985.2), (204.0, 16005.0),
    (204.5, 16024.2), (205.0, 16043.0), (205.5, 16061.2), (206.0, 16078.8),
    (206.5, 16095.8), (207.0, 16112.1), (207.5, 16127.9), (208.0, 16142.9),
    (208.5, 16157.3), (209.0, 16170.8), (209.5, 16183.6), (210.0, 16195.6),
    (210.5, 16206.5), (211.0, 16216.4), (211.5, 16225.1), (212.0, 16232.5),
    (212.5, 16238.3), (213.0, 16241.4),
]


# ---------------------------------------------------------------------------
# 22500L — fully measured from PDF page 3 (integer cm steps, 1-244)
# ---------------------------------------------------------------------------

_22500_DATA: list[tuple[float, float]] = [
    (1, 10.8), (2, 30.4), (3, 55.9), (4, 85.9), (5, 119.9), (6, 157.4), (7, 198.1),
    (8, 241.7), (9, 288.0), (10, 336.9), (11, 388.2), (12, 441.8), (13, 497.5),
    (14, 555.3), (15, 615.1), (16, 676.7), (17, 740.2), (18, 805.4), (19, 872.3),
    (20, 940.9), (21, 1011.0), (22, 1082.6), (23, 1155.8), (24, 1230.4), (25, 1306.3),
    (26, 1383.7), (27, 1462.3), (28, 1542.0), (29, 1623.4), (30, 1705.8),
    (31, 1789.4), (32, 1874.2), (33, 1960.1), (34, 2047.1), (35, 2135.2),
    (36, 2224.3), (37, 2314.5), (38, 2405.6), (39, 2497.8), (40, 2590.9),
    (41, 2684.9), (42, 2779.9), (43, 2875.8), (44, 2972.5), (45, 3070.1),
    (46, 3168.6), (47, 3267.9), (48, 3368.0), (49, 3468.9),
    (50, 3570.5), (51, 3672.9), (52, 3776.1), (53, 3879.9), (54, 3984.5),
    (55, 4089.8), (56, 4195.8), (57, 4302.4), (58, 4409.7), (59, 4517.6),
    (60, 4626.2), (61, 4735.3), (62, 4845.1), (63, 4955.4), (64, 5066.3),
    (65, 5177.8), (66, 5289.8), (67, 5402.4), (68, 5515.4), (69, 5629.0),
    (70, 5743.1), (71, 5857.7), (72, 5972.8), (73, 6088.3), (74, 6204.3),
    (75, 6320.7), (76, 6437.5), (77, 6554.8), (78, 6672.5), (79, 6790.6),
    (80, 6909.0), (81, 7027.9), (82, 7147.1), (83, 7266.7), (84, 7386.6),
    (85, 7506.9), (86, 7627.5), (87, 7748.4), (88, 7869.7), (89, 7991.2),
    (90, 8113.0), (91, 8235.1), (92, 8357.5), (93, 8480.2), (94, 8603.0),
    (95, 8726.2), (96, 8849.6), (97, 8973.2), (98, 9097.0),
    (99, 9221.0), (100, 9345.2), (101, 9469.6), (102, 9594.2), (103, 9719.0),
    (104, 9843.9), (105, 9969.0), (106, 10094.2), (107, 10219.6), (108, 10345.1),
    (109, 10470.7), (110, 10596.4), (111, 10722.3), (112, 10848.2), (113, 10974.2),
    (114, 11100.3), (115, 11226.4), (116, 11352.7), (117, 11478.9), (118, 11605.2),
    (119, 11731.6), (120, 11857.9), (121, 11984.3), (122, 12110.7), (123, 12237.1),
    (124, 12363.4), (125, 12489.8), (126, 12616.2), (127, 12742.5), (128, 12868.8),
    (129, 12995.0), (130, 13121.1), (131, 13247.2), (132, 13373.2), (133, 13499.1),
    (134, 13625.0), (135, 13750.7), (136, 13876.3), (137, 14001.8), (138, 14127.2),
    (139, 14252.4), (140, 14377.5), (141, 14502.4), (142, 14627.2), (143, 14751.8),
    (144, 14876.2), (145, 15000.4), (146, 15124.4), (147, 15248.3),
    (148, 15371.9), (149, 15495.2), (150, 15618.4), (151, 15741.3), (152, 15863.9),
    (153, 15986.3), (154, 16108.4), (155, 16230.2), (156, 16351.7), (157, 16473.0),
    (158, 16593.9), (159, 16714.5), (160, 16834.8), (161, 16954.7), (162, 17074.3),
    (163, 17193.5), (164, 17312.4), (165, 17430.8), (166, 17548.9), (167, 17666.6),
    (168, 17783.9), (169, 17900.7), (170, 18017.1), (171, 18133.1), (172, 18248.6),
    (173, 18363.7), (174, 18478.3), (175, 18592.4), (176, 18706.0), (177, 18819.0),
    (178, 18931.6), (179, 19043.6), (180, 19155.1), (181, 19266.0), (182, 19376.3),
    (183, 19486.1), (184, 19595.3), (185, 19703.8), (186, 19811.7), (187, 19919.0),
    (188, 20025.6), (189, 20131.6), (190, 20236.9), (191, 20341.5), (192, 20445.3),
    (193, 20548.5), (194, 20650.9), (195, 20752.6), (196, 20853.4),
    (197, 20953.5), (198, 21052.8), (199, 21151.3), (200, 21248.9), (201, 21345.6),
    (202, 21441.5), (203, 21536.5), (204, 21630.5), (205, 21723.6), (206, 21815.8),
    (207, 21907.0), (208, 21997.1), (209, 22086.2), (210, 22174.3), (211, 22261.3),
    (212, 22347.2), (213, 22432.0), (214, 22515.6), (215, 22598.0), (216, 22679.2),
    (217, 22759.1), (218, 22837.7), (219, 22915.1), (220, 22991.0), (221, 23065.6),
    (222, 23138.8), (223, 23210.4), (224, 23280.5), (225, 23349.1), (226, 23416.0),
    (227, 23481.2), (228, 23544.7), (229, 23606.3), (230, 23666.1), (231, 23723.9),
    (232, 23779.6), (233, 23833.2), (234, 23884.5), (235, 23933.4), (236, 23979.7),
    (237, 24023.3), (238, 24064.0), (239, 24101.5), (240, 24135.5), (241, 24165.6),
    (242, 24191.0), (243, 24210.6), (244, 24221.4),
]


# ---------------------------------------------------------------------------
# Build complete calibration tables
# ---------------------------------------------------------------------------

def _build_9000() -> list[tuple[float, float]]:
    """Measured groups 1-4 and 8; cylinder formula for groups 5-7 (100.5-175.0)."""
    measured = {h: v for h, v in _9K_MEASURED}
    result = list(_9K_MEASURED)
    h = 100.5
    while h <= 175.0 + 1e-9:
        h_r = round(h, 1)
        if h_r not in measured:
            result.append((h_r, _v9000(h_r)))
        h += 0.5
    return sorted(result)


def _build_15000() -> list[tuple[float, float]]:
    """Cylinder formula for 0.5-200.0 cm; measured Group 9 for 200.5-213.0 cm."""
    result: list[tuple[float, float]] = []
    h = 0.5
    while h <= 200.0 + 1e-9:
        result.append((round(h, 1), _v15000(round(h, 1))))
        h += 0.5
    result.extend(_15K_G9)
    return sorted(result)


# ---------------------------------------------------------------------------
# Public calibration data dict (built once at import time)
# ---------------------------------------------------------------------------

CALIBRATION_DATA: dict[str, list[tuple[float, float]]] = {
    'LP92':  _build_15000(),
    'EURO3': _build_9000(),
    'LAD':   _22500_DATA,
    'LADXM': _build_9000(),
}


# ---------------------------------------------------------------------------
# Seeding helper — called from wsgi.py after DB init
# ---------------------------------------------------------------------------

def seed_dip_charts(engine) -> None:
    """Insert calibration data for any tank type that has no rows yet."""
    from sqlalchemy import text
    from .database import SessionLocal

    db = SessionLocal()
    try:
        for tank_type, rows in CALIBRATION_DATA.items():
            count = db.execute(
                text("SELECT COUNT(*) FROM tank_dip_charts WHERE tank_type = :t"),
                {"t": tank_type},
            ).scalar() or 0
            if count > 0:
                continue
            db.execute(
                text(
                    "INSERT INTO tank_dip_charts (tank_type, height_cm, volume_ltr) "
                    "VALUES (:tank_type, :height_cm, :volume_ltr)"
                ),
                [{"tank_type": tank_type, "height_cm": h, "volume_ltr": v} for h, v in rows],
            )
            db.commit()
    except Exception:
        db.rollback()
        raise
    finally:
        db.close()
