CREATE TABLE IF NOT EXISTS satellite_mode_cell (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    mode_id BIGINT NOT NULL REFERENCES satellite_modes(id) ON DELETE CASCADE,
    cell_num BIGINT NOT NULL,
    CONSTRAINT uq_satellite_mode_cell_mode_cell UNIQUE(mode_id, cell_num)
);

CREATE INDEX IF NOT EXISTS idx_satellite_mode_cell_mode_id
    ON satellite_mode_cell(mode_id);

CREATE INDEX IF NOT EXISTS idx_satellite_mode_cell_cell_num
    ON satellite_mode_cell(cell_num);

INSERT INTO satellite_mode_cell (mode_id, cell_num)
SELECT id, cell_num
FROM satellite_modes
WHERE cell_num > 0
ON CONFLICT DO NOTHING;
