-- Seed initial components for impact analysis
-- Run after 009_impact_analysis.sql

-- Docker services on docker-host
INSERT INTO components (id, name, type, path, repo, description, health_check) VALUES
  ('propertymap-scraper', 'PropertyMap Scraper', 'service', '/opt/docker/propertymap', 'christian/propertymap', 'Scrapes Bazaraki/BuySellCyprus listings', 'docker inspect --format="{{.State.Health.Status}}" propertymap-scraper'),
  ('propertymap-db', 'PropertyMap Database', 'database', 'litellm-pgvector', NULL, 'PostgreSQL with pgvector for property embeddings', 'docker exec litellm-pgvector pg_isready -U litellm'),
  ('gridbot-conductor', 'Gridbot Conductor', 'service', '/opt/apps/eToroGridbot', 'christian/eToroGridbot', 'Trading signal processor and order executor', 'curl -s http://localhost:8000/health'),
  ('litellm', 'LiteLLM Proxy', 'service', '/opt/docker/litellm', NULL, 'LLM API proxy with cost tracking', 'curl -s http://localhost:4000/health'),
  ('n8n', 'n8n Workflows', 'service', '/opt/docker/n8n', NULL, 'Workflow automation', 'curl -s http://localhost:5678/healthz'),
  ('gitea', 'Gitea', 'service', '/opt/docker/gitea', NULL, 'Git hosting and CI', 'curl -s http://localhost:3000/api/v1/version')
ON CONFLICT (id) DO UPDATE SET
  name = EXCLUDED.name,
  type = EXCLUDED.type,
  path = EXCLUDED.path,
  repo = EXCLUDED.repo,
  description = EXCLUDED.description,
  health_check = EXCLUDED.health_check,
  updated_at = NOW();

-- Local scripts (AgilitonScripts)
INSERT INTO components (id, name, type, path, repo, description) VALUES
  ('agiliton-scripts', 'AgilitonScripts', 'library', '~/Development/Infrastructure/AgilitonScripts', 'christian/AgilitonScripts', 'CLI tools and automation scripts'),
  ('task-cli', 'Task CLI', 'script', '~/Development/Infrastructure/AgilitonScripts/bin/task', 'christian/AgilitonScripts', 'Task management CLI wrapper'),
  ('backup-restic', 'Backup Restic', 'script', '~/Development/Infrastructure/AgilitonScripts/bin/backup-restic', 'christian/AgilitonScripts', 'Restic backup to Hetzner S3'),
  ('vault-cli', 'Vault CLI', 'script', '~/Development/Infrastructure/AgilitonScripts/bin/vault', 'christian/AgilitonScripts', 'GPG-encrypted credential vault')
ON CONFLICT (id) DO UPDATE SET
  name = EXCLUDED.name,
  path = EXCLUDED.path,
  repo = EXCLUDED.repo,
  description = EXCLUDED.description,
  updated_at = NOW();

-- MCP servers
INSERT INTO components (id, name, type, path, repo, description) VALUES
  ('task-mcp', 'Task MCP', 'service', '~/Development/Infrastructure/mcp-servers/task-mcp', NULL, 'Task management MCP server'),
  ('gridbot-mcp', 'Gridbot MCP', 'api', 'docker-host:8000', 'christian/eToroGridbot', 'Trading operations MCP proxy'),
  ('vault-mcp', 'Vault MCP', 'api', '~/bin/vault-mcp', 'christian/AgilitonScripts', 'Credential vault MCP server'),
  ('gitea-mcp', 'Gitea MCP', 'api', '~/bin/gitea-mcp', NULL, 'Gitea operations MCP server')
ON CONFLICT (id) DO UPDATE SET
  name = EXCLUDED.name,
  path = EXCLUDED.path,
  repo = EXCLUDED.repo,
  description = EXCLUDED.description,
  updated_at = NOW();

-- Configuration files
INSERT INTO components (id, name, type, path, description) VALUES
  ('claude-config', 'Claude Code Config', 'config', '~/.claude.json', 'MCP server and Claude Code configuration'),
  ('ssh-tunnel-config', 'SSH Tunnel Config', 'config', '~/Library/LaunchAgents/eu.agiliton.ssh-tunnel.plist', 'autossh tunnels to docker-host'),
  ('zshrc', 'Zsh Config', 'config', '~/.zshrc', 'Shell configuration and PATH')
