# DEPENDENCY_GRAPH.yml - Platform Marketing Content
# Grafo de dependencias entre modulos y componentes
# Version: 1.0.0
# Fecha: 2025-12-08

version: "1.0.0"
proyecto: "Platform Marketing Content"
fecha_actualizacion: "2025-12-08"

# =============================================================================
# DEPENDENCIAS ENTRE MODULOS
# =============================================================================

modulos:
  # -------------------------------------------------------------------------
  # AUTH - Modulo base (sin dependencias de otros modulos PMC)
  # -------------------------------------------------------------------------
  auth:
    id: PMC-007
    nombre: Auth/Admin
    dependencias_internas: []
    dependencias_catalogo:
      - "@CATALOG_AUTH"
      - "@CATALOG_SESSION"
    es_dependencia_de:
      - crm
      - projects
      - generation
      - assets
      - automation
      - analytics

  # -------------------------------------------------------------------------
  # TENANTS - Modulo base para multi-tenancy
  # -------------------------------------------------------------------------
  tenants:
    id: PMC-001
    nombre: Tenants
    dependencias_internas:
      - auth  # Para usuarios y roles
    dependencias_catalogo:
      - "@CATALOG_TENANT"
      - "@CATALOG_PAYMENTS"
    es_dependencia_de:
      - crm
      - projects
      - generation
      - assets
      - automation
      - analytics

  # -------------------------------------------------------------------------
  # CRM - Depende de auth y tenants
  # -------------------------------------------------------------------------
  crm:
    id: PMC-002
    nombre: CRM
    dependencias_internas:
      - auth
      - tenants
    dependencias_catalogo: []
    es_dependencia_de:
      - projects
      - generation

  # -------------------------------------------------------------------------
  # PROJECTS - Depende de CRM para clientes y marcas
  # -------------------------------------------------------------------------
  projects:
    id: PMC-003
    nombre: Projects
    dependencias_internas:
      - auth
      - tenants
      - crm      # clients, brands para proyectos
      - assets   # para campaign_assets
    dependencias_catalogo: []
    es_dependencia_de:
      - generation
      - automation

  # -------------------------------------------------------------------------
  # GENERATION - Motor de IA
  # -------------------------------------------------------------------------
  generation:
    id: PMC-004
    nombre: Generation
    dependencias_internas:
      - auth
      - tenants
      - crm       # products, brands para contexto
      - projects  # campaigns para asociar generaciones
      - assets    # para guardar resultados
    dependencias_catalogo:
      - "@CATALOG_RATELIMIT"
      - "@CATALOG_WS"
    es_dependencia_de:
      - automation

  # -------------------------------------------------------------------------
  # ASSETS - DAM
  # -------------------------------------------------------------------------
  assets:
    id: PMC-006
    nombre: Assets
    dependencias_internas:
      - auth
      - tenants
    dependencias_catalogo: []
    es_dependencia_de:
      - projects
      - generation

  # -------------------------------------------------------------------------
  # AUTOMATION - Flujos automatizados
  # -------------------------------------------------------------------------
  automation:
    id: PMC-005
    nombre: Automation
    dependencias_internas:
      - auth
      - tenants
      - crm         # eventos de clientes/productos
      - projects    # eventos de campanas
      - generation  # trigger de generaciones
      - assets      # eventos de assets
    dependencias_catalogo:
      - "@CATALOG_NOTIFY"
    es_dependencia_de: []

  # -------------------------------------------------------------------------
  # ANALYTICS - Reportes y dashboards
  # -------------------------------------------------------------------------
  analytics:
    id: PMC-008
    nombre: Analytics
    dependencias_internas:
      - auth
      - tenants
      - crm
      - projects
      - generation
      - assets
    dependencias_catalogo: []
    es_dependencia_de: []

# =============================================================================
# ORDEN DE IMPLEMENTACION
# =============================================================================

orden_implementacion:
  fase_1:
    nombre: "Core Foundation"
    modulos:
      - auth
      - tenants
    razon: "Base para multi-tenancy y autenticacion"
    duracion_estimada: "Sprint 1-2"

  fase_2:
    nombre: "Business Domain"
    modulos:
      - crm
      - assets
    razon: "Entidades de negocio base"
    duracion_estimada: "Sprint 3-4"

  fase_3:
    nombre: "Project Management"
    modulos:
      - projects
    razon: "Depende de CRM y Assets"
    duracion_estimada: "Sprint 5"

  fase_4:
    nombre: "AI Generation"
    modulos:
      - generation
    razon: "Core diferenciador, depende de CRM y Projects"
    duracion_estimada: "Sprint 6-7"

  fase_5:
    nombre: "Advanced Features"
    modulos:
      - automation
      - analytics
    razon: "Funcionalidades avanzadas que integran todo"
    duracion_estimada: "Sprint 8-9"

# =============================================================================
# DEPENDENCIAS DE BASE DE DATOS
# =============================================================================

