-- Deduplicate any existing market_trades rows before adding the unique constraint.
-- Keeps the row with the lowest id for each (instrument_id, trade_id) pair.
DELETE FROM market_trades a USING market_trades b
WHERE a.instrument_id = b.instrument_id
  AND a.trade_id = b.trade_id
  AND a.id > b.id;

-- Allow ON CONFLICT (instrument_id, trade_id) DO NOTHING during batch inserts,
-- which safely handles overlap between live-stream and historical ingestion paths.
ALTER TABLE market_trades
    ADD CONSTRAINT uq_market_trades_instrument_trade_id
    UNIQUE (instrument_id, trade_id);

-- Ingest configuration: one row per (instrument, source) pair.
-- The service polls this table to decide what to fetch and how far back to retain.
CREATE TABLE ingest_configs (
    id               INTEGER      PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    instrument_id    INTEGER      NOT NULL REFERENCES instruments(id),
    source           TEXT         NOT NULL DEFAULT 'binance_s3',
    retention_days   INTEGER      NOT NULL DEFAULT 365,
    active           BOOLEAN      NOT NULL DEFAULT true,
    last_ingested_date DATE,
    created_at       TIMESTAMPTZ  NOT NULL DEFAULT now(),
    updated_at       TIMESTAMPTZ  NOT NULL DEFAULT now(),
    UNIQUE (instrument_id, source)
);

CREATE INDEX idx_ingest_configs_active ON ingest_configs(active) WHERE active = true;
