-- ============================================================================
-- Migration 033: MVP Seed Real Entities
-- Purpose: Seed real agents, microDAOs, and assignments for production MVP
-- Date: 2025-11-30
-- Status: MVP Recovery
-- ============================================================================

-- ============================================================================
-- 0. CREATE USERS IF NEEDED (for owner_user_id reference)
-- ============================================================================

-- Ensure admin user exists
INSERT INTO users (id, email, display_name, created_at)
SELECT 
    '00000000-0000-0000-0000-000000000001'::UUID,
    'admin@daarion.xyz',
    'DAARION Admin',
    NOW()
WHERE NOT EXISTS (SELECT 1 FROM users WHERE email = 'admin@daarion.xyz')
ON CONFLICT (id) DO NOTHING;

-- ============================================================================
-- 1. DAIS IDENTITIES - Core Agents
-- ============================================================================

-- City Governance Agents
INSERT INTO dais_identities (id, did, default_email, trust_level, metadata)
VALUES 
    ('dais-daarwizz', 'did:daarion:daarwizz', 'daarwizz@daarion.city', 'operator', '{"role": "city_mayor", "type": "ai-agent"}'::jsonb),
    ('dais-dario', 'did:daarion:dario', 'dario@daarion.city', 'operator', '{"role": "city_community", "type": "ai-agent"}'::jsonb),
    ('dais-daria', 'did:daarion:daria', 'daria@daarion.city', 'operator', '{"role": "city_support", "type": "ai-agent"}'::jsonb)
ON CONFLICT (id) DO UPDATE SET 
    trust_level = EXCLUDED.trust_level,
    metadata = EXCLUDED.metadata;

-- District Lead Agents
INSERT INTO dais_identities (id, did, default_email, trust_level, metadata)
VALUES 
    ('dais-soul', 'did:daarion:soul', 'soul@daarion.city', 'operator', '{"role": "district_lead", "district": "soul-retreat", "type": "ai-agent"}'::jsonb),
    ('dais-helion', 'did:daarion:helion', 'helion@daarion.city', 'operator', '{"role": "district_lead", "district": "energyunion", "type": "ai-agent"}'::jsonb),
    ('dais-greenfood', 'did:daarion:greenfood', 'greenfood@daarion.city', 'operator', '{"role": "district_lead", "district": "greenfood", "type": "ai-agent"}'::jsonb)
ON CONFLICT (id) DO UPDATE SET 
    trust_level = EXCLUDED.trust_level,
    metadata = EXCLUDED.metadata;

-- District Support Agents
INSERT INTO dais_identities (id, did, default_email, trust_level, metadata)
VALUES 
    ('dais-spirit', 'did:daarion:spirit', 'spirit@daarion.city', 'verified', '{"role": "guidance_agent", "district": "soul-retreat", "type": "ai-agent"}'::jsonb),
    ('dais-logic', 'did:daarion:logic', 'logic@daarion.city', 'verified', '{"role": "info_agent", "district": "soul-retreat", "type": "ai-agent"}'::jsonb),
    ('dais-energia', 'did:daarion:energia', 'energia@daarion.city', 'verified', '{"role": "energy_agent", "district": "energyunion", "type": "ai-agent"}'::jsonb)
ON CONFLICT (id) DO UPDATE SET 
    trust_level = EXCLUDED.trust_level,
    metadata = EXCLUDED.metadata;

-- Other Core Agents
INSERT INTO dais_identities (id, did, default_email, trust_level, metadata)
VALUES 
    ('dais-clan', 'did:daarion:clan', 'clan@daarion.city', 'orchestrator', '{"role": "orchestrator", "type": "ai-agent"}'::jsonb),
    ('dais-druid', 'did:daarion:druid', 'druid@daarion.city', 'verified', '{"role": "researcher", "type": "ai-agent"}'::jsonb),
    ('dais-eonarch', 'did:daarion:eonarch', 'eonarch@daarion.city', 'verified', '{"role": "personal_agent", "type": "ai-agent"}'::jsonb),
    ('dais-yaromir', 'did:daarion:yaromir', 'yaromir@daarion.city', 'verified', '{"role": "personal_agent", "type": "ai-agent"}'::jsonb)
ON CONFLICT (id) DO UPDATE SET 
    trust_level = EXCLUDED.trust_level,
    metadata = EXCLUDED.metadata;

-- ============================================================================
-- 2. AGENTS - Core Agents
-- ============================================================================

-- Get admin user ID
DO $$
DECLARE
    admin_user_id UUID;
