CREATE TABLE IF NOT EXISTS satellite_mode_snapshots (
    id BIGSERIAL PRIMARY KEY,
    satellite_id BIGINT NOT NULL,
    interval_start TIMESTAMP NOT NULL,
    interval_end TIMESTAMP NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    is_active BOOLEAN NOT NULL DEFAULT FALSE,
    complex_plan_run_id BIGINT REFERENCES complex_plan_run(id) ON DELETE SET NULL,
    comment TEXT
);

CREATE INDEX IF NOT EXISTS idx_satellite_mode_snapshots_satellite_id
    ON satellite_mode_snapshots(satellite_id);

CREATE INDEX IF NOT EXISTS idx_satellite_mode_snapshots_interval_start
    ON satellite_mode_snapshots(interval_start);

CREATE INDEX IF NOT EXISTS idx_satellite_mode_snapshots_interval_end
    ON satellite_mode_snapshots(interval_end);

CREATE INDEX IF NOT EXISTS idx_satellite_mode_snapshots_is_active
    ON satellite_mode_snapshots(is_active);

CREATE INDEX IF NOT EXISTS idx_satellite_mode_snapshots_run_id
    ON satellite_mode_snapshots(complex_plan_run_id);

ALTER TABLE satellite_modes
    ADD COLUMN IF NOT EXISTS snapshot_id BIGINT REFERENCES satellite_mode_snapshots(id) ON DELETE CASCADE;

INSERT INTO satellite_mode_snapshots (
    satellite_id,
    interval_start,
    interval_end,
    created_at,
    is_active,
    complex_plan_run_id,
    comment
)
SELECT
    sm.satellite_id,
    COALESCE(cpr.interval_start, MIN(sm.start_time)),
    COALESCE(cpr.interval_end, MAX(sm.end_time)),
    COALESCE(cpr.created_at, CURRENT_TIMESTAMP),
    TRUE,
    sm.complex_plan_run_id,
    CASE
        WHEN sm.complex_plan_run_id IS NULL THEN 'legacy-backfill-no-run'
        ELSE 'legacy-backfill-from-run'
    END
FROM satellite_modes sm
LEFT JOIN complex_plan_run cpr ON cpr.id = sm.complex_plan_run_id
WHERE sm.snapshot_id IS NULL
GROUP BY
    sm.satellite_id,
    sm.complex_plan_run_id,
    cpr.interval_start,
    cpr.interval_end,
    cpr.created_at;

UPDATE satellite_modes sm
SET snapshot_id = snapshots.id
FROM satellite_mode_snapshots snapshots
WHERE sm.snapshot_id IS NULL
  AND snapshots.satellite_id = sm.satellite_id
  AND (
    (sm.complex_plan_run_id IS NOT NULL AND snapshots.complex_plan_run_id = sm.complex_plan_run_id)
    OR
    (sm.complex_plan_run_id IS NULL AND snapshots.complex_plan_run_id IS NULL)
  );

WITH ranked_snapshots AS (
    SELECT
        id,
        ROW_NUMBER() OVER (
            PARTITION BY satellite_id, interval_start, interval_end
            ORDER BY created_at DESC, id DESC
        ) AS rn
    FROM satellite_mode_snapshots
)
UPDATE satellite_mode_snapshots snapshots
SET is_active = CASE WHEN ranked_snapshots.rn = 1 THEN TRUE ELSE FALSE END
FROM ranked_snapshots
WHERE ranked_snapshots.id = snapshots.id;

ALTER TABLE satellite_modes
    ALTER COLUMN snapshot_id SET NOT NULL;

CREATE INDEX IF NOT EXISTS idx_satellite_modes_snapshot_id
    ON satellite_modes(snapshot_id);

CREATE UNIQUE INDEX IF NOT EXISTS uq_satellite_mode_snapshots_active_per_interval
    ON satellite_mode_snapshots(satellite_id, interval_start, interval_end)
    WHERE is_active = TRUE;
