-- =============================================================
-- V010__indexes.sql
-- B-tree indexy pro časté dotazy (plán, telemetrie, ceny, audit, EV, režimy).
-- Pozn.: idx_ev_session_active na (charger_id, session_end) je ve V006;
-- zde idx_ev_session_site_active doplňuje vyhledávání aktivní session podle site.
-- =============================================================

-- Planning (control exporter hledá aktivní plán pro aktuální slot)
CREATE INDEX IF NOT EXISTS idx_planning_run_site_status
    ON ems.planning_run (site_id, status, created_at DESC);

CREATE INDEX IF NOT EXISTS idx_planning_interval_run_start
    ON ems.planning_interval (run_id, interval_start);

-- Telemetrie (dashboard čte poslední hodnoty)
CREATE INDEX IF NOT EXISTS idx_telemetry_inverter_site_time
    ON ems.telemetry_inverter (site_id, measured_at DESC);

CREATE INDEX IF NOT EXISTS idx_telemetry_ev_site_time
    ON ems.telemetry_ev_charger (site_id, measured_at DESC);

CREATE INDEX IF NOT EXISTS idx_telemetry_hp_site_time
    ON ems.telemetry_heat_pump (site_id, measured_at DESC);

-- Market prices (forecast + planning čte ceny pro horizont)
CREATE INDEX IF NOT EXISTS idx_market_price_source_start
    ON ems.market_interval_price (market_source, interval_start);

-- Audit (dashboard čte dnešní data)
CREATE INDEX IF NOT EXISTS idx_audit_interval_site_start
    ON ems.audit_interval (site_id, interval_start DESC);

-- EV session (control exporter + UI hledá aktivní session podle lokality)
CREATE INDEX IF NOT EXISTS idx_ev_session_site_active
    ON ems.ev_session (site_id, session_end)
    WHERE session_end IS NULL;

-- Operating mode log
CREATE INDEX IF NOT EXISTS idx_mode_log_site_time
    ON ems.site_operating_mode_log (site_id, activated_at DESC);
