// Mirrors Go struct definitions in internal/models/
// Keep in sync with backend JSON serialization.

export interface User {
  id: number
  username: string
  email: string
  avatarUrl: string
  isAdmin: boolean
  createdAt: string // ISO 8601
  updatedAt: string
}

export interface Repository {
  id: number
  ownerId: number
  ownerName: string
  name: string
  description: string
  isPrivate: boolean
  defaultBranch: string
  isEmpty: boolean
  size: number
  avatarUrl: string
  createdAt: string
  updatedAt: string
}

export type PRStatus = 'open' | 'merged' | 'closed'

export interface PRReviewer {
  userId: number
  username: string
  avatarUrl: string
}

export interface PullRequest {
  id: number
  repoId: number
  authorId: number
  title: string
  body: string
  sourceBranch: string
  targetBranch: string
  status: PRStatus
  createdAt: string
  updatedAt: string
  reviewers: PRReviewer[]
}

export interface TreeEntry {
  mode: string
  type: 'blob' | 'tree'
  hash: string
  name: string
  size: number
  commitHash: string
  commitMsg: string
  commitDate: string
}

export interface Commit {
  hash: string
  author: string
  message: string
  date: string
}

// Pipeline is a workflow definition file stored in the repository.
export interface Pipeline {
  id: number
  repoId: number
  name: string
  filePath: string
  createdAt: string
  updatedAt: string
}

export type RunStatus = 'queued' | 'running' | 'succeeded' | 'failed' | 'cancelled'

// PipelineRun is a single execution triggered by a push.
export interface PipelineRun {
  id: number
  pipelineId: number
  repoId: number
  triggerRef: string   // e.g. refs/heads/main
  triggerSha: string   // 40-char commit SHA
  triggeredBy: string  // username
  status: RunStatus
  startedAt: string | null
  finishedAt: string | null
  createdAt: string
}

// PipelineJob is a single node in the run DAG.
export interface PipelineJob {
  id: number
  runId: number
  name: string
  image: string
  needs: string   // JSON array of dependency job names: '["build","test"]'
  status: RunStatus
  startedAt: string | null
  finishedAt: string | null
  createdAt: string
}

// PipelineStep is a single command within a job.
export interface PipelineStep {
  id: number
  jobId: number
  seq: number
  name: string
  runCmd: string
  usesAction: string
  status: RunStatus
  exitCode: number
  startedAt: string | null
  finishedAt: string | null
}

// PipelineStepLog is one append-only log chunk for a step.
export interface PipelineStepLog {
  id: number
  stepId: number
  chunkIndex: number
  content: string
  createdAt: string
}

// RunDetail is what GET /runs/:runId returns — run + jobs with steps.
export interface RunDetail extends PipelineRun {
  jobs: Array<PipelineJob & { steps: PipelineStep[] }>
}

// StepLogs is what GET /jobs/:jobId/logs returns.
export type StepLogs = Array<PipelineStep & { logs: PipelineStepLog[] }>

export type IssueState = 'open' | 'closed'

export interface Issue {
  id: number
  repoId: number
  authorId: number
  authorName: string
  number: number
  title: string
  body: string
  state: IssueState
  createdAt: string
  updatedAt: string
}

export interface SSHKey {
  id: number
  userId: number
  title: string
  fingerprint: string
  publicKey: string
  createdAt: string
}

export interface DeployKey {
  id: number
  title: string
  readOnly: boolean
  createdAt: string
  token?: string
}

export interface AccessToken {
  id: number
  title: string
  scopes: string
  expiresAt?: string | null
  createdAt: string
  token?: string
}

export interface RepoMember {
  userId: number
  username: string
  avatarUrl: string
  permission: 'read' | 'write' | 'admin'
  isOwner: boolean
  addedAt: string
}

export interface BranchProtection {
  id: number
  pattern: string
  requirePR: boolean
  blockForcePush: boolean
  allowedUsers: string
  createdAt: string
}

