// 输入账号（JSON 文件格式）
export interface AccountInput {
  account: string // 邮箱
  password: string // 密码
  token: string // access_token
}

// 账号状态
export type AccountStatus = 'pending' | 'checking' | 'active' | 'banned' | 'token_expired' | 'error'

// 检查后的账号
export interface CheckedAccount extends AccountInput {
  id: number // 本地序号
  status: AccountStatus
  accountId?: string // ChatGPT workspace_id
  planType?: string
  error?: string
}

// 加号记录
export interface AddRecord {
  id: string
  timestamp: string
  total: number
  success: number
  failed: number
  source: 'manual' | 'auto'
  details?: string
}

// S2A Dashboard 统计
export interface DashboardStats {
  total_accounts: number
  normal_accounts: number
  error_accounts: number
  ratelimit_accounts: number
  overload_accounts: number
  today_requests: number
  today_tokens: number
  today_cost: number
  total_requests: number
  total_tokens: number
  total_cost: number
  rpm: number
  tpm: number
}

// S2A 账号
export interface S2AAccount {
  id: number
  name: string
  notes?: string
  platform: 'openai' | 'anthropic' | 'gemini'
  type: 'oauth' | 'access_token' | 'apikey' | 'setup-token'
  credentials: Record<string, unknown>
  extra?: Record<string, unknown>
  proxy_id?: number
  concurrency: number
  priority: number
  rate_multiplier?: number
  status: 'active' | 'inactive' | 'error'
  error_message?: string
  schedulable: boolean
  last_used_at?: string
  expires_at?: string
  auto_pause_on_expired: boolean
  created_at: string
  updated_at: string
  // 实时字段
  current_concurrency?: number
  current_window_cost?: number
  active_sessions?: number
}

// 分页响应
export interface PaginatedResponse<T> {
  data: T[]
  total: number
  page: number
  page_size: number
  total_pages: number
}

// 账号列表查询参数
export interface AccountListParams {
  page?: number
  page_size?: number
  platform?: 'openai' | 'anthropic' | 'gemini'
  type?: 'oauth' | 'access_token' | 'apikey'
  status?: 'active' | 'inactive' | 'error'
  search?: string
}

// 创建账号请求
export interface CreateAccountRequest {
  name: string
  platform: 'openai' | 'anthropic' | 'gemini'
  type: 'access_token'
  credentials: {
    access_token: string
    refresh_token?: string
    email?: string
  }
  concurrency?: number
  priority?: number
  group_ids?: number[]
  proxy_id?: number | null
  auto_pause_on_expired?: boolean
}

// OAuth 创建账号请求
export interface OAuthCreateRequest {
  session_id: string
  code: string
  name?: string
  concurrency?: number
  priority?: number
  group_ids?: number[]
  proxy_id?: number | null
}

// 分组
export interface Group {
  id: number
  name: string
  description?: string
  created_at: string
  updated_at: string
}

// 代理
export interface Proxy {
  id: number
  name: string
  url: string
  status: 'active' | 'inactive' | 'error'
  created_at: string
  updated_at: string
}

// 趋势数据
export interface TrendData {
  date: string
  requests: number
  tokens: number
  cost: number
}

// 邮箱服务配置
export interface MailServiceConfig {
  name: string           // 服务名称
  apiBase: string        // API 地址
  apiToken: string       // API Token
  domain: string         // 邮箱域名
  emailPath?: string     // 获取邮件列表的 API 路径
  addUserApi?: string    // 创建用户的 API 路径
}

// 应用配置
export interface AppConfig {
  s2a: {
    apiBase: string
    adminKey: string
  }
  pooling: {
    concurrency: number
    priority: number
    groupIds: number[]
    proxyId: number | null
  }
  check: {
    concurrency: number
    timeout: number
  }
  email: {
    services: MailServiceConfig[]  // 多个邮箱服务配置
  }
}

// 默认配置
export const defaultConfig: AppConfig = {
  s2a: {
    apiBase: '',
    adminKey: '',
  },
  pooling: {
    concurrency: 1,
    priority: 0,
    groupIds: [],
    proxyId: null,
  },
  check: {
    concurrency: 20,
    timeout: 30000,
  },
  email: {
    services: [
      {
        name: 'esyteam',
        apiBase: 'https://mail.esyteam.edu.kg',
        apiToken: '',
        domain: 'esyteam.edu.kg',
      },
    ],
  },
}

// 检查结果
export interface CheckResult {
  status: AccountStatus
  accountId?: string
  planType?: string
  error?: string
}

// 测试结果
export interface TestResult {
  success: boolean
  message?: string
  latency?: number
}

// Account 类型别名 (对应 requirements.md A5)
// S2AAccount 已包含完整的 Account 数据结构
export type Account = S2AAccount