BEGIN
    SELECT id INTO admin_user_id FROM users WHERE email = 'admin@daarion.xyz' LIMIT 1;
    IF admin_user_id IS NULL THEN
        SELECT id INTO admin_user_id FROM users LIMIT 1;
    END IF;

    -- City Governance Agents
    INSERT INTO agents (external_id, name, kind, owner_user_id, description, is_active, gov_level, status, dais_identity_id, agent_role, agent_service_scope, avatar_url)
    VALUES 
        ('agent:daarwizz', 'DAARWIZZ', 'governance', admin_user_id, 'Мер DAARION.city — головний агент міста', true, 'city_governance', 'active', 'dais-daarwizz', 'orchestrator', 'city', '/avatars/daarwizz.png'),
        ('agent:dario', 'DARIO', 'community', admin_user_id, 'Комʼюніті-менеджер DAARION.city', true, 'city_governance', 'active', 'dais-dario', 'orchestrator', 'city', '/avatars/dario.png'),
        ('agent:daria', 'DARIA', 'support', admin_user_id, 'Технічна підтримка DAARION.city', true, 'city_governance', 'active', 'dais-daria', 'orchestrator', 'city', '/avatars/daria.png')
    ON CONFLICT (external_id) DO UPDATE SET 
        gov_level = EXCLUDED.gov_level,
        status = EXCLUDED.status,
        dais_identity_id = EXCLUDED.dais_identity_id,
        agent_role = EXCLUDED.agent_role,
        agent_service_scope = EXCLUDED.agent_service_scope,
        description = EXCLUDED.description;

    -- District Lead Agents
    INSERT INTO agents (external_id, name, kind, owner_user_id, description, is_active, gov_level, status, dais_identity_id, agent_role, agent_service_scope, avatar_url)
    VALUES 
        ('agent:soul', 'SOUL', 'district_lead', admin_user_id, 'District Lead Agent — SOUL Retreat (Wellness/Metahuman)', true, 'district_lead', 'active', 'dais-soul', 'orchestrator', 'district', '/avatars/soul.png'),
        ('agent:helion', 'Helion', 'district_lead', admin_user_id, 'District Lead Agent — ENERGYUNION (DePIN/Energy/Compute)', true, 'district_lead', 'active', 'dais-helion', 'orchestrator', 'district', '/avatars/helion.png'),
        ('agent:greenfood', 'GREENFOOD', 'district_lead', admin_user_id, 'District Lead Agent — GREENFOOD (IndustryOps/Supply-Chain)', true, 'district_lead', 'active', 'dais-greenfood', 'orchestrator', 'district', '/avatars/greenfood.png')
    ON CONFLICT (external_id) DO UPDATE SET 
        gov_level = EXCLUDED.gov_level,
        status = EXCLUDED.status,
        dais_identity_id = EXCLUDED.dais_identity_id,
        agent_role = EXCLUDED.agent_role,
        agent_service_scope = EXCLUDED.agent_service_scope,
        description = EXCLUDED.description;

    -- District Support Agents
    INSERT INTO agents (external_id, name, kind, owner_user_id, description, is_active, gov_level, status, dais_identity_id, agent_role, agent_service_scope, avatar_url)
    VALUES 
        ('agent:spirit', 'Spirit', 'guidance', admin_user_id, 'AI Guidance Agent — медитації, практики, підтримка (SOUL)', true, 'core_team', 'active', 'dais-spirit', 'regular', 'district', '/avatars/spirit.png'),
        ('agent:logic', 'Logic', 'info', admin_user_id, 'AI Information Agent — розклад, інфо, форми (SOUL)', true, 'core_team', 'active', 'dais-logic', 'regular', 'district', '/avatars/logic.png'),
        ('agent:energia', 'Energia', 'energy', admin_user_id, 'Energy Agent — телеметрія та аналітика (ENERGYUNION)', true, 'core_team', 'active', 'dais-energia', 'regular', 'district', '/avatars/energia.png')
    ON CONFLICT (external_id) DO UPDATE SET 
        gov_level = EXCLUDED.gov_level,
        status = EXCLUDED.status,
        dais_identity_id = EXCLUDED.dais_identity_id,
        agent_role = EXCLUDED.agent_role,
        agent_service_scope = EXCLUDED.agent_service_scope,
        description = EXCLUDED.description;

    -- Other Core Agents
    INSERT INTO agents (external_id, name, kind, owner_user_id, description, is_active, gov_level, status, dais_identity_id, agent_role, avatar_url)
    VALUES 
        ('agent:clan', 'CLAN', 'orchestrator', admin_user_id, 'Orchestrator для clan-типу microDAO', true, 'orchestrator', 'active', 'dais-clan', 'orchestrator', '/avatars/clan.png'),
        ('agent:druid', 'DRUID', 'researcher', admin_user_id, 'Research & Development Agent', true, 'worker', 'active', 'dais-druid', 'regular', '/avatars/druid.png'),
        ('agent:eonarch', 'EONARCH', 'personal', admin_user_id, 'Personal AI Assistant', true, 'personal', 'active', 'dais-eonarch', 'regular', '/avatars/eonarch.png'),
        ('agent:yaromir', 'YAROMIR', 'personal', admin_user_id, 'Personal AI Assistant', true, 'personal', 'active', 'dais-yaromir', 'regular', '/avatars/yaromir.png')
    ON CONFLICT (external_id) DO UPDATE SET 
        gov_level = EXCLUDED.gov_level,
        status = EXCLUDED.status,
        dais_identity_id = EXCLUDED.dais_identity_id,
        agent_role = EXCLUDED.agent_role,
        description = EXCLUDED.description;
