// bot/src/types.ts — all shared interfaces

export interface SlackConfig {
  session_name: string   // e.g. "cijira" → prefix "ciji"
  bot_token: string
  app_token: string
}

export interface JiraConfig {
  base_url: string
  project_key: string
  backlog_column: string
  api_token: string
  user: string
}

export interface BitbucketConfig {
  workspace: string
  repo_slug: string
  base_branch: string   // always "homolog"
  app_password: string
  user: string
}

export interface GitHubAuthConfig {
  token?: string
  token_env?: string
  app_id?: string
  installation_id?: string
  private_key?: string
  private_key_env?: string
  private_key_path?: string
}

export interface GitHubProjectFieldMapping {
  status_field_id?: string
  default_status_option_id?: string
  todo_option_id?: string
  in_progress_option_id?: string
  done_option_id?: string
}

export interface GitHubProjectConfig {
  owner_type?: 'organization' | 'user'
  owner?: string
  number?: number
  id?: string
  field_mapping?: GitHubProjectFieldMapping
}

export interface GitHubWebhookConfig {
  enabled?: boolean
  port?: number
  path?: string
}

export interface GitHubConfig {
  owner: string
  repo: string
  base_branch: string
  auth?: GitHubAuthConfig
  project?: GitHubProjectConfig
  webhook_secret_env?: string
  webhook?: GitHubWebhookConfig
  labels?: string[]
  request_timeout_ms?: number
  max_attempts?: number
  retry_base_delay_ms?: number
}

export interface BotIntegrationsConfig {
  work_tracker?: 'jira' | 'github'
  repo?: 'bitbucket' | 'github' | 'git'
}

export interface LLMConfig {
  provider: 'anthropic' | 'openai' | 'ollama'
  model: string
  api_key?: string
  openai_api_key?: string
  ollama_base_url?: string
}

export interface SddConfig {
  worktrees_root: string   // ".worktree"
}

export interface BotConfig {
  slack: SlackConfig
  integrations?: BotIntegrationsConfig
  jira?: JiraConfig
  bitbucket?: BitbucketConfig
  github?: GitHubConfig
  llm: LLMConfig
  sdd: SddConfig
}

export interface SessionEntry {
  slack_channel_id: string
  worktree_path: string
  agent_session_id: string
  owner_user_id: string
  card_id: string
  status: 'em_elaboracao' | 'aprovado'
  created_at: string
}

export type SessionMap = Record<string, SessionEntry>

// ── Tipos de domínio: Card e CardStage ────────────────────────────────────────
// S-07: nenhum campo de Card ou CardStage armazena tokens, API keys ou passwords.

export type CardProvider = 'jira' | 'github'
export type CardStatus = 'em_progresso' | 'aprovado'
export type StageName = 'risk' | 'idea' | 'prd'

export interface Card {
  card_id: string
  provider: CardProvider
  external_url: string | null
  slack_user_id: string
  slack_channel_id: string
  titulo: string
  texto_original: string
  status: CardStatus
  criado_em: string        // ISO-8601
  atualizado_em: string    // ISO-8601
  deleted_at: string | null
}

export interface CardStage {
  id: number
  card_id: string
  stage: StageName
  conteudo: string
  gerado_em: string
}

export const SDD_EXIT = {
  OK: 0,
  GATE_BLOCKED: 2,
  CONFIG_ERROR: 3,
  RUNTIME_ERROR: 1,
} as const
