import axios from 'axios'

const BASE = '/api/v1/mock'

export interface TokenRequest {
  user_id?: string
  user_name?: string
  office_id?: string
  office_name?: string
  dept_id?: string
  dept_name?: string
  area_id?: string
  area_name?: string
  role_ids?: string[]
  expires_minutes?: number
}

export interface TokenResponse {
  access_token: string
  token_type: string
  expires_in: number
  user_id: string
  office_id: string
  dept_id: string
  area_id: string
  role_ids: string[]
}

export interface LoginRequest {
  username: string
  password: string
}

export interface LoginResponse {
  access_token: string
  token_type: string
  expires_in: number
  user_id: string
  username: string
  user_name: string
  office_id: string
  office_name: string
  dept_id: string
  dept_name: string
  area_id: string
  area_name: string
  role_ids: string[]
  role: string
}

export interface TestUserInfo {
  username: string
  user_name: string
  office_id: string
  office_name: string
  dept_id: string
  dept_name: string
  area_id: string
  area_name: string
  role_ids: string[]
  role: string
}

export interface DocListItem {
  doc_id: string
  content_hash: string
  title: string
  doc_number: string
  issuing_org: string
  doc_type: string
  publish_date: string
  chunk_count: number
  acl_ids: string[]
  status: string
  indexed_at: string
}

export interface TaskStatus {
  task_id: string
  status: string
  progress: number
  result?: Record<string, any> | null
  error?: string | null
}

export interface SystemStats {
  es: { chunks: number; documents: number } | { error: string }
  redis: { status: string }
  neo4j: Record<string, number> | { error: string }
}

export const mockApi = {
  /** Login with a predefined test account */
  login: (body: LoginRequest): Promise<LoginResponse> =>
    axios.post(`${BASE}/login`, body).then((r) => r.data),

  /** List all predefined test users (no passwords) */
  listUsers: (): Promise<TestUserInfo[]> =>
    axios.get(`${BASE}/users`).then((r) => r.data),

  generateToken: (body: TokenRequest): Promise<TokenResponse> =>
    axios.post(`${BASE}/token`, body).then((r) => r.data),

  generateDepts: (prefix: string, count: number): Promise<string[]> =>
    axios.post(`${BASE}/batch-depts`, { prefix, count }).then((r) => r.data),

  listDocs: (params?: { size?: number; from?: number; keyword?: string }): Promise<DocListItem[]> =>
    axios.get(`${BASE}/docs`, { params }).then((r) => r.data),

  getTaskStatus: (taskId: string): Promise<TaskStatus> =>
    axios.get(`${BASE}/task/${taskId}`).then((r) => r.data),

  deleteDoc: (docId: string): Promise<any> =>
    axios.delete(`${BASE}/doc/${docId}`).then((r) => r.data),

  getStats: (): Promise<SystemStats> =>
    axios.get(`${BASE}/stats`).then((r) => r.data),

  /** Upload document via webhook (same endpoint the real OA uses) */
  uploadDoc: (formData: FormData): Promise<{ doc_id: string; task_id: string; status: string }> =>
    axios
      .post('/api/v1/ingest/webhook/document', formData, {
        headers: { 'Content-Type': 'multipart/form-data' },
      })
      .then((r) => r.data),
}