database_dependencies:
  schemas_orden:
    - auth      # Primero: tenants, users, roles
    - crm       # Segundo: clients, brands, products
    - projects  # Tercero: projects, campaigns
    - generation # Cuarto: jobs, workflows
    - assets    # Quinto: assets, collections
    - automation # Sexto: flows, webhooks
    - analytics # Septimo: metrics, reports

  foreign_keys_criticas:
    - from: "auth.users.tenant_id"
      to: "auth.tenants.id"
      tipo: "mandatory"

    - from: "crm.clients.tenant_id"
      to: "auth.tenants.id"
      tipo: "mandatory"

    - from: "crm.brands.client_id"
      to: "crm.clients.id"
      tipo: "mandatory"

    - from: "projects.campaigns.project_id"
      to: "projects.projects.id"
      tipo: "mandatory"

    - from: "generation.jobs.campaign_id"
      to: "projects.campaigns.id"
      tipo: "optional"

    - from: "generation.jobs.product_id"
      to: "crm.products.id"
      tipo: "optional"

# =============================================================================
# DEPENDENCIAS DE SERVICIOS EXTERNOS
# =============================================================================

external_dependencies:
  postgresql:
    version: "15+"
    requerido_por:
      - todos los modulos
    criticidad: alta

  redis:
    version: "7+"
    requerido_por:
      - auth (sesiones)
      - generation (colas BullMQ)
      - tenants (cache de limites)
    criticidad: alta

  s3_minio:
    requerido_por:
      - assets
      - generation (outputs)
    criticidad: alta

  comfyui:
    requerido_por:
      - generation (imagenes)
    criticidad: alta

  openai:
    requerido_por:
      - generation (texto)
    criticidad: media

  n8n:
    requerido_por:
      - automation
    criticidad: media

  stripe:
    requerido_por:
      - tenants (billing)
    criticidad: baja (Fase 4)

# =============================================================================
# GRAFO VISUAL (Mermaid)
# =============================================================================

diagrama_mermaid: |
  graph TD
    AUTH[Auth/Admin] --> TENANTS[Tenants]
    AUTH --> CRM[CRM]
    AUTH --> PROJECTS[Projects]
    AUTH --> GENERATION[Generation]
    AUTH --> ASSETS[Assets]
    AUTH --> AUTOMATION[Automation]
    AUTH --> ANALYTICS[Analytics]

    TENANTS --> CRM
    TENANTS --> PROJECTS
    TENANTS --> GENERATION
    TENANTS --> ASSETS
    TENANTS --> AUTOMATION
    TENANTS --> ANALYTICS

    CRM --> PROJECTS
    CRM --> GENERATION

    ASSETS --> PROJECTS
    ASSETS --> GENERATION

    PROJECTS --> GENERATION
    PROJECTS --> AUTOMATION

    GENERATION --> AUTOMATION

    CRM --> ANALYTICS
    PROJECTS --> ANALYTICS
    GENERATION --> ANALYTICS
    ASSETS --> ANALYTICS

    subgraph "Externos"
      COMFYUI[ComfyUI]
      OPENAI[OpenAI]
      N8N[n8n]
      STRIPE[Stripe]
    end

    GENERATION --> COMFYUI
    GENERATION --> OPENAI
    AUTOMATION --> N8N
    TENANTS --> STRIPE

# =============================================================================
# IMPACTO DE CAMBIOS
# =============================================================================

impacto_cambios:
  auth:
    impacto: CRITICO
    afecta: "Todos los modulos"
    consideraciones:
      - "Cambios en User afectan auditoria en todas las tablas"
      - "Cambios en permisos requieren revision de guards"

  tenants:
    impacto: CRITICO
    afecta: "Todos los modulos con tenant_id"
    consideraciones:
      - "Cambios en limites afectan quotas"
      - "Cambios en RLS requieren migracion cuidadosa"

  crm:
    impacto: ALTO
    afecta: "Projects, Generation"
    consideraciones:
      - "Cambios en Brand afectan generation context"
      - "Cambios en Product afectan asociaciones de jobs"

  projects:
    impacto: MEDIO
    afecta: "Generation, Automation"
    consideraciones:
      - "Cambios en Campaign afectan tracking de jobs"

  generation:
    impacto: BAJO
    afecta: "Automation (triggers)"
    consideraciones:
      - "Cambios en job status afectan webhooks"

  assets:
    impacto: MEDIO
    afecta: "Projects, Generation"
    consideraciones:
      - "Cambios en storage path afectan URLs existentes"

  automation:
    impacto: BAJO
    afecta: "Ninguno (hoja del arbol)"
    consideraciones:
      - "Cambios aislados, no afectan otros modulos"

  analytics:
    impacto: BAJO
    afecta: "Ninguno (hoja del arbol)"
    consideraciones:
      - "Solo lectura de datos de otros modulos"

# =============================================================================
# METADATOS
# =============================================================================

metadata:
  creado_por: Requirements-Analyst
  fecha_creacion: "2025-12-08"
  ultima_actualizacion: "2025-12-08"