END $$;

-- ============================================================================
-- 3. MICRODAOS - Core DAOs
-- ============================================================================

DO $$
DECLARE
    admin_user_id UUID;
    daarion_id UUID;
    energyunion_id UUID;
    greenfood_id UUID;
    soul_retreat_id UUID;
BEGIN
    SELECT id INTO admin_user_id FROM users WHERE email = 'admin@daarion.xyz' LIMIT 1;
    IF admin_user_id IS NULL THEN
        SELECT id INTO admin_user_id FROM users LIMIT 1;
    END IF;

    -- Root MicroDAO (City)
    INSERT INTO microdaos (external_id, slug, name, description, owner_user_id, is_active, dao_type)
    VALUES ('microdao:daarion', 'daarion', 'DAARION City', 'Root MicroDAO — DAARION.city', admin_user_id, true, 'root')
    ON CONFLICT (slug) DO UPDATE SET 
        dao_type = 'root',
        description = EXCLUDED.description
    RETURNING id INTO daarion_id;
    
    IF daarion_id IS NULL THEN
        SELECT id INTO daarion_id FROM microdaos WHERE slug = 'daarion';
    END IF;

    -- District: ENERGYUNION
    INSERT INTO microdaos (external_id, slug, name, description, owner_user_id, is_active, dao_type)
    VALUES ('microdao:energyunion', 'energyunion', 'ENERGYUNION', 'DePIN / Energy Grid / Compute Grid / AI District', admin_user_id, true, 'district')
    ON CONFLICT (slug) DO UPDATE SET 
        dao_type = 'district',
        description = EXCLUDED.description
    RETURNING id INTO energyunion_id;

    IF energyunion_id IS NULL THEN
        SELECT id INTO energyunion_id FROM microdaos WHERE slug = 'energyunion';
    END IF;

    -- District: GREENFOOD
    INSERT INTO microdaos (external_id, slug, name, description, owner_user_id, is_active, dao_type)
    VALUES ('microdao:greenfood', 'greenfood', 'GREENFOOD', 'IndustryOps / Supply-Chain District', admin_user_id, true, 'district')
    ON CONFLICT (slug) DO UPDATE SET 
        dao_type = 'district',
        description = EXCLUDED.description
    RETURNING id INTO greenfood_id;

    IF greenfood_id IS NULL THEN
        SELECT id INTO greenfood_id FROM microdaos WHERE slug = 'greenfood';
    END IF;

    -- District: SOUL Retreat
    INSERT INTO microdaos (external_id, slug, name, description, owner_user_id, is_active, dao_type)
    VALUES ('microdao:soul-retreat', 'soul-retreat', 'SOUL Retreat', 'Wellness / Retreat / Metahuman District', admin_user_id, true, 'district')
    ON CONFLICT (slug) DO UPDATE SET 
        dao_type = 'district',
        description = EXCLUDED.description
    RETURNING id INTO soul_retreat_id;

    IF soul_retreat_id IS NULL THEN
        SELECT id INTO soul_retreat_id FROM microdaos WHERE slug = 'soul-retreat';
    END IF;

    -- Update orchestrator references
    UPDATE microdaos SET primary_orchestrator_agent_id = (SELECT id::TEXT FROM agents WHERE external_id = 'agent:daarwizz' LIMIT 1)
    WHERE slug = 'daarion';

    UPDATE microdaos SET primary_orchestrator_agent_id = (SELECT id::TEXT FROM agents WHERE external_id = 'agent:helion' LIMIT 1)
    WHERE slug = 'energyunion';

    UPDATE microdaos SET primary_orchestrator_agent_id = (SELECT id::TEXT FROM agents WHERE external_id = 'agent:greenfood' LIMIT 1)
    WHERE slug = 'greenfood';

    UPDATE microdaos SET primary_orchestrator_agent_id = (SELECT id::TEXT FROM agents WHERE external_id = 'agent:soul' LIMIT 1)
    WHERE slug = 'soul-retreat';

    -- Update agents' home_microdao_id
    UPDATE agents SET home_microdao_id = daarion_id::TEXT 
    WHERE external_id IN ('agent:daarwizz', 'agent:dario', 'agent:daria');

    UPDATE agents SET home_microdao_id = energyunion_id::TEXT 
    WHERE external_id IN ('agent:helion', 'agent:energia');

    UPDATE agents SET home_microdao_id = greenfood_id::TEXT 
    WHERE external_id = 'agent:greenfood';

    UPDATE agents SET home_microdao_id = soul_retreat_id::TEXT 
    WHERE external_id IN ('agent:soul', 'agent:spirit', 'agent:logic');