export interface BranchingModel {
  enabled: boolean
  featurePrefix: string
  bugfixPrefix: string
  releasePrefix: string
  hotfixPrefix: string
}

export interface MergeStrategies {
  allowMergeCommit: boolean
  allowSquash: boolean
  allowRebase: boolean
}

export interface Webhook {
  id: number
  title: string
  url: string
  events: string
  active: boolean
  hasSecret: boolean
  lastStatus: number
  lastDeliveredAt?: string | null
  createdAt: string
}

// ── Environment + Deployment (Phase 3A) ──────────────────────────────────────

export type DeployStatus =
  | 'pending'
  | 'in_progress'
  | 'success'
  | 'failure'
  | 'cancelled'

export interface Environment {
  id: number
  repoId: number
  name: string
  url: string
  protectionRules: string  // JSON: {"require_approval":true,"required_reviewers":1}
  createdAt: string
  updatedAt: string
}

export interface Deployment {
  id: number
  envId: number
  repoId: number
  sha: string
  ref: string
  status: DeployStatus
  triggeredBy: string
  description: string
  runId: number | null
  startedAt: string | null
  finishedAt: string | null
  createdAt: string
}

// EnvironmentWithLatest is what ListEnvironments returns — env + most recent deploy.
export interface EnvironmentWithLatest extends Environment {
  latestDeployment: Deployment | null
}

// ── Workspaces + Secrets (Phase 3C) ──────────────────────────────────────────

export type WorkspaceRole = 'owner' | 'admin' | 'member'

export interface Workspace {
  id: number
  handle: string
  displayName: string
  description: string
  avatarUrl: string
  createdBy: number
  createdAt: string
  updatedAt: string
}

export interface WorkspaceWithMeta extends Workspace {
  memberCount: number
  repoCount: number
  myRole: string
}

export interface WorkspaceMember {
  id: number
  workspaceId: number
  userId: number
  username: string
  role: WorkspaceRole
  addedAt: string
}

export interface SecretListItem {
  id: number
  name: string
  createdAt: string
  updatedAt: string
}

// ── Unified Operational Timeline (Phase 3B) ──────────────────────────────────

export interface CommitEvent {
  type: 'commit'
  timestamp: string
  sha: string
  message: string
  author: string
}

export interface RunEvent {
  type: 'run'
  timestamp: string
  runId: number
  triggerRef: string
  triggerSha: string
  triggeredBy: string
  runStatus: string
  startedAt: string | null
  finishedAt: string | null
}

export interface DeploymentEvent {
  type: 'deployment'
  timestamp: string
  deploymentId: number
  envName: string
  deployStatus: string
  deployedSha: string
  deployRef: string
  triggeredBy: string
  description: string
  runLink: number | null
}

export type TimelineEvent = CommitEvent | RunEvent | DeploymentEvent

export interface ApiError {
  error: string
  status: number
}

export interface HealthResponse {
  status: 'ok'
}

// ── SBOM (Phase 4.2) ───────────────────────────────────────────────────────────

export interface SBOMReport {
  id: number
  repoId: number
  runId: number
  sha: string
  format: string
  componentCount: number
  generatedAt: string
}

// ── Secret Scanning (Phase 4.4) ────────────────────────────────────────────────

export interface SecretLeak {
  id: number
  repoId: number
  commitSha: string
  ref: string
  patternName: string
  description: string
  severity: string
  matchSample: string
  dismissed: boolean
  dismissedBy?: string
  dismissedAt?: string | null
  detectedAt: string
}

// ── Vulnerability Scanning (Phase 4.5) ─────────────────────────────────────────

export interface VulnerabilityFinding {
  id: number
  repoId: number
  vulnId: string
  purl: string
  version: string
  summary: string
  details?: string
  cvssScore: number
  fixedVersion: string
  dismissed: boolean
  dismissedBy?: string
  dismissedAt?: string | null
  detectedAt: string
}
