-- HamLog initial schema
-- QSO table: core of the logbook. Field names stay close to ADIF.

CREATE TABLE IF NOT EXISTS qso (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    callsign        TEXT    NOT NULL,
    qso_date        TEXT    NOT NULL,   -- ISO 8601 UTC: YYYY-MM-DDTHH:MM:SSZ
    band            TEXT    NOT NULL,   -- e.g. 20m, 40m, 2m
    mode            TEXT    NOT NULL,   -- e.g. SSB, CW, FT8
    freq_hz         INTEGER,            -- frequency in Hz (integer, avoids floats)
    rst_sent        TEXT,
    rst_rcvd        TEXT,
    name            TEXT,
    qth             TEXT,
    grid            TEXT,
    country         TEXT,
    dxcc            INTEGER,
    cont            TEXT,
    cqz             INTEGER,
    ituz            INTEGER,
    iota            TEXT,
    sota_ref        TEXT,
    pota_ref        TEXT,
    -- Operator context (multi-callsign / multi-location)
    station_callsign TEXT,
    operator        TEXT,
    my_grid         TEXT,
    my_country      TEXT,
    my_sota_ref     TEXT,
    my_pota_ref     TEXT,
    -- Misc
    tx_pwr          REAL,
    comment         TEXT,
    notes           TEXT,
    qsl_sent        TEXT DEFAULT 'N',
    qsl_rcvd        TEXT DEFAULT 'N',
    lotw_sent       TEXT DEFAULT 'N',
    lotw_rcvd       TEXT DEFAULT 'N',
    eqsl_sent       TEXT DEFAULT 'N',
    eqsl_rcvd       TEXT DEFAULT 'N',
    created_at      TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
    updated_at      TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
);

CREATE INDEX IF NOT EXISTS idx_qso_callsign  ON qso(callsign);
CREATE INDEX IF NOT EXISTS idx_qso_date      ON qso(qso_date DESC);
CREATE INDEX IF NOT EXISTS idx_qso_band_mode ON qso(band, mode);
CREATE INDEX IF NOT EXISTS idx_qso_dxcc      ON qso(dxcc);
CREATE INDEX IF NOT EXISTS idx_qso_grid      ON qso(grid);
CREATE INDEX IF NOT EXISTS idx_qso_station   ON qso(station_callsign);
