-- Enable extensions
CREATE EXTENSION IF NOT EXISTS postgis;
CREATE EXTENSION IF NOT EXISTS timescaledb;

-- Main object catalog (replaces 'satellites')
CREATE TABLE objects (
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL,
    norad_id INT UNIQUE NOT NULL,
    intl_designator TEXT,
    launch_date DATE,
    orbit_type TEXT
);

CREATE TABLE orbits (
    object_id INT REFERENCES objects(id),
    ts TIMESTAMPTZ NOT NULL,
    position GEOGRAPHY(POINT, 4326),
    altitude_km DOUBLE PRECISION,
    velocity_kms DOUBLE PRECISION,
    PRIMARY KEY (object_id, ts)
);
SELECT create_hypertable('orbits', 'ts', if_not_exists => TRUE);

-- Conjunction Data Messages / alerts
CREATE TABLE conjunctions (
    id SERIAL PRIMARY KEY,
    primary_object INT REFERENCES objects(id),
    secondary_object INT,
    tca TIMESTAMPTZ,  -- Time of Closest Approach
    miss_distance_km DOUBLE PRECISION,
    risk_level TEXT,
    poc DOUBLE PRECISION  -- Probability of Collision
);

-- Re-entry prediction windows and footprints
CREATE TABLE reentry_predictions (
    id SERIAL PRIMARY KEY,
    object_id INT REFERENCES objects(id),
    window_start TIMESTAMPTZ,
    window_end TIMESTAMPTZ,
    footprint GEOGRAPHY(POLYGON, 4326),
    notes TEXT
);
