import client from './client'

export function login(data: { tenantCode: string; username: string; password: string }) {
  return client.post('/api/v1/account/login', data, {
    headers: { 'X-Tenant-Code': data.tenantCode },
  })
}

export function createTenant(data: {
  tenantCode: string
  tenantName: string
  inboundWebhookUrl?: string
  initAdminUsername?: string
  initAdminPassword?: string
}) {
  return client.post('/api/v1/account/tenant', data)
}

export function updateTenant(
  id: number,
  data: { tenantName: string; inboundWebhookUrl?: string; status?: number }
) {
  return client.put(`/api/v1/account/tenant/${id}`, data)
}

export function deleteTenant(id: number) {
  return client.delete(`/api/v1/account/tenant/${id}`)
}

export function listTenants() {
  return client.get('/api/v1/account/tenant/list')
}

export function listRoles() {
  return client.get('/api/v1/account/role/list')
}

export function createUser(
  tenantCode: string,
  data: {
    username: string
    password: string
    realName?: string
    phone?: string
    roleCodes?: string[]
  }
) {
  return client.post('/api/v1/account/user', data, {
    headers: { 'X-Tenant-Code': tenantCode },
  })
}

export function updateUser(
  id: number,
  data: {
    realName: string
    phone?: string
    status?: number
    roleCodes?: string[]
  }
) {
  return client.put(`/api/v1/account/user/${id}`, data)
}

export function deleteUser(id: number) {
  return client.delete(`/api/v1/account/user/${id}`)
}

export function listUsers(tenantCode?: string) {
  return client.get('/api/v1/account/user/list', {
    params: tenantCode ? { tenantCode } : {},
  })
}

export function me() {
  return client.get('/api/v1/account/me')
}