ON CONFLICT (id) DO UPDATE SET
  name = EXCLUDED.name,
  path = EXCLUDED.path,
  description = EXCLUDED.description,
  updated_at = NOW();

-- Dependencies
INSERT INTO component_dependencies (component_id, depends_on, dependency_type, description) VALUES
  ('propertymap-scraper', 'propertymap-db', 'hard', 'Stores scraped properties'),
  ('propertymap-scraper', 'litellm', 'soft', 'Uses LLM for embeddings'),
  ('gridbot-conductor', 'gridbot-mcp', 'config', 'MCP exposes API'),
  ('task-mcp', 'propertymap-db', 'hard', 'Uses litellm-pgvector database'),
  ('task-mcp', 'ssh-tunnel-config', 'hard', 'Needs port 5435 tunnel'),
  ('task-cli', 'task-mcp', 'soft', 'CLI can use MCP or direct DB'),
  ('agiliton-scripts', 'zshrc', 'config', 'PATH configuration'),
  ('vault-mcp', 'vault-cli', 'hard', 'MCP wraps vault CLI'),
  ('gitea-mcp', 'ssh-tunnel-config', 'hard', 'Needs port 3000 tunnel')
ON CONFLICT (component_id, depends_on) DO UPDATE SET
  dependency_type = EXCLUDED.dependency_type,
  description = EXCLUDED.description;

-- File patterns for git diff analysis
INSERT INTO component_files (component_id, file_pattern) VALUES
  ('propertymap-scraper', 'propertymap/services/*.py'),
  ('propertymap-scraper', 'propertymap/scraper/*.py'),
  ('propertymap-scraper', 'propertymap/docker-compose.yml'),
  ('gridbot-conductor', 'eToroGridbot/conductor/*.py'),
  ('gridbot-conductor', 'eToroGridbot/docker-compose.yml'),
  ('task-mcp', 'mcp-servers/task-mcp/src/*.ts'),
  ('task-mcp', 'mcp-servers/task-mcp/migrations/*.sql'),
  ('task-cli', 'AgilitonScripts/bin/task'),
  ('agiliton-scripts', 'AgilitonScripts/bin/*'),
  ('claude-config', '.claude.json'),
  ('ssh-tunnel-config', 'Library/LaunchAgents/eu.agiliton.ssh-tunnel.plist'),
  ('zshrc', '.zshrc')
ON CONFLICT (component_id, file_pattern) DO NOTHING;

-- Verification checks
INSERT INTO verification_checks (component_id, name, check_type, check_command, expected_result) VALUES
  ('propertymap-scraper', 'container-health', 'command', 'ssh docker-host "docker inspect --format={{.State.Health.Status}} propertymap-scraper"', 'healthy'),
  ('propertymap-db', 'db-ready', 'command', 'ssh docker-host "docker exec litellm-pgvector pg_isready -U litellm"', 'accepting connections'),
  ('gridbot-conductor', 'api-health', 'http', 'http://localhost:8000/health', '{"status":"ok"}'),
  ('task-mcp', 'db-connection', 'tcp', 'localhost:5435', 'connected'),
  ('ssh-tunnel-config', 'tunnel-active', 'command', 'pgrep -f "ssh.*5435" || pgrep -f autossh', 'running')
ON CONFLICT DO NOTHING;

-- Historical impacts (learned from this session)
INSERT INTO change_impacts (changed_component, affected_component, impact_description, task_id) VALUES
  ('propertymap-db', 'propertymap-scraper', 'pgvector migration required updating processor.py in services/ directory, not scraper/services/', NULL),
  ('agiliton-scripts', 'backup-restic', 'Path changed from ~/Development/AgilitonScripts to ~/Development/Infrastructure/AgilitonScripts', NULL),
  ('task-mcp', 'task-cli', 'CLI uses project name in INSERT but database expects project key - foreign key violation', NULL)
ON CONFLICT DO NOTHING;
