-- =============================================================
-- R__053_vw_audit_summary.sql
-- EMS Platform – přehledové views pro audit a dashboard
-- Repeatable migration
-- =============================================================

-- Denní souhrn per lokalita
CREATE OR REPLACE VIEW ems.vw_audit_daily AS
SELECT
    site_id,
    date_trunc('day', interval_start AT TIME ZONE 'Europe/Prague') AS day_local,
    COUNT(*)                                                         AS interval_count,
    -- Energie (kWh = W × 15min / 60 = W / 4 / 1000)
    ROUND(SUM(GREATEST(actual_grid_power_w, 0))::NUMERIC / 4000, 3) AS import_kwh,
    ROUND(SUM(ABS(LEAST(actual_grid_power_w, 0)))::NUMERIC / 4000, 3) AS export_kwh,
    ROUND(SUM(GREATEST(actual_pv_power_w, 0))::NUMERIC / 4000, 3)  AS pv_kwh,
    ROUND(SUM(GREATEST(actual_load_power_w, 0))::NUMERIC / 4000, 3) AS load_kwh,
    ROUND(SUM(GREATEST(actual_ev_power_w, 0))::NUMERIC / 4000, 3)  AS ev_kwh,
    ROUND(SUM(GREATEST(actual_heat_pump_power_w, 0))::NUMERIC / 4000, 3) AS hp_kwh,
    -- Náklady
    ROUND(SUM(actual_cost_czk), 2)                                  AS actual_cost_czk,
    ROUND(SUM(deviation_cost_czk), 2)                               AS total_deviation_czk,
    -- Počet intervalů s velkými odchylkami (>1kW)
    COUNT(*) FILTER (WHERE ABS(deviation_grid_w) > 1000)            AS high_deviation_count,
    ROUND(SUM(green_bonus_czk), 4)                                  AS green_bonus_czk,
    ROUND(COALESCE(SUM(actual_cost_czk), 0) * -1 + COALESCE(SUM(green_bonus_czk), 0), 2) AS total_revenue_czk
FROM ems.audit_interval
GROUP BY site_id, date_trunc('day', interval_start AT TIME ZONE 'Europe/Prague');

COMMENT ON VIEW ems.vw_audit_daily IS
'Denní souhrn auditu per lokalita. Energie v kWh, náklady v Kč.
total_revenue_czk = -actual_cost_czk + green_bonus_czk (export/síť vs. bonus).
Používat pro dashboard denního přehledu a reporty.';

-- ============================================================

-- Týdenní souhrn
CREATE OR REPLACE VIEW ems.vw_audit_weekly AS
SELECT
    site_id,
    date_trunc('week', interval_start AT TIME ZONE 'Europe/Prague') AS week_local,
    ROUND(SUM(GREATEST(actual_grid_power_w, 0))::NUMERIC / 4000, 1)  AS import_kwh,
    ROUND(SUM(ABS(LEAST(actual_grid_power_w, 0)))::NUMERIC / 4000, 1) AS export_kwh,
    ROUND(SUM(GREATEST(actual_pv_power_w, 0))::NUMERIC / 4000, 1)   AS pv_kwh,
    ROUND(SUM(actual_cost_czk), 0)                                   AS actual_cost_czk
FROM ems.audit_interval
GROUP BY site_id, date_trunc('week', interval_start AT TIME ZONE 'Europe/Prague');

COMMENT ON VIEW ems.vw_audit_weekly IS
'Týdenní souhrn auditu per lokalita.';

-- ============================================================

-- Aktuální den – hourly breakdown pro dashboard graf
CREATE OR REPLACE VIEW ems.vw_audit_today_hourly AS
SELECT
    site_id,
    date_trunc('hour', interval_start AT TIME ZONE 'Europe/Prague') AS hour_local,
    ROUND(AVG(actual_pv_power_w)::NUMERIC / 1000, 2)               AS avg_pv_kw,
    ROUND(AVG(actual_battery_power_w)::NUMERIC / 1000, 2)          AS avg_battery_kw,
    ROUND(AVG(actual_grid_power_w)::NUMERIC / 1000, 2)             AS avg_grid_kw,
    ROUND(AVG(actual_load_power_w)::NUMERIC / 1000, 2)             AS avg_load_kw,
    ROUND(AVG(actual_battery_soc_pct), 1)                          AS avg_soc_pct,
    ROUND(SUM(actual_cost_czk), 2)                                 AS cost_czk
FROM ems.audit_interval
WHERE interval_start >= date_trunc('day', now() AT TIME ZONE 'Europe/Prague')
  AND interval_start < date_trunc('day', now() AT TIME ZONE 'Europe/Prague') + INTERVAL '1 day'
GROUP BY site_id, date_trunc('hour', interval_start AT TIME ZONE 'Europe/Prague')
ORDER BY hour_local;

COMMENT ON VIEW ems.vw_audit_today_hourly IS
'Hodinový přehled dnešního dne pro dashboard graf výkonů a nákladů.';
