// =====================================================
// NEO4J SCHEMA FROM POSTGRESQL DATA - TSS NAMESPACE
// Price-focused migration from existing PostgreSQL database
// Uses TSS (Tech Stack Selector) namespace for data isolation
// =====================================================

// Clear existing TSS data only (preserve TM namespace data)
MATCH (n) WHERE 'TSS' IN labels(n) DETACH DELETE n;

// Clear any non-namespaced tech-stack-selector data (but preserve TM data)
MATCH (n:Technology) WHERE NOT 'TM' IN labels(n) AND NOT 'TSS' IN labels(n) DETACH DELETE n;
MATCH (n:PriceTier) WHERE NOT 'TM' IN labels(n) AND NOT 'TSS' IN labels(n) DETACH DELETE n;
MATCH (n:Tool) WHERE NOT 'TM' IN labels(n) AND NOT 'TSS' IN labels(n) DETACH DELETE n;
MATCH (n:TechStack) WHERE NOT 'TM' IN labels(n) AND NOT 'TSS' IN labels(n) DETACH DELETE n;

// =====================================================
// CREATE CONSTRAINTS AND INDEXES
// =====================================================

// Create uniqueness constraints for TSS namespace
CREATE CONSTRAINT price_tier_name_unique_tss IF NOT EXISTS FOR (p:PriceTier:TSS) REQUIRE p.tier_name IS UNIQUE;
CREATE CONSTRAINT technology_name_unique_tss IF NOT EXISTS FOR (t:Technology:TSS) REQUIRE t.name IS UNIQUE;
CREATE CONSTRAINT tool_name_unique_tss IF NOT EXISTS FOR (tool:Tool:TSS) REQUIRE tool.name IS UNIQUE;
CREATE CONSTRAINT stack_name_unique_tss IF NOT EXISTS FOR (s:TechStack:TSS) REQUIRE s.name IS UNIQUE;

// Create indexes for performance (TSS namespace)
CREATE INDEX price_tier_range_idx_tss IF NOT EXISTS FOR (p:PriceTier:TSS) ON (p.min_price_usd, p.max_price_usd);
CREATE INDEX tech_category_idx_tss IF NOT EXISTS FOR (t:Technology:TSS) ON (t.category);
CREATE INDEX tech_cost_idx_tss IF NOT EXISTS FOR (t:Technology:TSS) ON (t.monthly_cost_usd);
CREATE INDEX tool_category_idx_tss IF NOT EXISTS FOR (tool:Tool:TSS) ON (tool.category);
CREATE INDEX tool_cost_idx_tss IF NOT EXISTS FOR (tool:Tool:TSS) ON (tool.monthly_cost_usd);

// =====================================================
// PRICE TIER NODES (from PostgreSQL price_tiers table)
// =====================================================

// These will be populated from PostgreSQL data with TSS namespace
// Structure matches PostgreSQL price_tiers table:
// - id, tier_name, min_price_usd, max_price_usd, target_audience, typical_project_scale, description
// All nodes will have labels: PriceTier:TSS

// =====================================================
// TECHNOLOGY NODES (from PostgreSQL technology tables)
// =====================================================

// These will be populated from PostgreSQL data with TSS namespace
// Categories: frontend_technologies, backend_technologies, database_technologies, 
//            cloud_technologies, testing_technologies, mobile_technologies, 
//            devops_technologies, ai_ml_technologies
// All nodes will have labels: Technology:TSS

// =====================================================
// TOOL NODES (from PostgreSQL tools table)
// =====================================================

// These will be populated from PostgreSQL data with TSS namespace
// Structure matches PostgreSQL tools table with pricing:
// - id, name, category, description, monthly_cost_usd, setup_cost_usd, 
//   price_tier_id, total_cost_of_ownership_score, price_performance_ratio
// All nodes will have labels: Tool:TSS

// =====================================================
// TECH STACK NODES (will be generated from combinations)
// =====================================================

// These will be dynamically created based on:
// - Price tier constraints
// - Technology compatibility
// - Budget optimization
// - Domain requirements
// All nodes will have labels: TechStack:TSS

// =====================================================
// RELATIONSHIP TYPES
// =====================================================

