// Neo4j Graph Schema for Helion Memory v3.0
// Run this in Neo4j Browser or via Cypher shell
// Created: 2026-01-17

// ============================================================================
// CONSTRAINTS (Uniqueness)
// ============================================================================

// User node uniqueness
CREATE CONSTRAINT user_platform_id IF NOT EXISTS
FOR (u:User) REQUIRE u.platform_user_id IS UNIQUE;

// Group node uniqueness  
CREATE CONSTRAINT group_chat_id IF NOT EXISTS
FOR (g:Group) REQUIRE g.chat_id IS UNIQUE;

// Role node uniqueness
CREATE CONSTRAINT role_code IF NOT EXISTS
FOR (r:Role) REQUIRE r.code IS UNIQUE;

// Topic node uniqueness
CREATE CONSTRAINT topic_id IF NOT EXISTS
FOR (t:Topic) REQUIRE t.topic_id IS UNIQUE;

// Project node uniqueness
CREATE CONSTRAINT project_id IF NOT EXISTS
FOR (p:Project) REQUIRE p.project_id IS UNIQUE;

// Artifact node uniqueness
CREATE CONSTRAINT artifact_id IF NOT EXISTS
FOR (a:Artifact) REQUIRE a.artifact_id IS UNIQUE;

// Decision node uniqueness
CREATE CONSTRAINT decision_id IF NOT EXISTS
FOR (d:Decision) REQUIRE d.decision_id IS UNIQUE;

// ============================================================================
// INDEXES (Performance)
// ============================================================================

// User indexes
CREATE INDEX user_telegram_id IF NOT EXISTS FOR (u:User) ON (u.telegram_user_id);
CREATE INDEX user_username IF NOT EXISTS FOR (u:User) ON (u.username);
CREATE INDEX user_status IF NOT EXISTS FOR (u:User) ON (u.status);

// Group indexes
CREATE INDEX group_channel IF NOT EXISTS FOR (g:Group) ON (g.channel);
CREATE INDEX group_trust IF NOT EXISTS FOR (g:Group) ON (g.trust_mode);

// Topic indexes
CREATE INDEX topic_name IF NOT EXISTS FOR (t:Topic) ON (t.name);
CREATE INDEX topic_category IF NOT EXISTS FOR (t:Topic) ON (t.category);

// Project indexes
CREATE INDEX project_name IF NOT EXISTS FOR (p:Project) ON (p.name);
CREATE INDEX project_status IF NOT EXISTS FOR (p:Project) ON (p.status);

// ============================================================================
// INITIAL NODES: Roles
// ============================================================================

MERGE (r:Role {code: 'admin'})
SET r.display_name = 'Administrator',
    r.scope = 'platform',
    r.description = 'Full platform access',
    r.created_at = datetime();

MERGE (r:Role {code: 'mentor'})
SET r.display_name = 'Mentor',
    r.scope = 'platform',
    r.description = 'Can teach Helion, elevated trust',
    r.created_at = datetime();

MERGE (r:Role {code: 'developer'})
SET r.display_name = 'Developer',
    r.scope = 'platform',
    r.description = 'Technical team member',
    r.created_at = datetime();

MERGE (r:Role {code: 'investor'})
SET r.display_name = 'Investor',
    r.scope = 'platform',
    r.description = 'Token holder or investor',
    r.created_at = datetime();

MERGE (r:Role {code: 'ops'})
SET r.display_name = 'Operations',
    r.scope = 'platform',
    r.description = 'Operations team member',
    r.created_at = datetime();

MERGE (r:Role {code: 'moderator'})
SET r.display_name = 'Moderator',
    r.scope = 'group',
    r.description = 'Group moderator',
    r.created_at = datetime();

MERGE (r:Role {code: 'member'})
SET r.display_name = 'Member',
    r.scope = 'platform',
    r.description = 'Regular platform member',
    r.created_at = datetime();

// ============================================================================
// INITIAL NODES: Energy Union Projects
// ============================================================================

MERGE (p:Project {project_id: 'energy-union'})
SET p.name = 'Energy Union',
    p.description = 'Main platform for decentralized energy solutions',
    p.status = 'active',
    p.created_at = datetime();

MERGE (p:Project {project_id: 'biominer'})
SET p.name = 'BioMiner',
    p.description = 'Bioenergy mining system',
    p.status = 'active',
    p.parent_project = 'energy-union',
    p.created_at = datetime();

MERGE (p:Project {project_id: 'ecominer'})
SET p.name = 'EcoMiner',
    p.description = 'Modular cogeneration system',
    p.status = 'active',
    p.parent_project = 'energy-union',
    p.created_at = datetime();

MERGE (p:Project {project_id: 'eu-token'})
SET p.name = 'EU Token',
    p.description = 'Energy Union utility token',
    p.status = 'active',
    p.parent_project = 'energy-union',
    p.created_at = datetime();

// ============================================================================
// INITIAL NODES: Topics
// ============================================================================

MERGE (t:Topic {topic_id: 'tokenomics'})
SET t.name = 'Tokenomics',
    t.category = 'technical',
    t.description = 'Token economics and distribution',
    t.created_at = datetime();

MERGE (t:Topic {topic_id: 'dao-governance'})
SET t.name = 'DAO Governance',
    t.category = 'organizational',
    t.description = 'Decentralized governance mechanisms',
    t.created_at = datetime();

MERGE (t:Topic {topic_id: 'cogeneration'})
SET t.name = 'Cogeneration',
    t.category = 'technical',
    t.description = 'Combined heat and power generation',
    t.created_at = datetime();

