# DATABASE_INVENTORY.yml - Platform Marketing Content
# Inventario de base de datos
# Version: 1.0.0
# Fecha: 2025-12-08
# Estado: Planificacion (sin implementar)

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

# =============================================================================
# RESUMEN
# =============================================================================

resumen:
  motor: PostgreSQL 15+
  schemas_planificados: 7
  tablas_planificadas: 28
  funciones_planificadas: ~20
  triggers_planificados: ~15
  politicas_rls_planificadas: ~25

# =============================================================================
# SCHEMAS
# =============================================================================

schemas:
  auth:
    proposito: Autenticacion, usuarios, tenants
    tablas:
      - tenants
      - plans
      - users
      - roles
      - user_sessions
      - invitations
      - audit_logs
    referencias:
      - "@CATALOG_AUTH"
      - "@CATALOG_TENANT"

  crm:
    proposito: Gestion de clientes y marcas
    tablas:
      - clients
      - contacts
      - brands
      - products
      - opportunities
    rls: true

  projects:
    proposito: Proyectos y campanas
    tablas:
      - projects
      - campaigns
      - briefs
      - campaign_assets
      - approvals
    rls: true

  generation:
    proposito: Motor de generacion IA
    tablas:
      - jobs
      - workflow_templates
      - custom_models
      - text_generations
    rls: true

  assets:
    proposito: DAM (Digital Asset Management)
    tablas:
      - assets
      - asset_versions
      - collections
      - collection_assets
      - asset_comments
    rls: true

  automation:
    proposito: Flujos y webhooks
    tablas:
      - automation_flows
      - automation_runs
      - webhook_endpoints
      - event_logs
    rls: true

  analytics:
    proposito: Metricas y reportes
    tablas:
      - metrics
      - reports
      - saved_views
    rls: true

# =============================================================================
# TABLAS DETALLADAS (Planificadas)
# =============================================================================

tablas_detalladas:
  "auth.tenants":
    columnas:
      - id: UUID PRIMARY KEY
      - name: VARCHAR(255) NOT NULL
      - slug: VARCHAR(100) UNIQUE NOT NULL
      - status: tenant_status NOT NULL DEFAULT 'active'
      - plan_id: UUID REFERENCES auth.plans(id)
      - settings: JSONB DEFAULT '{}'
      - branding: JSONB DEFAULT '{}'
      - limits: JSONB DEFAULT '{}'
      - created_at: TIMESTAMPTZ DEFAULT NOW()
      - updated_at: TIMESTAMPTZ DEFAULT NOW()
      - deleted_at: TIMESTAMPTZ
    indices:
      - idx_tenants_slug (slug)
      - idx_tenants_status (status)

  "auth.users":
    columnas:
      - id: UUID PRIMARY KEY
      - tenant_id: UUID NOT NULL REFERENCES auth.tenants(id)
      - email: VARCHAR(255) NOT NULL
      - password_hash: VARCHAR(255)
      - first_name: VARCHAR(100)
      - last_name: VARCHAR(100)
      - avatar_url: TEXT
      - status: user_status DEFAULT 'pending'
      - role_id: UUID REFERENCES auth.roles(id)
      - preferences: JSONB DEFAULT '{}'
      - last_login_at: TIMESTAMPTZ
      - email_verified_at: TIMESTAMPTZ
      - created_at: TIMESTAMPTZ DEFAULT NOW()
      - updated_at: TIMESTAMPTZ DEFAULT NOW()
    indices:
      - idx_users_tenant_email UNIQUE (tenant_id, email)
      - idx_users_status (status)
    rls: true

  "crm.clients":
    columnas:
      - id: UUID PRIMARY KEY
      - tenant_id: UUID NOT NULL
      - name: VARCHAR(255) NOT NULL
      - slug: VARCHAR(100) NOT NULL
      - type: client_type DEFAULT 'company'
      - industry: VARCHAR(100)
      - status: client_status DEFAULT 'active'
      - metadata: JSONB DEFAULT '{}'
      - created_at: TIMESTAMPTZ DEFAULT NOW()
      - updated_at: TIMESTAMPTZ DEFAULT NOW()
      - deleted_at: TIMESTAMPTZ
    indices:
      - idx_clients_tenant (tenant_id)
      - idx_clients_tenant_slug UNIQUE (tenant_id, slug)
    rls: true

  "crm.brands":
    columnas:
      - id: UUID PRIMARY KEY
      - tenant_id: UUID NOT NULL
      - client_id: UUID NOT NULL REFERENCES crm.clients(id)
      - name: VARCHAR(255) NOT NULL
      - slug: VARCHAR(100) NOT NULL
      - description: TEXT
      - logo_url: TEXT
      - colors: JSONB DEFAULT '{}'
      - fonts: JSONB DEFAULT '{}'
      - guidelines: JSONB DEFAULT '{}'
      - status: brand_status DEFAULT 'active'
      - created_at: TIMESTAMPTZ DEFAULT NOW()
      - updated_at: TIMESTAMPTZ DEFAULT NOW()
    indices:
      - idx_brands_tenant (tenant_id)
      - idx_brands_client (client_id)
      - idx_brands_tenant_slug UNIQUE (tenant_id, slug)
    rls: true

  "generation.jobs":
    columnas:
      - id: UUID PRIMARY KEY
      - tenant_id: UUID NOT NULL
      - campaign_id: UUID REFERENCES projects.campaigns(id)
      - product_id: UUID REFERENCES crm.products(id)
      - user_id: UUID NOT NULL REFERENCES auth.users(id)
      - type: job_type NOT NULL
      - status: job_status DEFAULT 'queued'
      - priority: INTEGER DEFAULT 5
      - workflow_id: UUID REFERENCES generation.workflow_templates(id)
      - input_params: JSONB NOT NULL
      - output_assets: UUID[] DEFAULT '{}'
      - error_message: TEXT
      - progress: INTEGER DEFAULT 0
      - started_at: TIMESTAMPTZ
      - completed_at: TIMESTAMPTZ
      - created_at: TIMESTAMPTZ DEFAULT NOW()
    indices:
      - idx_jobs_tenant (tenant_id)
      - idx_jobs_status (status)
      - idx_jobs_tenant_status (tenant_id, status)
    rls: true