// Price-based relationships (TSS namespace)
// - [:BELONGS_TO_TIER_TSS] - Technology/Tool belongs to price tier
// - [:WITHIN_BUDGET_TSS] - Technology/Tool fits within budget range
// - [:COST_OPTIMIZED_TSS] - Optimal cost-performance ratio

// Technology relationships (TSS namespace)
// - [:COMPATIBLE_WITH_TSS] - Technology compatibility
// - [:USES_FRONTEND_TSS] - Stack uses frontend technology
// - [:USES_BACKEND_TSS] - Stack uses backend technology
// - [:USES_DATABASE_TSS] - Stack uses database technology
// - [:USES_CLOUD_TSS] - Stack uses cloud technology
// - [:USES_TESTING_TSS] - Stack uses testing technology
// - [:USES_MOBILE_TSS] - Stack uses mobile technology
// - [:USES_DEVOPS_TSS] - Stack uses devops technology
// - [:USES_AI_ML_TSS] - Stack uses AI/ML technology

// Tool relationships (TSS namespace)
// - [:RECOMMENDED_FOR_TSS] - Tool recommended for domain/use case
// - [:INTEGRATES_WITH_TSS] - Tool integrates with technology
// - [:SUITABLE_FOR_TSS] - Tool suitable for price tier

// Domain relationships (TSS namespace)
// - [:RECOMMENDS_TSS] - Domain recommends tech stack

// =====================================================
// PRICE-BASED QUERIES (examples)
// =====================================================

// Query 1: Find technologies within budget (TSS namespace)
// MATCH (t:Technology:TSS)-[:BELONGS_TO_TIER_TSS]->(p:PriceTier:TSS)
// WHERE $budget >= p.min_price_usd AND $budget <= p.max_price_usd
// RETURN t, p ORDER BY t.total_cost_of_ownership_score DESC

// Query 2: Find optimal tech stack for budget (TSS namespace)
// MATCH (frontend:Technology:TSS {category: "frontend"})-[:BELONGS_TO_TIER_TSS]->(p1:PriceTier:TSS)
// MATCH (backend:Technology:TSS {category: "backend"})-[:BELONGS_TO_TIER_TSS]->(p2:PriceTier:TSS)
// MATCH (database:Technology:TSS {category: "database"})-[:BELONGS_TO_TIER_TSS]->(p3:PriceTier:TSS)
// MATCH (cloud:Technology:TSS {category: "cloud"})-[:BELONGS_TO_TIER_TSS]->(p4:PriceTier:TSS)
// WHERE (frontend.monthly_cost_usd + backend.monthly_cost_usd + 
//        database.monthly_cost_usd + cloud.monthly_cost_usd) <= $budget
// RETURN frontend, backend, database, cloud, 
//        (frontend.monthly_cost_usd + backend.monthly_cost_usd + 
//         database.monthly_cost_usd + cloud.monthly_cost_usd) as total_cost
// ORDER BY total_cost ASC, 
//          (frontend.total_cost_of_ownership_score + backend.total_cost_of_ownership_score + 
//           database.total_cost_of_ownership_score + cloud.total_cost_of_ownership_score) DESC

// Query 3: Find tools for specific price tier (TSS namespace)
// MATCH (tool:Tool:TSS)-[:BELONGS_TO_TIER_TSS]->(p:PriceTier:TSS {tier_name: $tier_name})
// RETURN tool ORDER BY tool.price_performance_ratio DESC

// Query 4: Find tech stacks by domain (TSS namespace)
// MATCH (d:Domain:TSS)-[:RECOMMENDS_TSS]->(s:TechStack:TSS)
// WHERE toLower(d.name) = toLower($domain)
// RETURN s ORDER BY s.satisfaction_score DESC

// Query 5: Check namespace isolation
// MATCH (tss_node) WHERE 'TSS' IN labels(tss_node) RETURN count(tss_node) as tss_count
// MATCH (tm_node) WHERE 'TM' IN labels(tm_node) RETURN count(tm_node) as tm_count

// =====================================================
// COMPLETION STATUS
// =====================================================

RETURN "✅ Neo4j Schema Ready for PostgreSQL Migration with TSS Namespace!" as status,
       "🎯 Focus: Price-based relationships with TSS namespace isolation" as focus,
       "📊 Ready for data migration with namespace separation from TM data" as ready_state,
       "🔒 Data Isolation: TSS namespace ensures no conflicts with Template Manager" as isolation;
