CREATE TABLE route_passports (
    route_id UUID PRIMARY KEY,
    route_name_full TEXT NOT NULL,
    route_name_short VARCHAR(255) NOT NULL,
    ka_short VARCHAR(64) NOT NULL,
    ka_full VARCHAR(255) NOT NULL,
    orbit_number BIGINT NOT NULL,
    orbit_state VARCHAR(64) NOT NULL,
    interval_begin TIMESTAMP NOT NULL,
    interval_end TIMESTAMP NOT NULL,
    roll_angle_min NUMERIC NOT NULL,
    roll_angle_max NUMERIC NOT NULL,
    visir_angle_min NUMERIC NOT NULL,
    visir_angle_max NUMERIC NOT NULL,
    resolution_range NUMERIC NOT NULL,
    resolution_azimuth NUMERIC NOT NULL,
    polarisation JSONB NOT NULL,
    processing_level VARCHAR(64) NOT NULL,
    geometry_wkt TEXT NOT NULL,
    route_status VARCHAR(32) NOT NULL,
    trace_id VARCHAR(255),
    correlation_id VARCHAR(255),
    source_topic VARCHAR(255),
    source_partition INTEGER,
    source_offset BIGINT,
    source_key VARCHAR(255),
    raw_payload JSONB,
    created_at TIMESTAMP NOT NULL,
    updated_at TIMESTAMP NOT NULL,
    version BIGINT NOT NULL DEFAULT 0
);

CREATE INDEX idx_route_passports_interval
    ON route_passports (interval_begin, interval_end);

CREATE INDEX idx_route_passports_ka_orbit
    ON route_passports (ka_short, orbit_number);

CREATE INDEX idx_route_passports_status
    ON route_passports (route_status);

CREATE UNIQUE INDEX ux_route_passports_source_record
    ON route_passports (source_topic, source_partition, source_offset)
    WHERE source_topic IS NOT NULL
      AND source_partition IS NOT NULL
      AND source_offset IS NOT NULL;