# =============================================================================
# ENUMS
# =============================================================================

enums:
  tenant_status: [active, suspended, trial, cancelled]
  user_status: [pending, active, suspended, deactivated]
  client_status: [active, inactive, archived]
  client_type: [company, individual]
  brand_status: [active, inactive, archived]
  job_type: [image, text, image_batch, mixed]
  job_status: [queued, processing, completed, failed, cancelled]
  asset_type: [image, video, document, audio]
  asset_status: [processing, ready, failed]

# =============================================================================
# FUNCIONES (Planificadas)
# =============================================================================

funciones_planificadas:
  - nombre: set_updated_at()
    proposito: Actualizar updated_at automaticamente
    trigger: BEFORE UPDATE

  - nombre: soft_delete()
    proposito: Marcar deleted_at en lugar de DELETE fisico

  - nombre: check_tenant_quota(tenant_id, resource)
    proposito: Verificar limites del tenant

  - nombre: increment_usage(tenant_id, resource, amount)
    proposito: Incrementar uso de recurso

# =============================================================================
# RLS POLICIES (Planificadas)
# =============================================================================

rls_policies:
  patron_base: |
    CREATE POLICY tenant_isolation ON {schema}.{tabla}
        USING (tenant_id = current_setting('app.current_tenant')::uuid);

  tablas_con_rls:
    - auth.users
    - crm.clients
    - crm.contacts
    - crm.brands
    - crm.products
    - crm.opportunities
    - projects.projects
    - projects.campaigns
    - generation.jobs
    - generation.custom_models
    - assets.assets
    - assets.collections
    - automation.automation_flows
    - analytics.metrics

# =============================================================================
# REFERENCIAS
# =============================================================================

referencias:
  esquema_detallado: docs/04-modelado/ESQUEMA-BD.md
  modelo_dominio: docs/04-modelado/MODELO-DOMINIO.md
  adr_multitenancy: docs/97-adr/ADR-002-multi-tenancy.md
  catalogo_tenant: shared/catalog/multi-tenancy/

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

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