export interface ScriptCategoryResponse {
  id: string
  name: string
  slug: string
  description: string | null
  icon: string | null
  sort_order: number
  template_count: number
}

export interface ScriptTemplateListItem {
  id: string
  category_id: string
  team_id: string | null
  created_by: string | null
  name: string
  slug: string
  description: string | null
  tags: string[]
  complexity: 'beginner' | 'intermediate' | 'advanced' // must match backend ScriptComplexity enum exactly
  estimated_runtime: string | null
  requires_elevation: boolean
  requires_modules: string[]
  is_verified: boolean
  usage_count: number
  language?: string | null
}

export interface ScriptParameterOption {
  value: string
  label: string
}

export interface ScriptParameterValidation {
  min_value?: number   // matches backend field name (not 'min')
  max_value?: number   // matches backend field name (not 'max')
  pattern?: string
  min_length?: number
  max_length?: number
}

export interface ScriptParameter {
  key: string
  label: string
  type: 'text' | 'password' | 'select' | 'boolean' | 'multi_text' | 'number' | 'textarea'
  required: boolean
  placeholder: string | null
  group: string | null
  order: number
  help_text: string | null
  options: ScriptParameterOption[] | null // for select type
  default: string | boolean | number | null
  validation: ScriptParameterValidation | null
  sensitive: boolean
}

export interface ScriptParametersSchema {
  parameters: ScriptParameter[]
}

export interface ScriptTemplateDetail extends ScriptTemplateListItem {
  use_case: string | null
  script_body: string
  // NOTE: backend types this as `dict` — arrives as unknown at runtime.
  // Always access via cast: (detail.parameters_schema as ScriptParametersSchema).parameters ?? []
  parameters_schema: ScriptParametersSchema
  default_values: Record<string, unknown>   // template-level metadata; not used in Phase 2
  validation_rules: Record<string, unknown> // template-level metadata; not used in Phase 2
  version: number
  created_at: string
  updated_at: string
}

export interface ScriptGenerateRequest {
  template_id: string
  parameters: Record<string, unknown>
  session_id?: string // Legacy tree-based session
  ai_session_id?: string // FlowPilot AI session
}

export interface ScriptGenerateResponse {
  id: string        // generation UUID
  script: string    // rendered PowerShell
  warnings: string[]
  metadata: {
    template_name: string
    template_version: number
    requires_elevation: boolean
    [key: string]: unknown
  }
}

export interface ScriptGenerationRecord {
  id: string
  template_id: string
  template_name: string
  parameters_used: Record<string, unknown> // sensitive values already redacted by backend
  created_at: string
}

export interface ScriptTemplateCreateRequest {
  category_id: string
  name: string
  description?: string | null
  use_case?: string | null
  script_body: string
  parameters_schema: ScriptParametersSchema
  tags?: string[]
  complexity?: 'beginner' | 'intermediate' | 'advanced'
  estimated_runtime?: string | null
  requires_elevation?: boolean
  requires_modules?: string[]
}

export interface ScriptTemplateUpdateRequest {
  name?: string
  description?: string | null
  use_case?: string | null
  script_body?: string
  parameters_schema?: ScriptParametersSchema
  tags?: string[]
  complexity?: 'beginner' | 'intermediate' | 'advanced'
  estimated_runtime?: string | null
  requires_elevation?: boolean
  requires_modules?: string[]
}

export interface ParameterCandidate {
  variableName: string
  suggestedKey: string
  suggestedLabel: string
  suggestedType: ScriptParameter['type']
  sensitive: boolean
  defaultValue: string | boolean | number | null
  source: 'param_block' | 'assignment'
  lineNumber: number
  matchedLine: string
  inferenceReason: string
}
