-- Create 60 MQTT agents for OpenRemote using PL/pgSQL block
-- Realm master: parent_id = '2LtWTTd29uPZLbuWMWUxBf'
-- Realm smartcity: parent_id = 'e174aad5c7b5489e8b2efe'

CREATE EXTENSION IF NOT EXISTS pgcrypto;

DO $$
DECLARE
    sensor_types text[] := ARRAY['airquality', 'traffic', 'parking', 'noise', 'weather', 'waterquality'];
    realm_rec RECORD;
    sensor_type text;
    i integer;
    new_id text;
    new_name text;
    new_topic text;
    parent_id text;
    realm_name text;
BEGIN
    -- Delete existing MQTT agents
    DELETE FROM asset WHERE type = 'urn:openremote:agent:mqtt';
    
    -- Loop over realms
    FOR realm_rec IN SELECT * FROM (VALUES ('master', '2LtWTTd29uPZLbuWMWUxBf'), ('smartcity', 'e174aad5c7b5489e8b2efe')) AS t(realm, parent) LOOP
        realm_name := realm_rec.realm;
        parent_id := realm_rec.parent;
        
        FOREACH sensor_type IN ARRAY sensor_types LOOP
            FOR i IN 1..5 LOOP
                new_id := LEFT(REPLACE(gen_random_uuid()::text, '-', ''), 22);
                new_name := 'MQTT-Agent-' || sensor_type || '-' || i;
                new_topic := 'smartcity/' || sensor_type || '/' || i;
                
                INSERT INTO asset (id, name, type, realm, parent_id, created_on, access_public_read, version, attributes)
                VALUES (
                    new_id,
                    new_name,
                    'urn:openremote:agent:mqtt',
                    realm_name,
                    parent_id,
                    NOW(),
                    false,
                    1,
                    jsonb_build_object(
                        'name', jsonb_build_object('type', 'String', 'value', new_name),
                        'agentLink', jsonb_build_object(
                            'type', 'Property',
                            'value', jsonb_build_object(
                                'type', 'mqtt',
                                'brokerUrl', 'tcp://openremote-manager-1:1883',
                                'topicFilter', new_topic,
                                'username', '',
                                'password', '',
                                'enabled', true
                            )
                        ),
                        'sensorType', jsonb_build_object('type', 'String', 'value', sensor_type),
                        'location', jsonb_build_object(
                            'type', 'GeoJSONPoint',
                            'value', jsonb_build_object(
                                'type', 'Point',
                                'coordinates', jsonb_build_array(
                                    14.6091 + (random() - 0.5) * 0.1,
                                    -61.2155 + (random() - 0.5) * 0.1
                                )
                            )
                        )
                    )
                );
            END LOOP;
        END LOOP;
    END LOOP;
END $$;

-- Verify insertion
SELECT realm, COUNT(*) as agent_count 
FROM asset 
WHERE type = 'urn:openremote:agent:mqtt' 
GROUP BY realm;
