CREATE EXTENSION IF NOT EXISTS postgis;

CREATE TABLE IF NOT EXISTS cell_covering (
    cell_covering_id BIGSERIAL PRIMARY KEY,
    satellite_id BIGINT NOT NULL,
    cell_num BIGINT NOT NULL,
    rev_begin BIGINT NOT NULL,
    time_begin TIMESTAMP NOT NULL,
    rev_sign_begin INT NOT NULL,
    rev_end BIGINT NOT NULL,
    time_end TIMESTAMP NOT NULL,
    rev_sign_end INT NOT NULL,
    roll_min DOUBLE PRECISION,
    roll_max DOUBLE PRECISION,
    contour_wkt TEXT NOT NULL
);

ALTER TABLE cell_covering ADD COLUMN IF NOT EXISTS contour_geom geometry(Polygon, 4326)
GENERATED ALWAYS AS (ST_GeomFromText(contour_wkt)::geometry(Polygon, 4326)) STORED;

CREATE INDEX idx_cell_covering_cell_num ON cell_covering(cell_num);
CREATE INDEX idx_cell_covering_satellite_id ON cell_covering(satellite_id);
CREATE INDEX idx_cell_covering_contour_geom ON cell_covering USING GIST (contour_geom);