CREATE TABLE IF NOT EXISTS satellite_group (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    name VARCHAR(128) NOT NULL
);

CREATE TABLE IF NOT EXISTS satellite_group_satellite (
    group_id BIGINT NOT NULL REFERENCES satellite_group(id) ON DELETE CASCADE,
    satellite_id BIGINT NOT NULL REFERENCES satellite(id) ON DELETE CASCADE,
    PRIMARY KEY (group_id, satellite_id)
);

CREATE UNIQUE INDEX IF NOT EXISTS idx_satellite_group_name ON satellite_group(name);
CREATE INDEX IF NOT EXISTS idx_satellite_group_satellite_satellite_id
    ON satellite_group_satellite(satellite_id);

INSERT INTO satellite_group (name)
SELECT 'KONDOR'
WHERE NOT EXISTS (
    SELECT 1
    FROM satellite_group
    WHERE name = 'KONDOR'
);

INSERT INTO satellite_group_satellite (group_id, satellite_id)
SELECT satellite_group.id, satellites.satellite_id
FROM satellite_group
CROSS JOIN (
    VALUES
        (56756),
        (62138)
) AS satellites(satellite_id)
WHERE satellite_group.name = 'KONDOR'
  AND NOT EXISTS (
      SELECT 1
      FROM satellite_group_satellite
      WHERE satellite_group_satellite.group_id = satellite_group.id
        AND satellite_group_satellite.satellite_id = satellites.satellite_id
  );
