import request from '@/utils/http'

export type IdentityKind = 'admin' | 'portal'

export interface IdentityRecord {
  id: string
  kind: IdentityKind
  login: string
  display_name: string
  role: string
  permissions: string[]
  effective_permissions: string[]
  active: boolean
  auth_source: string
  totp_enabled: boolean
  locked_until?: string
  created_at: string
  updated_at: string
  department_id?: string
  department_name?: string
}

export interface IdentityInput {
  login: string
  display_name: string
  role: string
  password?: string
  permissions: string[]
  active: boolean
  department_id?: string
}

export interface DepartmentRecord {
  id: string
  code: string
  name: string
  description: string
  parent_id?: string
  parent_name?: string
  active: boolean
  user_count: number
  created_at: string
  updated_at: string
}

export interface DepartmentInput {
  code: string
  name: string
  description: string
  parent_id?: string
  active: boolean
}

export interface IdentityProviderRecord {
  id: string
  code: string
  display_name: string
  issuer_url: string
  client_id: string
  secret_configured: boolean
  redirect_uri: string
  portal_return_url: string
  scopes: string[]
  auto_provision: boolean
  default_department_id?: string
  enabled: boolean
  revision: number
  credential_kek_version: number
}

export interface IdentityProviderInput {
  code: string
  display_name: string
  issuer_url: string
  client_id: string
  client_secret?: string
  redirect_uri: string
  portal_return_url: string
  scopes: string[]
  auto_provision: boolean
  default_department_id?: string
  enabled: boolean
}

export interface SAMLProviderRecord {
  id: string
  kind: 'saml'
  code: string
  display_name: string
  metadata_url: string
  sp_entity_id: string
  acs_url: string
  portal_return_url: string
  email_attribute: string
  name_attribute: string
  auto_provision: boolean
  default_department_id?: string
  enabled: boolean
  revision: number
}

export type SAMLProviderInput = Omit<SAMLProviderRecord, 'id' | 'kind' | 'revision'>

function resource(kind: IdentityKind) {
  return kind === 'admin' ? 'admins' : 'portal-users'
}

export function fetchIdentities(kind: IdentityKind) {
  return request.get<IdentityRecord[]>({
    url: `/api/v1/admin/identities/${resource(kind)}`
  })
}

export function createIdentity(kind: IdentityKind, data: IdentityInput) {
  return request.post<IdentityRecord>({
    url: `/api/v1/admin/identities/${resource(kind)}`,
    params: data
  })
}

export function updateIdentity(kind: IdentityKind, id: string, data: IdentityInput) {
  return request.put<IdentityRecord>({
    url: `/api/v1/admin/identities/${resource(kind)}/${id}`,
    params: data
  })
}

export function fetchDepartments() {
  return request.get<DepartmentRecord[]>({ url: '/api/v1/admin/departments' })
}

export function createDepartment(data: DepartmentInput) {
  return request.post<DepartmentRecord>({ url: '/api/v1/admin/departments', params: data })
}

export function updateDepartment(id: string, data: DepartmentInput) {
  return request.put<DepartmentRecord>({ url: `/api/v1/admin/departments/${id}`, params: data })
}

export function fetchIdentityProviders() {
  return request.get<IdentityProviderRecord[]>({ url: '/api/v1/admin/identity-providers' })
}

export function createIdentityProvider(data: IdentityProviderInput) {
  return request.post<IdentityProviderRecord>({ url: '/api/v1/admin/identity-providers', params: data })
}

export function updateIdentityProvider(id: string, data: IdentityProviderInput) {
  return request.put<IdentityProviderRecord>({ url: `/api/v1/admin/identity-providers/${id}`, params: data })
}

export function fetchSAMLProviders() {
  return request.get<SAMLProviderRecord[]>({ url: '/api/v1/admin/saml-providers' })
}

export function createSAMLProvider(data: SAMLProviderInput) {
  return request.post<SAMLProviderRecord>({ url: '/api/v1/admin/saml-providers', params: data })
}

export function updateSAMLProvider(id: string, data: SAMLProviderInput) {
  return request.put<SAMLProviderRecord>({ url: `/api/v1/admin/saml-providers/${id}`, params: data })
}
