"""Kiem thu bam nguon (grounding) cho mo hinh BIM Cong hop Ong Den - ba Lanh.

Moi gia tri duoc kiem o day deu truy nguyen ve ho so that:
- DWG `02. THIET KE CHI TIET_1_3.8.dwg` (chuyen bang tools/libredwg/dwg2dxf.exe):
  tim tuyen layer `TK-DBO-1317-Mb Cong doc`, ga cho layer `1.GACHO`,
  chi tiet BPTC cu (5000 giua 2 hang cu, cu dai 5000, 2 tang vang cach 1500, buoc chong 10000).
- DWG `03. TD +TN_3.8.DWG`: bang cong A=50d27'36'' R=25 K=22.02 T=11.78.
- Du toan tham dinh `02. DToan thanh dinh ba Lanh.xls`: phan doan 50/50/45, M250 = 439,636 m3,
  bang can nuoc 145*0,25*4, chong ngang 10 m/diem x 2 tang, cu tinh dai 5 m.
- HD + Gantt BPTC: 270 ngay 25/09/2026 -> 21/06/2027.
"""
from pathlib import Path
import re

import pytest

HTML = Path(__file__).resolve().parents[1] / "app" / "static" / "bim" / "index.html"


@pytest.fixture(scope="module")
def src() -> str:
    return HTML.read_text(encoding="utf-8")


@pytest.mark.parametrize("bad", ["51.46", "101.46", "474.9", "Larsen IV", "I300", "s = 6.0", "/ 90", "Ngày 90"])
def test_no_unsourced_values(src, bad):
    assert bad not in src, f"Gia tri khong co nguon con ton tai: {bad}"


def test_no_synthetic_diary(src):
    # Luat 1: cam du lieu ao - thoi tiet/nhan luc sinh theo cong thuc modulo
    assert not re.search(r"day\s*%\s*\d", src), "Nhat ky gia (day % n) van con"
    assert "weather" not in src.lower()


def test_alignment_from_dwg(src):
    for token in ["1.428", "61.434", "50.46", "5.999", "R_CURVE = 25.0"]:
        assert token in src, f"Thieu thong so tuyen tu DWG: {token}"
    for sta in ["18.16", "46.47", "75.60", "97.62", "125.69", "146.46"]:
        assert sta in src


def test_segments_and_joints(src):
    assert "JOINT_STATIONS = [50.0, 100.0]" in src
    assert "439.64" in src


def test_shoring_from_dwg_and_estimate(src):
    for token in ["SHEET_PILE_OFFSET = 2.5", "SHEET_PILE_LENGTH = 5.0", "STRUT_SPACING = 10.0",
                  "STRUT_SPACING_TC1_C4 = 3.5", "STRUT_TIER_GAP = 1.5", "U250"]:
        assert token in src, f"Thieu thong so cu/vang: {token}"


def test_waterstop_longitudinal(src):
    assert "WATERSTOP_LINES = 4" in src


def test_manholes_from_dwg(src):
    for sta in ["19.29", "33.37", "75.17", "103.69", "117.25"]:
        assert sta in src, f"Thieu ly trinh ga cho tu DWG: {sta}"


def test_schedule_270_days(src):
    assert "TOTAL_DAYS = 270" in src
    assert "2026-09-25" in src
    for d in ["2026-10-10", "2026-12-20", "2027-03-15", "2027-05-15", "2027-06-21"]:
        assert d in src, f"Thieu moc Gantt: {d}"
