CREATE EXTENSION IF NOT EXISTS postgis;

CREATE TABLE IF NOT EXISTS slot(
    slot_id BIGSERIAL PRIMARY KEY,
    satellite_id BIGINT NOT NULL,
    covering_type INT NOT NULL,
    slot_num BIGINT NOT NULL,
    cycle BIGINT NOT NULL,
    tn TIMESTAMP NOT NULL,
    tk TIMESTAMP NOT NULL,
    roll DOUBLE PRECISION,
    contour_wkt TEXT NOT NULL,
    revolution BIGINT NOT NULL,
    revolution_sign VARCHAR(4) NOT NULL CHECK (revolution_sign IN ('ASC', 'DESC'))
);

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


CREATE INDEX idx_covering_type ON slot(covering_type);
CREATE INDEX idx_satellite_id ON slot(satellite_id);
CREATE INDEX idx_revolution_sign ON slot(revolution_sign);
CREATE INDEX idx_slot_contour_geom ON slot USING GIST (contour_geom);
