import api from '../utils/request'

export interface Application {
  id: number
  app_id: string
  owner_id: number
  app_name: string
  icon_url?: string
  protocol_type: 'OIDC' | 'SIMPLE_API'
  redirect_uris?: string
  notification_url?: string
  created_at: string
}

export interface ApplicationCreate {
  app_name: string
  icon_url?: string
  protocol_type: 'OIDC' | 'SIMPLE_API'
  redirect_uris?: string
  notification_url?: string
  password?: string
  verification_code?: string
}

export interface AppSecretResponse {
  app_id: string
  app_secret: string
  access_token: string
}

export interface ApplicationListResponse {
  total: number
  items: Application[]
}

export interface MappingResponse {
  id: number
  app_id: number
  user_id: number
  mapped_key: string
  mapped_email?: string
  user_mobile: string
  user_status: string // Added
  new_user_created?: boolean
  generated_password?: string
}

export interface MappingListResponse {
  total: number
  items: MappingResponse[]
}

// Logs
export interface OperationLog {
    id: number
    app_id: number
    action_type: 'MANUAL_ADD' | 'DELETE' | 'UPDATE' | 'IMPORT' | 'TRANSFER' | 'VIEW_SECRET' | 'REGENERATE_SECRET' | 'SYNC'
    target_mobile?: string
    operator_mobile?: string
    details?: any
    created_at: string
}

export interface LogListResponse {
    total: number
    items: OperationLog[]
}

export interface LogQueryParams {
    skip: number
    limit: number
    action_type?: string
    keyword?: string
    start_date?: string
    end_date?: string
}

export const getApps = (skip = 0, limit = 10, search = '') => {
  return api.get<ApplicationListResponse>('/apps/', { params: { skip, limit, search } })
}

export const getApp = (id: number) => {
  return api.get<Application>(`/apps/${id}`)
}

export const createApp = (data: ApplicationCreate) => {
  return api.post<AppSecretResponse>('/apps/', data)
}

export const updateApp = (id: number, data: Partial<ApplicationCreate>) => {
  return api.put<Application>(`/apps/${id}`, data)
}

export const transferApp = (id: number, data: { target_mobile: string, password: string, verification_code: string }) => {
  return api.post<Application>(`/apps/${id}/transfer`, data)
}

export const deleteApp = (id: number) => {
  return api.delete<Application>(`/apps/${id}`)
}

export const regenerateSecret = (id: number, data: { password: string, verification_code: string }) => {
  return api.post<AppSecretResponse>(`/apps/${id}/regenerate-secret`, data)
}

export const viewSecret = (id: number, password: string) => {
  return api.post<AppSecretResponse>(`/apps/${id}/view-secret`, { password })
}

// Mappings
export const getMappings = (appId: number, skip = 0, limit = 10) => {
  return api.get<MappingListResponse>(`/apps/${appId}/mappings`, { params: { skip, limit } })
}

export const createMapping = (appId: number, data: { mobile: string, mapped_key?: string, mapped_email?: string, password: string }) => {
  return api.post<MappingResponse>(`/apps/${appId}/mappings`, data)
}

export const updateMapping = (appId: number, mappingId: number, data: { mapped_key?: string, mapped_email?: string, password: string }) => {
  return api.put<MappingResponse>(`/apps/${appId}/mappings/${mappingId}`, data)
}

export const deleteMapping = (appId: number, mappingId: number, password: string) => {
  return api.delete(`/apps/${appId}/mappings/${mappingId}`, { data: { password } })
}

export const exportMappings = (appId: number) => {
  return api.get(`/apps/${appId}/mappings/export`, { responseType: 'blob' })
}

export const syncAppUsers = (appId: number) => {
  return api.post<{ message: string }>(`/apps/${appId}/sync-users`)
}

export interface AppSyncRequest {
    mode: 'ALL' | 'SELECTED'
    user_ids?: number[]
    init_email: boolean
    email_domain?: string
    verification_code: string
}

export const syncAppUsersV2 = (appId: number, data: AppSyncRequest) => {
  return api.post<{ message: string }>(`/apps/${appId}/sync-users-v2`, data)
}

export const getOperationLogs = (appId: number, params: LogQueryParams) => {
    return api.get<LogListResponse>(`/apps/${appId}/logs`, { params })
}
