CREATE TABLE IF NOT EXISTS modes (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    plan_id BIGINT NOT NULL REFERENCES plans(id) ON DELETE CASCADE,
    time_start TIMESTAMP WITH TIME ZONE,
    revolution BIGINT NOT NULL,
    type VARCHAR(20) NOT NULL,
    CONSTRAINT chk_modes_type CHECK (type IN ('SURVEY', 'DROP'))
);

CREATE INDEX IF NOT EXISTS idx_modes_plan_id ON modes(plan_id);
CREATE INDEX IF NOT EXISTS idx_modes_type ON modes(type);

CREATE TABLE IF NOT EXISTS survey_modes (
    id BIGINT PRIMARY KEY REFERENCES modes(id) ON DELETE CASCADE,
    lat DOUBLE PRECISION NOT NULL,
    long DOUBLE PRECISION NOT NULL,
    duration DOUBLE PRECISION NOT NULL,
    contour_wkt TEXT,
    contour_geometry geometry(Polygon, 4326)
        GENERATED ALWAYS AS (ST_GeomFromText(contour_wkt)::geometry(Polygon, 4326)) STORED,
    roll DOUBLE PRECISION NOT NULL
);

CREATE TABLE IF NOT EXISTS drop_modes (
    id BIGINT PRIMARY KEY REFERENCES modes(id) ON DELETE CASCADE,
    station VARCHAR(255),
    duration DOUBLE PRECISION NOT NULL
);