MERGE (t:Topic {topic_id: 'biogas'})
SET t.name = 'Biogas',
    t.category = 'technical',
    t.description = 'Biogas production and utilization',
    t.created_at = datetime();

MERGE (t:Topic {topic_id: 'investment'})
SET t.name = 'Investment',
    t.category = 'business',
    t.description = 'Investment opportunities and strategies',
    t.created_at = datetime();

MERGE (t:Topic {topic_id: 'staking'})
SET t.name = 'Staking',
    t.category = 'technical',
    t.description = 'Token staking mechanisms',
    t.created_at = datetime();

// ============================================================================
// INITIAL NODES: Known Mentors (as Users)
// ============================================================================

MERGE (u:User {username: '@ivantytar'})
SET u.display_name = 'Іван Титар',
    u.status = 'active',
    u.is_mentor = true,
    u.created_at = datetime();

MERGE (u:User {username: '@archenvis'})
SET u.display_name = 'Александр Вертій',
    u.status = 'active',
    u.is_mentor = true,
    u.created_at = datetime();

MERGE (u:User {username: '@olegarch88'})
SET u.display_name = 'Олег Ковальчук',
    u.status = 'active',
    u.is_mentor = true,
    u.created_at = datetime();

// ============================================================================
// INITIAL NODES: Trusted Groups
// ============================================================================

MERGE (g:Group {chat_id: 'energyunionofficial'})
SET g.channel = 'telegram',
    g.username = '@energyunionofficial',
    g.title = 'Energy Union Official',
    g.trust_mode = true,
    g.apprentice_mode = true,
    g.created_at = datetime();

MERGE (g:Group {chat_id: 'energyunionteam'})
SET g.channel = 'telegram',
    g.username = '@energyunionteam',
    g.title = 'Energy Union Team',
    g.trust_mode = true,
    g.apprentice_mode = true,
    g.created_at = datetime();

// ============================================================================
// INITIAL RELATIONSHIPS
// ============================================================================

// Mentors have MENTOR role
MATCH (u:User {username: '@ivantytar'}), (r:Role {code: 'mentor'})
MERGE (u)-[:HAS_ROLE {confidence: 1.0, assigned_by: 'config', assigned_at: datetime()}]->(r);

MATCH (u:User {username: '@archenvis'}), (r:Role {code: 'mentor'})
MERGE (u)-[:HAS_ROLE {confidence: 1.0, assigned_by: 'config', assigned_at: datetime()}]->(r);

MATCH (u:User {username: '@olegarch88'}), (r:Role {code: 'mentor'})
MERGE (u)-[:HAS_ROLE {confidence: 1.0, assigned_by: 'config', assigned_at: datetime()}]->(r);

// Mentors can TEACH Helion
MATCH (u:User {is_mentor: true})
MERGE (h:Agent {agent_id: 'helion'})
SET h.name = 'Helion', h.status = 'active'
MERGE (u)-[:MENTORS {scope: 'platform', since: datetime()}]->(h);

// Projects related to Topics
MATCH (p:Project {project_id: 'biominer'}), (t:Topic {topic_id: 'biogas'})
MERGE (p)-[:RELATED_TO]->(t);

MATCH (p:Project {project_id: 'ecominer'}), (t:Topic {topic_id: 'cogeneration'})
MERGE (p)-[:RELATED_TO]->(t);

MATCH (p:Project {project_id: 'eu-token'}), (t:Topic {topic_id: 'tokenomics'})
MERGE (p)-[:RELATED_TO]->(t);

MATCH (p:Project {project_id: 'eu-token'}), (t:Topic {topic_id: 'staking'})
MERGE (p)-[:RELATED_TO]->(t);

MATCH (p:Project {project_id: 'energy-union'}), (t:Topic {topic_id: 'dao-governance'})
MERGE (p)-[:RELATED_TO]->(t);

// Sub-projects
MATCH (parent:Project {project_id: 'energy-union'}), (child:Project)
WHERE child.parent_project = 'energy-union'
MERGE (child)-[:PART_OF]->(parent);

// ============================================================================
// USEFUL QUERIES (Examples)
// ============================================================================

// Find all mentors:
// MATCH (u:User)-[:HAS_ROLE]->(r:Role {code: 'mentor'}) RETURN u.display_name, u.username

// Find user's roles:
// MATCH (u:User {username: $username})-[hr:HAS_ROLE]->(r:Role) RETURN r.code, hr.confidence

// Find who works on a project:
// MATCH (u:User)-[:WORKS_ON]->(p:Project {name: 'EcoMiner'}) RETURN u.display_name

// Find related topics for a project:
// MATCH (p:Project {name: 'BioMiner'})-[:RELATED_TO]->(t:Topic) RETURN t.name

// Find who asked about a topic:
// MATCH (u:User)-[a:ASKED_ABOUT]->(t:Topic {name: 'Tokenomics'}) RETURN u.display_name, a.count, a.last_asked

// Find mentors who can help with a topic:
// MATCH (u:User)-[:HAS_ROLE]->(r:Role {code: 'mentor'}), (u)-[:KNOWS_ABOUT]->(t:Topic {name: $topic})
// RETURN u.display_name, u.username

// Get full user context (roles, projects, topics):
// MATCH (u:User {username: $username})
// OPTIONAL MATCH (u)-[hr:HAS_ROLE]->(r:Role)
// OPTIONAL MATCH (u)-[:WORKS_ON]->(p:Project)
// OPTIONAL MATCH (u)-[:ASKED_ABOUT]->(t:Topic)
// RETURN u, collect(DISTINCT r.code) as roles, collect(DISTINCT p.name) as projects, collect(DISTINCT t.name) as topics