END $$;

-- ============================================================================
-- 4. AGENT ASSIGNMENTS
-- ============================================================================

-- City Governance Assignments
INSERT INTO agent_assignments (agent_id, target_microdao_id, scope, role, metadata)
SELECT 
    a.id::TEXT,
    m.id::TEXT,
    'city'::assignment_scope,
    'city_governance',
    '{"power": ["administrative", "moderation", "protocol"]}'::jsonb
FROM agents a, microdaos m
WHERE a.external_id = 'agent:daarwizz' AND m.slug = 'daarion'
ON CONFLICT (agent_id, target_microdao_id, role) DO NOTHING;

INSERT INTO agent_assignments (agent_id, target_microdao_id, scope, role, metadata)
SELECT 
    a.id::TEXT,
    m.id::TEXT,
    'city'::assignment_scope,
    'city_community',
    '{"power": ["moderation", "execution"]}'::jsonb
FROM agents a, microdaos m
WHERE a.external_id = 'agent:dario' AND m.slug = 'daarion'
ON CONFLICT (agent_id, target_microdao_id, role) DO NOTHING;

INSERT INTO agent_assignments (agent_id, target_microdao_id, scope, role, metadata)
SELECT 
    a.id::TEXT,
    m.id::TEXT,
    'city'::assignment_scope,
    'city_support',
    '{"power": ["moderation", "identity"]}'::jsonb
FROM agents a, microdaos m
WHERE a.external_id = 'agent:daria' AND m.slug = 'daarion'
ON CONFLICT (agent_id, target_microdao_id, role) DO NOTHING;

-- District Lead Assignments
INSERT INTO agent_assignments (agent_id, target_microdao_id, scope, role, metadata)
SELECT 
    a.id::TEXT,
    m.id::TEXT,
    'district'::assignment_scope,
    'district_lead',
    '{"power": ["administrative", "moderation", "district"]}'::jsonb
FROM agents a, microdaos m
WHERE a.external_id = 'agent:helion' AND m.slug = 'energyunion'
ON CONFLICT (agent_id, target_microdao_id, role) DO NOTHING;

INSERT INTO agent_assignments (agent_id, target_microdao_id, scope, role, metadata)
SELECT 
    a.id::TEXT,
    m.id::TEXT,
    'district'::assignment_scope,
    'district_lead',
    '{"power": ["administrative", "moderation", "district"]}'::jsonb
FROM agents a, microdaos m
WHERE a.external_id = 'agent:greenfood' AND m.slug = 'greenfood'
ON CONFLICT (agent_id, target_microdao_id, role) DO NOTHING;

INSERT INTO agent_assignments (agent_id, target_microdao_id, scope, role, metadata)
SELECT 
    a.id::TEXT,
    m.id::TEXT,
    'district'::assignment_scope,
    'district_lead',
    '{"power": ["administrative", "moderation", "district"]}'::jsonb
FROM agents a, microdaos m
WHERE a.external_id = 'agent:soul' AND m.slug = 'soul-retreat'
ON CONFLICT (agent_id, target_microdao_id, role) DO NOTHING;

