-- OpenNGC catalog cache (refreshed weekly)
CREATE TABLE IF NOT EXISTS catalog (
    id              TEXT PRIMARY KEY,
    name            TEXT NOT NULL,
    common_name     TEXT,
    obj_type        TEXT NOT NULL,
    ra_deg          REAL NOT NULL,
    dec_deg         REAL NOT NULL,
    ra_h            TEXT NOT NULL,
    dec_dms         TEXT NOT NULL,
    constellation   TEXT,
    size_arcmin_maj REAL,
    size_arcmin_min REAL,
    pos_angle_deg   REAL,
    mag_v           REAL,
    surface_brightness REAL,
    hubble_type     TEXT,
    messier_num     INTEGER,
    is_highlight    BOOLEAN DEFAULT FALSE,
    fov_fill_pct    REAL,
    mosaic_flag     BOOLEAN DEFAULT FALSE,
    mosaic_panels_w INTEGER DEFAULT 1,
    mosaic_panels_h INTEGER DEFAULT 1,
    difficulty      INTEGER,
    guide_star_density TEXT,
    fetched_at      INTEGER NOT NULL
);

-- Nightly precomputed visibility (refreshed each evening at sunset)
CREATE TABLE IF NOT EXISTS nightly_cache (
    catalog_id      TEXT NOT NULL,
    night_date      TEXT NOT NULL,
    max_alt_deg     REAL,
    transit_utc     TEXT,
    rise_utc        TEXT,
    set_utc         TEXT,
    best_start_utc  TEXT,
    best_end_utc    TEXT,
    usable_min      INTEGER,
    meridian_flip_utc TEXT,
    airmass_at_transit REAL,
    extinction_mag  REAL,
    moon_sep_deg    REAL,
    recommended_filter TEXT,
    visibility_json TEXT,
    PRIMARY KEY (catalog_id, night_date)
);

-- Tonight summary (single row, refreshed at sunset)
CREATE TABLE IF NOT EXISTS tonight (
    id              INTEGER PRIMARY KEY CHECK (id = 1),
    date            TEXT NOT NULL,
    astro_dusk_utc  TEXT NOT NULL,
    astro_dawn_utc  TEXT NOT NULL,
    moon_rise_utc   TEXT,
    moon_set_utc    TEXT,
    moon_illumination REAL,
    moon_phase_name TEXT,
    moon_ra_deg     REAL,
    moon_dec_deg    REAL,
    true_dark_start_utc TEXT,
    true_dark_end_utc   TEXT,
    true_dark_minutes   INTEGER,
    computed_at     INTEGER
);

-- Custom horizon profile
CREATE TABLE IF NOT EXISTS horizon (
    az_deg  INTEGER PRIMARY KEY,
    alt_deg REAL NOT NULL DEFAULT 15.0
);

-- Imaging log
CREATE TABLE IF NOT EXISTS imaging_log (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    catalog_id      TEXT NOT NULL,
    session_date    TEXT NOT NULL,
    filter_id       TEXT NOT NULL,
    integration_min INTEGER NOT NULL,
    quality         TEXT NOT NULL DEFAULT 'pending',
    notes           TEXT,
    guiding_rms     REAL,
    mean_temp_c     REAL,
    phd2_log_id     INTEGER,
    created_at      INTEGER NOT NULL DEFAULT (unixepoch())
);

-- Target gallery images
CREATE TABLE IF NOT EXISTS gallery (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    catalog_id      TEXT NOT NULL,
    log_id          INTEGER,
    filename        TEXT NOT NULL,
    caption         TEXT,
    created_at      INTEGER NOT NULL DEFAULT (unixepoch())
);

-- PHD2 guiding log analysis results
CREATE TABLE IF NOT EXISTS phd2_logs (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    session_date    TEXT NOT NULL,
    filename        TEXT NOT NULL,
    rms_total       REAL,
    rms_ra          REAL,
    rms_dec         REAL,
    peak_error      REAL,
    star_lost_count INTEGER,
    duration_min    INTEGER,
    guide_star_snr  REAL,
    created_at      INTEGER NOT NULL DEFAULT (unixepoch())
);

-- Weather cache
CREATE TABLE IF NOT EXISTS weather_cache (
    id              INTEGER PRIMARY KEY CHECK (id = 1),
    seventimer_json TEXT,
    openmeteo_json  TEXT,
    dew_point_c     REAL,
    temp_c          REAL,
    humidity_pct    REAL,
    go_nogo         TEXT,
    fetched_at      INTEGER
);

-- App settings
CREATE TABLE IF NOT EXISTS settings (
    key   TEXT PRIMARY KEY,
    value TEXT NOT NULL
);

-- Per-target planning notes (separate from session log notes)
CREATE TABLE IF NOT EXISTS target_notes (
    catalog_id  TEXT PRIMARY KEY,
    notes       TEXT NOT NULL DEFAULT '',
    updated_at  INTEGER NOT NULL DEFAULT (unixepoch())
);

-- Custom user-defined targets (manual coordinates, TLE satellites, custom objects)
CREATE TABLE IF NOT EXISTS custom_targets (
    id          TEXT PRIMARY KEY,   -- user-chosen, e.g. "MyNebula", "ISS"
    name        TEXT NOT NULL,
    obj_type    TEXT NOT NULL DEFAULT 'custom', -- custom, satellite, comet
    ra_deg      REAL,               -- NULL for TLE objects (computed live)
    dec_deg     REAL,
    tle_line1   TEXT,               -- for satellites
    tle_line2   TEXT,
    notes       TEXT,
    created_at  INTEGER NOT NULL DEFAULT (unixepoch())
);
