/**
 * Brainfish API Documentation Types
 * 
 * These types are inspired by Hoppscotch's data structures but adapted for Brainfish.
 * Ported from Hoppscotch's HoppCollection and HoppRESTRequest types.
 */

// ============================================================================
// Core Request Types
// ============================================================================

export type HTTPMethod = 
  | 'GET' 
  | 'POST' 
  | 'PUT' 
  | 'DELETE' 
  | 'PATCH' 
  | 'HEAD' 
  | 'OPTIONS'

export type ContentType = 
  | 'application/json'
  | 'application/xml'
  | 'text/plain'
  | 'text/html'
  | 'application/x-www-form-urlencoded'
  | 'multipart/form-data'
  | 'application/octet-stream'
  | null

// ============================================================================
// Request Parameter Types
// ============================================================================

export interface BrainfishRESTParam {
  key: string
  value: string
  active: boolean
  description: string
}

export interface BrainfishRESTHeader {
  key: string
  value: string
  active: boolean
  description: string
}

export interface BrainfishRESTRequestVariable {
  key: string
  value: string
  active: boolean
}

export interface FormDataKeyValue {
  key: string
  value: string | File | Blob
  isFile: boolean
  active: boolean
  contentType?: string
}

// ============================================================================
// Request Body Types
// ============================================================================

export interface BrainfishRESTReqBody {
  contentType: ContentType
  body: string | FormDataKeyValue[] | null
}

// ============================================================================
// Authentication Types
// ============================================================================

export interface BrainfishRESTAuthNone {
  authType: 'none'
  authActive: boolean
}

export interface BrainfishRESTAuthInherit {
  authType: 'inherit'
  authActive: boolean
}

export interface BrainfishRESTAuthBasic {
  authType: 'basic'
  authActive: boolean
  username: string
  password: string
}

export interface BrainfishRESTAuthBearer {
  authType: 'bearer'
  authActive: boolean
  token: string
}

export interface BrainfishRESTAuthAPIKey {
  authType: 'api-key'
  authActive: boolean
  addTo: 'HEADERS' | 'QUERY_PARAMS'
  key: string
  value: string
}

export interface BrainfishRESTAuthOAuth2 {
  authType: 'oauth-2'
  authActive: boolean
  grantTypeInfo: {
    grantType: 
      | 'AUTHORIZATION_CODE'
      | 'IMPLICIT'
      | 'PASSWORD'
      | 'CLIENT_CREDENTIALS'
    authEndpoint?: string
    tokenEndpoint?: string
    clientID: string
    clientSecret?: string
    username?: string
    password?: string
    token: string
    scopes: string
    isPKCE?: boolean
    clientAuthentication?: 'IN_BODY' | 'IN_HEADER'
    authRequestParams?: Array<{ key: string; value: string }>
    refreshRequestParams?: Array<{ key: string; value: string }>
    tokenRequestParams?: Array<{ key: string; value: string }>
  }
  addTo: 'HEADERS' | 'QUERY_PARAMS'
}

export type BrainfishRESTAuth = 
  | BrainfishRESTAuthNone
  | BrainfishRESTAuthInherit
  | BrainfishRESTAuthBasic
  | BrainfishRESTAuthBearer
  | BrainfishRESTAuthAPIKey
  | BrainfishRESTAuthOAuth2

// ============================================================================
// Response Types
// ============================================================================

export interface BrainfishRESTResponseOriginalRequest {
  name: string
  method: HTTPMethod
  endpoint: string
  params: BrainfishRESTParam[]
  headers: BrainfishRESTHeader[]
  body: BrainfishRESTReqBody
  auth: BrainfishRESTAuth
  requestVariables: BrainfishRESTRequestVariable[]
}

export interface BrainfishRESTRequestResponse {
  name: string
  status: string
  code: number
  headers: BrainfishRESTHeader[]
  body: string
  originalRequest: BrainfishRESTResponseOriginalRequest
}

export interface BrainfishRESTRequestResponses {
  [name: string]: BrainfishRESTRequestResponse
}

// ============================================================================
// Request Type
// ============================================================================

export interface BrainfishRESTRequest {
  id: string
  name: string
  description: string | null
  method: HTTPMethod
  endpoint: string
  params: BrainfishRESTParam[]
  headers: BrainfishRESTHeader[]
  auth: BrainfishRESTAuth
  body: BrainfishRESTReqBody
  requestVariables: BrainfishRESTRequestVariable[]
  responses: BrainfishRESTRequestResponses
  tags: string[]
}

// ============================================================================
// Documentation Page Types (MDX pages from starter template)
// ============================================================================

export interface BrainfishDocPage {
  id: string
  slug: string
  title: string
  description?: string
  icon?: string
  filePath: string
  group?: string
  order?: number
  /** Nested pages for collapsible groups */
  children?: BrainfishDocPage[]
  /** Whether this is a collapsible group header */
  isGroup?: boolean
}

export interface BrainfishDocGroup {
  id: string
  title: string
  pages: BrainfishDocPage[]
  order?: number
  /** Phosphor icon name for the group header (e.g., "rocket", "book-open", "sliders") */
  icon?: string
}

// ============================================================================
// Collection Variable Types
// ============================================================================

export interface BrainfishCollectionVariable {
  key: string
  value: string
  active: boolean
}

// ============================================================================
// Collection Type
// ============================================================================

export interface BrainfishCollection {
  id: string
  name: string
  description: string | null
  folders: BrainfishCollection[]  // Nested folders (tags)
  requests: BrainfishRESTRequest[]
  variables: BrainfishCollectionVariable[]
  auth: BrainfishRESTAuth
  headers: BrainfishRESTHeader[]
  // Documentation pages from starter template
  docGroups?: BrainfishDocGroup[]
}

// ============================================================================
// Helper Type Guards
// ============================================================================

export function isBrainfishRESTAuthBasic(
  auth: BrainfishRESTAuth
): auth is BrainfishRESTAuthBasic {
  return auth.authType === 'basic'
}

export function isBrainfishRESTAuthBearer(
  auth: BrainfishRESTAuth
): auth is BrainfishRESTAuthBearer {
  return auth.authType === 'bearer'
}

export function isBrainfishRESTAuthAPIKey(
  auth: BrainfishRESTAuth
): auth is BrainfishRESTAuthAPIKey {
  return auth.authType === 'api-key'
}

export function isBrainfishRESTAuthOAuth2(
  auth: BrainfishRESTAuth
): auth is BrainfishRESTAuthOAuth2 {
  return auth.authType === 'oauth-2'
}