-- District Core-team Assignments
INSERT INTO agent_assignments (agent_id, target_microdao_id, scope, role, metadata)
SELECT 
    a.id::TEXT,
    m.id::TEXT,
    'district'::assignment_scope,
    'core_team',
    '{"power": ["execution"], "specialty": "guidance"}'::jsonb
FROM agents a, microdaos m
WHERE a.external_id = 'agent:spirit' AND m.slug = 'soul-retreat'
ON CONFLICT (agent_id, target_microdao_id, role) DO NOTHING;

INSERT INTO agent_assignments (agent_id, target_microdao_id, scope, role, metadata)
SELECT 
    a.id::TEXT,
    m.id::TEXT,
    'district'::assignment_scope,
    'core_team',
    '{"power": ["execution"], "specialty": "information"}'::jsonb
FROM agents a, microdaos m
WHERE a.external_id = 'agent:logic' AND m.slug = 'soul-retreat'
ON CONFLICT (agent_id, target_microdao_id, role) DO NOTHING;

INSERT INTO agent_assignments (agent_id, target_microdao_id, scope, role, metadata)
SELECT 
    a.id::TEXT,
    m.id::TEXT,
    'district'::assignment_scope,
    'core_team',
    '{"power": ["execution", "infrastructure"], "specialty": "energy"}'::jsonb
FROM agents a, microdaos m
WHERE a.external_id = 'agent:energia' AND m.slug = 'energyunion'
ON CONFLICT (agent_id, target_microdao_id, role) DO NOTHING;

-- ============================================================================
-- 5. PERMISSIONS - Base Permissions for City Governance
-- ============================================================================

-- DAARWIZZ permissions (superadmin on city)
INSERT INTO permissions (dais_id, target_type, target_id, action, granted_by)
VALUES 
    ('dais-daarwizz', 'city', 'daarion', 'superadmin', 'system')
ON CONFLICT (dais_id, target_type, target_id, action) DO NOTHING;

-- DARIO permissions (moderate city)
INSERT INTO permissions (dais_id, target_type, target_id, action, granted_by)
VALUES 
    ('dais-dario', 'city', 'daarion', 'moderate', 'dais-daarwizz')
ON CONFLICT (dais_id, target_type, target_id, action) DO NOTHING;

-- DARIA permissions (admin city)
INSERT INTO permissions (dais_id, target_type, target_id, action, granted_by)
VALUES 
    ('dais-daria', 'city', 'daarion', 'admin', 'dais-daarwizz')
ON CONFLICT (dais_id, target_type, target_id, action) DO NOTHING;

-- District Lead permissions
INSERT INTO permissions (dais_id, target_type, target_id, action, granted_by)
SELECT 'dais-helion', 'district', id::TEXT, 'admin', 'dais-daarwizz'
FROM microdaos WHERE slug = 'energyunion'
ON CONFLICT (dais_id, target_type, target_id, action) DO NOTHING;

INSERT INTO permissions (dais_id, target_type, target_id, action, granted_by)
SELECT 'dais-greenfood', 'district', id::TEXT, 'admin', 'dais-daarwizz'
FROM microdaos WHERE slug = 'greenfood'
ON CONFLICT (dais_id, target_type, target_id, action) DO NOTHING;

INSERT INTO permissions (dais_id, target_type, target_id, action, granted_by)
SELECT 'dais-soul', 'district', id::TEXT, 'admin', 'dais-daarwizz'
FROM microdaos WHERE slug = 'soul-retreat'
ON CONFLICT (dais_id, target_type, target_id, action) DO NOTHING;

-- ============================================================================
-- 6. ROOMS - District Rooms (if not exist)
-- ============================================================================

-- SOUL District Rooms
INSERT INTO rooms (id, owner_type, owner_id, type, space_scope, visibility, name, description, primary_agent_id)
SELECT 
    'soul-lobby', 'district', id::TEXT, 'district-room', 'district', 'public-city', 
    'SOUL Lobby', 'Головний вхід до SOUL Retreat District', 'soul'
FROM microdaos WHERE slug = 'soul-retreat'
ON CONFLICT (id) DO NOTHING;

INSERT INTO rooms (id, owner_type, owner_id, type, space_scope, visibility, name, description, primary_agent_id)
SELECT 
    'soul-events', 'district', id::TEXT, 'district-room', 'district', 'public-city',
    'SOUL Events', 'Анонси подій та ретритів', 'soul'
FROM microdaos WHERE slug = 'soul-retreat'
ON CONFLICT (id) DO NOTHING;

