DROP VIEW IF EXISTS mv_reports_ucepsa_demo.machine_state_latest_multi;
DROP VIEW IF EXISTS mv_reports_ucepsa_demo.energy_3phase_latest;
DROP VIEW IF EXISTS mv_reports_ucepsa_demo.energy_3phase_timeseries;

CREATE VIEW mv_reports_ucepsa_demo.energy_3phase_timeseries AS
SELECT
    r.ingest_ts AS ts,
    r.ingest_ts,
    r.tenant,
    r.site,
    r.asset,
    r.machine_id,
    EXTRACT(EPOCH FROM (now() - r.ingest_ts))::double precision AS age_s,
    r.comm_ok,

    CASE
        WHEN lower(r.payload_json ->> 'sample_valid') IN ('true','t','1','yes') THEN true
        WHEN lower(r.payload_json ->> 'sample_valid') IN ('false','f','0','no') THEN false
        ELSE NULL
    END AS sample_valid,

    r.payload_json ->> 'energy_meter_model' AS energy_meter_model,
    r.payload_json ->> 'energy_map' AS energy_map,

    r.voltage_l1_v,
    NULLIF(r.payload_json ->> 'voltage_l2_v','')::double precision AS voltage_l2_v,
    NULLIF(r.payload_json ->> 'voltage_l3_v','')::double precision AS voltage_l3_v,

    r.current_l1_a,
    NULLIF(r.payload_json ->> 'current_l2_a','')::double precision AS current_l2_a,
    NULLIF(r.payload_json ->> 'current_l3_a','')::double precision AS current_l3_a,

    NULLIF(r.payload_json ->> 'active_power_l1_w','')::double precision AS active_power_l1_w,
    NULLIF(r.payload_json ->> 'active_power_l2_w','')::double precision AS active_power_l2_w,
    NULLIF(r.payload_json ->> 'active_power_l3_w','')::double precision AS active_power_l3_w,

    COALESCE(
        NULLIF(r.payload_json ->> 'active_power_w','')::double precision,
        r.power_total_kw * 1000.0
    ) AS active_power_w,

    r.power_total_kw,

    NULLIF(r.payload_json ->> 'apparent_power_va','')::double precision AS apparent_power_va,
    NULLIF(r.payload_json ->> 'reactive_power_var','')::double precision AS reactive_power_var,
    NULLIF(r.payload_json ->> 'power_factor','')::double precision AS power_factor,

    r.frequency_hz,
    r.import_kwh,
    NULLIF(r.payload_json ->> 'export_kwh','')::double precision AS export_kwh,
    NULLIF(r.payload_json ->> 'total_active_energy_kwh','')::double precision AS total_active_energy_kwh,

    r.payload_json
FROM mv_hot.edge_oee_eastron_readings r;

CREATE VIEW mv_reports_ucepsa_demo.energy_3phase_latest AS
SELECT DISTINCT ON (machine_id)
    *
FROM mv_reports_ucepsa_demo.energy_3phase_timeseries
WHERE site = 'ucepsa_onpremise'
ORDER BY machine_id, ingest_ts DESC;

CREATE VIEW mv_reports_ucepsa_demo.machine_state_latest_multi AS
SELECT
    l.ingest_ts,
    l.ts,
    l.tenant,
    l.site,
    l.asset,
    l.machine_id,
    l.age_s,
    l.comm_ok,
    l.sample_valid,
    l.energy_map,

    CASE
        WHEN lower(l.payload_json ->> 'machine_running') IN ('true','t','1','yes') THEN true
        WHEN lower(l.payload_json ->> 'machine_running') IN ('false','f','0','no') THEN false
        ELSE false
    END AS machine_running,

    COALESCE(NULLIF(l.payload_json ->> 'cycle_pulse_count','')::numeric::bigint, 0) AS cycle_pulse_count,
    COALESCE(NULLIF(l.payload_json ->> 'cycle_rate_ppm','')::double precision, 0.0) AS cycle_rate_ppm,
    NULLIF(l.payload_json ->> 'wise_cycle_counter_raw','')::numeric::bigint AS wise_cycle_counter_raw,

    CASE
        WHEN lower(l.payload_json ->> 'cycle_input_state') IN ('true','t','1','yes') THEN true
        WHEN lower(l.payload_json ->> 'cycle_input_state') IN ('false','f','0','no') THEN false
        ELSE NULL
    END AS cycle_input_state,

    l.payload_json -> 'digital_inputs' AS digital_inputs,

    l.voltage_l1_v,
    l.voltage_l2_v,
    l.voltage_l3_v,

    l.current_l1_a,
    l.current_l2_a,
    l.current_l3_a,

    l.active_power_l1_w,
    l.active_power_l2_w,
    l.active_power_l3_w,

    l.power_total_kw,
    l.frequency_hz,
    l.import_kwh,
    l.payload_json
FROM mv_reports_ucepsa_demo.energy_3phase_latest l;

GRANT SELECT ON mv_reports_ucepsa_demo.energy_3phase_timeseries TO grafana_ucepsa_ro;
GRANT SELECT ON mv_reports_ucepsa_demo.energy_3phase_latest TO grafana_ucepsa_ro;
GRANT SELECT ON mv_reports_ucepsa_demo.machine_state_latest_multi TO grafana_ucepsa_ro;
