-- Recréation de price_history avec support intraday (plusieurs prix par jour)
-- Le job de nettoyage consolide à J-1 en gardant uniquement le dernier fetch
DROP TABLE IF EXISTS price_history;

CREATE TABLE price_history (
    instrument_id INTEGER     NOT NULL REFERENCES instrument(id),
    fetched_at    TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    prix          NUMERIC(24, 8) NOT NULL,
    PRIMARY KEY (instrument_id, fetched_at)
);
SELECT create_hypertable('price_history', by_range('fetched_at'));
CREATE INDEX idx_price_history_instrument ON price_history(instrument_id, fetched_at DESC);

-- Cache OpenFIGI (ISIN → ticker Yahoo Finance) et CoinGecko (ticker → coin ID)
CREATE TABLE instrument_ticker_cache (
    instrument_id INTEGER     PRIMARY KEY REFERENCES instrument(id) ON DELETE CASCADE,
    ticker        TEXT        NOT NULL,
    fetched_at    TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