INSERT INTO rooms (id, owner_type, owner_id, type, space_scope, visibility, name, description, primary_agent_id)
SELECT 
    'soul-guidance', 'district', id::TEXT, 'district-room', 'district', 'members',
    'SOUL Guidance', 'Кімната Spirit & Logic — медитації та інфо', 'spirit'
FROM microdaos WHERE slug = 'soul-retreat'
ON CONFLICT (id) DO NOTHING;

INSERT INTO rooms (id, owner_type, owner_id, type, space_scope, visibility, name, description, primary_agent_id)
SELECT 
    'soul-masters', 'district', id::TEXT, 'district-room', 'district', 'public-city',
    'SOUL Masters', 'Профілі майстрів та вчителів', 'soul'
FROM microdaos WHERE slug = 'soul-retreat'
ON CONFLICT (id) DO NOTHING;

INSERT INTO rooms (id, owner_type, owner_id, type, space_scope, visibility, name, description, primary_agent_id)
SELECT 
    'soul-help', 'district', id::TEXT, 'district-room', 'district', 'public-city',
    'SOUL Help', 'Підтримка та FAQ', 'logic'
FROM microdaos WHERE slug = 'soul-retreat'
ON CONFLICT (id) DO NOTHING;

-- SOUL Portal to City
INSERT INTO rooms (id, owner_type, owner_id, type, space_scope, visibility, name, description, is_portal, portal_target_microdao_id, zone)
SELECT 
    'soul-city-portal', 'district', id::TEXT, 'front-room', 'city', 'public-city',
    'SOUL Retreat Portal', 'Портал до SOUL Retreat District', true, id::TEXT, 'wellness-zone'
FROM microdaos WHERE slug = 'soul-retreat'
ON CONFLICT (id) DO NOTHING;

-- ENERGYUNION additional rooms
INSERT INTO rooms (id, owner_type, owner_id, type, space_scope, visibility, name, description, primary_agent_id)
SELECT 
    'energyunion-telemetry', 'district', id::TEXT, 'district-room', 'district', 'members',
    'ENERGYUNION Telemetry', 'Телеметрія та моніторинг енергії', 'energia'
FROM microdaos WHERE slug = 'energyunion'
ON CONFLICT (id) DO NOTHING;

INSERT INTO rooms (id, owner_type, owner_id, type, space_scope, visibility, name, description, primary_agent_id)
SELECT 
    'energyunion-compute', 'district', id::TEXT, 'district-room', 'district', 'members',
    'ENERGYUNION Compute', 'AI/Compute ресурси та лабораторії', 'helion'
FROM microdaos WHERE slug = 'energyunion'
ON CONFLICT (id) DO NOTHING;

-- GREENFOOD additional rooms
INSERT INTO rooms (id, owner_type, owner_id, type, space_scope, visibility, name, description, primary_agent_id)
SELECT 
    'greenfood-warehouse', 'district', id::TEXT, 'district-room', 'district', 'members',
    'GREENFOOD Warehouses', 'Склади та інвентар', 'greenfood'
FROM microdaos WHERE slug = 'greenfood'
ON CONFLICT (id) DO NOTHING;

INSERT INTO rooms (id, owner_type, owner_id, type, space_scope, visibility, name, description, primary_agent_id)
SELECT 
    'greenfood-logistics', 'district', id::TEXT, 'district-room', 'district', 'members',
    'GREENFOOD Logistics', 'Логістика та доставка', 'greenfood'
FROM microdaos WHERE slug = 'greenfood'
ON CONFLICT (id) DO NOTHING;

-- ============================================================================
-- 7. LOG SEED EVENT
-- ============================================================================

INSERT INTO event_outbox (event_type, subject, payload, version, status, actor_id, scope)
VALUES (
    'system.seed.completed',
    'daarion.system.seed',
    jsonb_build_object(
        'migration', '033_mvp_seed_real_entities',
        'agents_seeded', ARRAY['daarwizz', 'dario', 'daria', 'soul', 'helion', 'greenfood', 'spirit', 'logic', 'energia', 'clan', 'druid', 'eonarch', 'yaromir'],
        'microdaos_seeded', ARRAY['daarion', 'energyunion', 'greenfood', 'soul-retreat'],
        'timestamp', NOW()
    ),
    '1.0',
    'published',
    'system',
    'city'
);

-- ============================================================================
-- DONE
-- ============================================================================

SELECT 'Migration 033 completed: MVP Seed Real Entities' as result;

