with open("app/static/bim/index.html", "r", encoding="utf-8") as f:
    content = f.read()

# Replace duration
content = content.replace("function exportLifetimeVideo(durationMs = 50000)", "function exportLifetimeVideo(durationMs = 150000)")
content = content.replace("function exportLifetimeVideo(durationMs = 5000)", "function exportLifetimeVideo(durationMs = 150000)")

# Find waypoints
start_idx = content.find('const camWaypoints = [')
end_idx = content.find('const targetSpline = new THREE.CatmullRomCurve3(targetWaypoints);') + len('const targetSpline = new THREE.CatmullRomCurve3(targetWaypoints);')

new_waypoints = """const camWaypoints = [
            // Toàn cảnh đầu tuyến
            new THREE.Vector3(-30, 45, 30),
            // Phân đoạn 1 (từ góc cao)
            getPointAt(10, 25, 20),
            // Phân đoạn 1 (góc cận cảnh bên dưới để thấy cốt thép)
            getPointAt(35, -15, 10),
            // Chuyển giao sang Phân đoạn 2
            getPointAt(60, 20, 15),
            // Phân đoạn 2 (góc cận)
            getPointAt(85, -20, 12),
            // Chuyển giao sang Phân đoạn 3
            getPointAt(110, 25, 18),
            // Phân đoạn 3 (góc cận cửa xả)
            getPointAt(135, -20, 12),
            // Toàn cảnh cuối tuyến (rút cừ xong)
            new THREE.Vector3(50, 75, 80)
          ];
          const targetWaypoints = [
            getPointAt(10, 0, 0),
            getPointAt(25, 0, -2),
            getPointAt(35, 0, -2),
            getPointAt(70, 0, -2),
            getPointAt(85, 0, -2),
            getPointAt(115, 0, -2),
            getPointAt(135, 0, -2),
            getPointAt(75, 0, 0)
          ];
          
          const camSpline = new THREE.CatmullRomCurve3(camWaypoints);
          const targetSpline = new THREE.CatmullRomCurve3(targetWaypoints);"""

if start_idx != -1 and end_idx != -1:
    content = content[:start_idx] + new_waypoints + content[end_idx:]
    with open("app/static/bim/index.html", "w", encoding="utf-8") as f:
        f.write(content)
    print("Camera updated successfully!")
else:
    print("Failed to find indices!")
