-- ============================================================================
-- SCRIPT: z_gen_history_data.sql
-- DESCRIPTION: Generates mock data for Zabbix history and trends tables.
--              Creates a dummy host and items if they don't exist.
-- ============================================================================

DO $$
DECLARE
    v_hostid bigint := 900001;
    v_groupid bigint := 900001;
    v_interfaceid bigint := 900001;
    v_itemid_start bigint := 900001;
    v_start_time integer := extract(epoch from (now() - interval '7 days'))::integer;
    v_end_time integer := extract(epoch from now())::integer;
    i integer;
BEGIN
    -- 1. CREATE DUMMY STRUCTURES
    -- Host Group
    INSERT INTO hstgrp (groupid, name, uuid, type)
    VALUES (v_groupid, 'Partition Test Group', 'df77189c49034553999973d8e0500001', 0)
    ON CONFLICT DO NOTHING;

    -- Host
    INSERT INTO hosts (hostid, host, name, status, uuid)
    VALUES (v_hostid, 'partition-test-host', 'Partition Test Host', 0, 'df77189c49034553999973d8e0500002')
    ON CONFLICT DO NOTHING;

    -- Interface
    INSERT INTO interface (interfaceid, hostid, main, type, useip, ip, dns, port)
    VALUES (v_interfaceid, v_hostid, 1, 1, 1, '127.0.0.1', '', '10050')
    ON CONFLICT DO NOTHING;

    -- 2. CREATE DUMMY ITEMS AND GENERATE HISTORY
    
    -- Item 1: Numeric Float (HISTORY)
    INSERT INTO items (itemid, hostid, interfaceid, name, key_, type, value_type, delay, uuid)
    VALUES (v_itemid_start + 1, v_hostid, v_interfaceid, 'Test Float Item', 'test.float', 0, 0, '1m', 'df77189c49034553999973d8e0500003');

    INSERT INTO history (itemid, clock, value, ns)
    SELECT 
        v_itemid_start + 1,
        ts,
        random() * 100,
        0
    FROM generate_series(v_start_time, v_end_time, 60) AS ts;

    INSERT INTO trends (itemid, clock, num, value_min, value_avg, value_max)
    SELECT
        v_itemid_start + 1,
        (ts / 3600) * 3600, -- Hourly truncation
        60,
        0,
        50,
        100
    FROM generate_series(v_start_time, v_end_time, 3600) AS ts;

    -- Item 2: Numeric Unsigned (HISTORY_UINT)
    INSERT INTO items (itemid, hostid, interfaceid, name, key_, type, value_type, delay, uuid)
    VALUES (v_itemid_start + 2, v_hostid, v_interfaceid, 'Test Uint Item', 'test.uint', 0, 3, '1m', 'df77189c49034553999973d8e0500004');

    INSERT INTO history_uint (itemid, clock, value, ns)
    SELECT 
        v_itemid_start + 2,
        ts,
        (random() * 1000)::integer,
        0
    FROM generate_series(v_start_time, v_end_time, 60) AS ts;

    INSERT INTO trends_uint (itemid, clock, num, value_min, value_avg, value_max)
    SELECT
        v_itemid_start + 2,
        (ts / 3600) * 3600,
        60,
        0,
        500,
        1000
    FROM generate_series(v_start_time, v_end_time, 3600) AS ts;

    -- Item 3: Character (HISTORY_STR)
    INSERT INTO items (itemid, hostid, interfaceid, name, key_, type, value_type, delay, uuid)
    VALUES (v_itemid_start + 3, v_hostid, v_interfaceid, 'Test Str Item', 'test.str', 0, 1, '1m', 'df77189c49034553999973d8e0500005');

    INSERT INTO history_str (itemid, clock, value, ns)
    SELECT 
        v_itemid_start + 3,
        ts,
        'test_value_' || ts,
        0
    FROM generate_series(v_start_time, v_end_time, 300) AS ts; -- Every 5 mins

END $$;
