import json
import math
import numpy as np

def get_alignment(s):
    L1 = 46.36
    L2 = 24.93
    Lc = 24.12
    th1 = 0.0
    th2 = math.radians(56.6)
    th3 = math.radians(7.0)
    
    if s <= L1:
        x = s * math.cos(th1)
        z = s * math.sin(th1)
        th = th1
    elif s <= L1 + L2:
        s_rel = s - L1
        x = L1 * math.cos(th1) + s_rel * math.cos(th2)
        z = L1 * math.sin(th1) + s_rel * math.sin(th2)
        th = th2
    elif s <= L1 + L2 + Lc:
        s_rel = s - (L1 + L2)
        k = (th2 - th3) / Lc
        dx = (math.sin(th2) - math.sin(th2 - k * s_rel)) / k
        dz = (math.cos(th2 - k * s_rel) - math.cos(th2)) / k
        x_td1 = L1 * math.cos(th1) + L2 * math.cos(th2)
        z_td1 = L1 * math.sin(th1) + L2 * math.sin(th2)
        x = x_td1 + dx
        z = z_td1 + dz
        th = th2 - k * s_rel
    else:
        s_rel = s - (L1 + L2 + Lc)
        k = (th2 - th3) / Lc
        dx_c = (math.sin(th2) - math.sin(th3)) / k
        dz_c = (math.cos(th3) - math.cos(th2)) / k
        x_tc1 = L1 * math.cos(th1) + L2 * math.cos(th2) + dx_c
        z_tc1 = L1 * math.sin(th1) + L2 * math.sin(th2) + dz_c
        x = x_tc1 + s_rel * math.cos(th3)
        z = z_tc1 + s_rel * math.sin(th3)
        th = th3
    return x, z, th

def point_at(s, y, offset):
    x, z, th = get_alignment(s)
    # normal vector: (-sin(th), cos(th))
    px = x - offset * math.sin(th)
    pz = z + offset * math.cos(th)
    return px, y, pz

def main():
    metadata = {
        "project_name": "Cống hộp từ nhà ông Đèn đến nhà bà Lanh, xã Kiến Minh",
        "alignment_type": "Curved Non-Linear Horizontal Alignment (Bình đồ uốn cong thực tế)",
        "total_length_m": 145.0,
        "contract_value_vnd": 6482295000,
        "alignment_nodes": [
            {"station_m": 0.0, "name": "Cọc 1 (Đầu cống D01 - Nhà ông Đèn)", "x": 0.0, "z": 0.0, "angle_deg": 0.0},
            {"station_m": 23.18, "name": "Cọc 2 (Cửa thăm CT-01)", "x": 23.18, "z": 0.0, "angle_deg": 0.0},
            {"station_m": 46.36, "name": "Cọc 3 / Đỉnh ngoặt P1 (Bẻ gắt vào ngõ khu dân cư)", "x": 46.36, "z": 0.0, "angle_deg": 56.6},
            {"station_m": 71.29, "name": "Cọc TD1 (Tiếp đầu đường cong lượn qua vườn)", "x": 60.08, "z": 20.81, "angle_deg": 56.6},
            {"station_m": 83.35, "name": "Đỉnh cung lượn (Bán kính cong R = 28m)", "x": 68.66, "z": 29.16, "angle_deg": 31.8},
            {"station_m": 95.41, "name": "Cọc TC1 (Tiếp cuối đường cong, hố ga CT2)", "x": 79.95, "z": 33.13, "angle_deg": 7.0},
            {"station_m": 120.30, "name": "Cọc 4 (Đoạn thẳng ngõ xóm)", "x": 104.65, "z": 36.16, "angle_deg": 7.0},
            {"station_m": 145.00, "name": "Cọc 5 (Cửa xả mương thủy lợi - Nhà bà Lanh)", "x": 129.17, "z": 39.17, "angle_deg": 7.0}
        ],
        "cross_section": {
            "clear_span_m": 2.5,
            "clear_height_m": 2.5,
            "wall_thickness_m": 0.25,
            "bottom_slab_thickness_m": 0.25,
            "top_slab_thickness_m": 0.25,
            "haunches_chamfer_mm": "150x150",
            "bamboo_piles_density_m2": 25,
            "bamboo_pile_length_m": 2.5,
            "larsen_sheet_pile_type": "Larsen IV (H=10m)"
        }
    }
    
    with open(r'C:\Users\Admin\.gemini\antigravity\brain\dd9b916f-153c-4fba-b1cd-9ab49b94ab2b\ong_den_bim_metadata.json', 'w', encoding='utf-8') as f:
        json.dump(metadata, f, ensure_ascii=False, indent=2)
    print("Metadata written successfully.")

if __name__ == '__main__':
    main()
