/**
 * API V2 Type Definitions
 * External Authentication & Task Management
 */

// ==================== Authentication ====================

export interface UserInfo {
  id: number
  email: string
  display_name: string | null
}

export interface LoginResponseV2 {
  access_token: string
  token_type: string
  expires_in: number
  user: UserInfo
}

export interface UserResponse {
  id: number
  email: string
  display_name: string | null
  created_at: string
  last_login: string | null
  is_active: boolean
}

export interface SessionInfo {
  id: number
  token_type: string
  expires_at: string
  issued_at: string
  ip_address: string | null
  user_agent: string | null
  created_at: string
  last_accessed_at: string
  is_expired: boolean
  time_until_expiry: number
}

// ==================== Task Management ====================

export type TaskStatus = 'pending' | 'processing' | 'completed' | 'failed'

export interface TaskCreate {
  filename?: string
  file_type?: string
}

export interface TaskUpdate {
  status?: TaskStatus
  error_message?: string
  processing_time_ms?: number
  result_json_path?: string
  result_markdown_path?: string
  result_pdf_path?: string
}

export interface Task {
  id: number
  user_id: number
  task_id: string
  filename: string | null
  file_type: string | null
  status: TaskStatus
  result_json_path: string | null
  result_markdown_path: string | null
  result_pdf_path: string | null
  error_message: string | null
  processing_time_ms: number | null
  created_at: string
  updated_at: string
  completed_at: string | null
  file_deleted: boolean
}

export interface TaskFile {
  id: number
  original_name: string | null
  stored_path: string | null
  file_size: number | null
  mime_type: string | null
  file_hash: string | null
  created_at: string
}

export interface TaskDetail extends Task {
  files: TaskFile[]
}

export interface TaskListResponse {
  tasks: Task[]
  total: number
  page: number
  page_size: number
  has_more: boolean
}

export interface TaskStats {
  total: number
  pending: number
  processing: number
  completed: number
  failed: number
}

// ==================== Task Filters ====================

export interface TaskFilters {
  status?: TaskStatus
  page: number
  page_size: number
  order_by: string
  order_desc: boolean
}

// ==================== Admin Types ====================

export interface SystemStats {
  total_users: number
  active_users: number
  total_tasks: number
  total_sessions: number
  recent_activity_count: number
  task_stats: {
    pending: number
    processing: number
    completed: number
    failed: number
  }
}

export interface UserWithStats {
  id: number
  email: string
  display_name: string | null
  created_at: string
  last_login: string | null
  is_active: boolean
  task_count: number
  completed_tasks: number
  failed_tasks: number
}

export interface TopUser {
  user_id: number
  email: string
  display_name: string | null
  task_count: number
  completed_tasks: number
}

export interface AuditLog {
  id: number
  user_id: number
  user_email: string
  category: 'auth' | 'task' | 'file' | 'admin' | 'system'
  action: string
  resource_type: string | null
  resource_id: string | null
  success: boolean
  error_message: string | null
  extra_data: string | null
  created_at: string
}

export interface AuditLogListResponse {
  logs: AuditLog[]
  total: number
  page: number
  page_size: number
  has_more: boolean
}

export interface UserActivitySummary {
  user_id: number
  email: string
  display_name: string | null
  total_actions: number
  successful_actions: number
  failed_actions: number
  categories: Record<string, number>
  recent_actions: AuditLog[]
}
