param(
    [Parameter(Mandatory = $true)][string]$DataRoot,
    [Parameter(Mandatory = $true)][string]$RtkCapture,
    [Parameter(Mandatory = $true)][string]$ImuCapture,
    [Parameter(Mandatory = $true)][string]$OutputRoot,
    [string]$LidarObject = "frontlidar",
    [string]$Timezone = "+08:00",
    [double]$RtkReferenceHeightAboveGroundM = 0.8535,
    [int]$ExpectedStations = 34,
    [int]$MinPairs = 20,
    [int]$Bootstrap = 200
)

$ErrorActionPreference = "Stop"
$ExportRoot = Join-Path $OutputRoot "exported"
$PreparedRoot = Join-Path $OutputRoot "prepared_rtk_direct"
$CalibrationRoot = Join-Path $OutputRoot "calibration"

& (Join-Path $PSScriptRoot "export_multisensor_stations.ps1") `
    -DataRoot $DataRoot -RtkCapture $RtkCapture -ImuCapture $ImuCapture `
    -OutputRoot $ExportRoot -LidarObject $LidarObject -Timezone $Timezone
if ($LASTEXITCODE -ne 0) { throw "Raw-data export failed" }

& (Join-Path $PSScriptRoot "run_direct_rtk_lidar.ps1") `
    -CombinedRoot (Join-Path $ExportRoot "combined") `
    -WorkRoot $PreparedRoot -OutputRoot $CalibrationRoot `
    -RtkReferenceHeightAboveGroundM $RtkReferenceHeightAboveGroundM `
    -ExpectedStations $ExpectedStations -MinPairs $MinPairs -Bootstrap $Bootstrap
if ($LASTEXITCODE -ne 0) { throw "RTK-LiDAR calibration failed" }

Write-Host "Final result: $(Join-Path $CalibrationRoot 'final_T_RTK_lidar.json')"
Write-Host "Prepared frames: $(Join-Path $PreparedRoot 'frames_all')"
