import { request } from '@/api/request'

/** IPAM服务统一响应 */
export interface IPAMResponse<T> {
  code: number
  details: T
  message?: string
  timeseq?: number
}

/** IP地址运行状态 */
export type IPStatus = 'online' | 'offline' | 'unknown'

/** IP地址分配状态 */
export type AllocationStatus = 'allocated' | 'unallocated' | 'reserved'

/** IP地址使用状态 */
export type UsageStatus = 'used' | 'unused' | 'used_within_30d' | 'used_before_30d'

/** IP来源类型 */
export type SourceType = 'scan' | 'dhcp' | 'manual'

/** 概览统计 */
export interface IPAMOverview {
  total_ip: number
  allocated: number
  unallocated: number
  reserved: number
  used: number
  unused: number
  used_within_30d: number
  used_before_30d: number
  conflict_total: number
  conflict_unresolved: number
  subnet_usage_top10?: SubnetUsageItem[]
}

/** 子网使用率项 */
export interface SubnetUsageItem {
  subnet_id: number
  cidr: string
  name: string
  total: number
  used: number
  usage_percent: number
}

/** IP地址项 */
export interface IPAddressItem {
  id: number
  ip_address: string
  status: IPStatus
  allocation_status: AllocationStatus
  usage_status: UsageStatus
  subnet_id: number
  subnet_name?: string
  subnet_cidr?: string
  hostname: string
  mac_address: string
  last_used_at: string
  source_type: SourceType
  owner_type: string
  owner_id: number
  resource_uid?: string
  asset_id?: number
  remark: string
  tags: string
  created_at: string
  updated_at: string
}

/** IP地址列表参数 */
export interface IPAddressListParams {
  page?: number
  size?: number
  scan_id?: number
  subnet_id?: number
  status?: IPStatus
  allocation_status?: AllocationStatus
  usage_status?: UsageStatus
  keyword?: string
}

/** IP地址列表响应 */
export interface IPAddressListResponse {
  total: number
  page: number
  page_size: number
  data: IPAddressItem[]
}

/** IP地址表单数据 */
export interface IPAddressFormData {
  ip_address?: string
  status?: IPStatus
  allocation_status?: AllocationStatus
  subnet_id?: number
  hostname?: string
  mac_address?: string
  source_type?: SourceType
  owner_type?: string
  owner_id?: number
  resource_uid?: string
  asset_id?: number
  remark?: string
  tags?: string
}

/** IP分组项 */
export interface IPGroupItem {
  id: number
  name: string
  parent_id: number
  description: string
  is_default: boolean
  created_at: string
  updated_at: string
  children?: IPGroupItem[]
}

/** IP分组列表参数 */
export interface IPGroupListParams {
  parent_id?: number
  keyword?: string
}

/** IP分组列表响应 */
export interface IPGroupListResponse {
  list: IPGroupItem[]
  count: number
}

/** IP分组表单数据 */
export interface IPGroupFormData {
  name?: string
  parent_id?: number
  description?: string
}

/** IP子网项 */
export interface IPSubnetItem {
  id: number
  cidr: string
  name: string
  group_id: number
  group_name?: string
  gateway: string
  vlan: string
  resource_uid?: string
  asset_id?: number
  total: number
  used: number
  available: number
  reserved_ranges_json: string
  description: string
  created_at: string
  updated_at: string
}

/** IP子网列表参数 */
export interface IPSubnetListParams {
  page?: number
  size?: number
  group_id?: number
  resource_uid?: string
  asset_id?: number
  keyword?: string
}

/** IP子网列表响应 */
export interface IPSubnetListResponse {
  total: number
  page: number
  page_size: number
  data: IPSubnetItem[]
}

/** IP子网表单数据 */
export interface IPSubnetFormData {
  cidr?: string
  name?: string
  group_id?: number
  gateway?: string
  vlan?: string
  resource_uid?: string
  asset_id?: number
  reserved_ranges_json?: string
  description?: string
}

/** DHCP租约项 */
export interface DHCPLeaseItem {
  id: number
  ip_address: string
  mac_address: string
  hostname: string
  subnet_id: number
  subnet_name?: string
  lease_start: string
  lease_end: string
  dhcp_server: string
  status: string
  created_at: string
  updated_at: string
}

/** DHCP租约列表参数 */
export interface DHCPLeaseListParams {
  page?: number
  size?: number
  subnet_id?: number
  keyword?: string
}

/** DHCP租约列表响应 */
export interface DHCPLeaseListResponse {
  total: number
  page: number
  page_size: number
  data: DHCPLeaseItem[]
}

/** DHCP租约表单数据 */
export interface DHCPLeaseFormData {
  ip_address?: string
  mac_address?: string
  hostname?: string
  subnet_id?: number
  lease_start?: string
  lease_end?: string
  dhcp_server?: string
  status?: string
}

/** IP冲突项 */
export interface IPConflictItem {
  id: number
  ip_address: string
  mac_address_1: string
  mac_address_2: string
  hostname_1: string
  hostname_2: string
  subnet_id: number
  subnet_name?: string
  detected_at: string
  resolved_at: string
  status: string
  evidence_json: string
  created_at: string
  updated_at: string
}

/** IP冲突列表参数 */
export interface IPConflictListParams {
  page?: number
  size?: number
  subnet_id?: number
  status?: string
  keyword?: string
}

/** IP冲突列表响应 */
export interface IPConflictListResponse {
  total: number
  page: number
  page_size: number
  data: IPConflictItem[]
}

/** IP冲突表单数据 */
export interface IPConflictFormData {
  ip_address?: string
  mac_address_1?: string
  mac_address_2?: string
  hostname_1?: string
  hostname_2?: string
  subnet_id?: number
  evidence_json?: string
  status?: string
}

/** IP变更项 */
export interface IPChangeItem {
  id: number
  ip_address: string
  change_type: string
  before_json: string
  after_json: string
  subnet_id: number
  subnet_name?: string
  changed_by: string
  changed_at: string
  remark: string
  created_at: string
}

/** IP变更列表参数 */
export interface IPChangeListParams {
  page?: number
  size?: number
  subnet_id?: number
  change_type?: string
  keyword?: string
}

/** IP变更列表响应 */
export interface IPChangeListResponse {
  total: number
  page: number
  page_size: number
  data: IPChangeItem[]
}

/** IP变更表单数据 */
export interface IPChangeFormData {
  ip_address?: string
  change_type?: string
  before_json?: string
  after_json?: string
  subnet_id?: number
  changed_by?: string
  remark?: string
}

/** IP异常项 */
export interface IPAnomalyItem {
  id: number
  ip_address: string
  anomaly_type: string
  subnet_id: number
  subnet_name?: string
  detail_json: string
  detected_at: string
  status: string
  remark: string
  created_at: string
  updated_at: string
}

/** IP异常列表参数 */
export interface IPAnomalyListParams {
  page?: number
  size?: number
  subnet_id?: number
  anomaly_type?: string
  status?: string
  keyword?: string
}

/** IP异常列表响应 */
export interface IPAnomalyListResponse {
  total: number
  page: number
  page_size: number
  data: IPAnomalyItem[]
}

/** IP异常表单数据 */
export interface IPAnomalyFormData {
  ip_address?: string
  anomaly_type?: string
  subnet_id?: number
  detail_json?: string
  status?: string
  remark?: string
}

/** ========== 概览 API ========== */

/** 获取IPAM概览统计 */
export const fetchIPAMOverview = () => request.get<IPAMResponse<IPAMOverview>>('/DC-Control/v1/ipam/overview')

/** ========== IP地址 API ========== */

/** 获取IP地址列表 */
export const fetchIPAddressList = (params?: IPAddressListParams) =>
  request.get<IPAMResponse<IPAddressListResponse>>('/DC-Control/v1/ipaddresses', { params })

/** 获取IP地址详情 */
export const fetchIPAddressDetail = (id: number) => request.get<IPAMResponse<IPAddressItem>>(`/DC-Control/v1/ipaddresses/${id}`)

/** 创建IP地址 */
export const createIPAddress = (data: IPAddressFormData) => request.post<IPAMResponse<IPAddressItem>>('/DC-Control/v1/ipaddresses', data)

/** 更新IP地址 */
export const updateIPAddress = (id: number, data: Partial<IPAddressFormData>) =>
  request.put<IPAMResponse<{ message: string }>>(`/DC-Control/v1/ipaddresses/${id}`, data)

/** 删除IP地址 */
export const deleteIPAddress = (id: number) => request.delete<IPAMResponse<{ message: string }>>(`/DC-Control/v1/ipaddresses/${id}`)

/** ========== IP分组 API ========== */

/** 获取IP分组列表（树形） */
export const fetchIPGroupList = (params?: IPGroupListParams) =>
  request.get<IPAMResponse<IPGroupListResponse>>('/DC-Control/v1/ip-groups', { params })

/** 创建IP分组 */
export const createIPGroup = (data: IPGroupFormData) => request.post<IPAMResponse<IPGroupItem>>('/DC-Control/v1/ip-groups', data)

/** 更新IP分组 */
export const updateIPGroup = (id: number, data: Partial<IPGroupFormData>) =>
  request.put<IPAMResponse<{ message: string }>>(`/DC-Control/v1/ip-groups/${id}`, data)

/** 删除IP分组 */
export const deleteIPGroup = (id: number) => request.delete<IPAMResponse<{ message: string }>>(`/DC-Control/v1/ip-groups/${id}`)

/** ========== IP子网 API ========== */

/** 获取IP子网列表 */
export const fetchIPSubnetList = (params?: IPSubnetListParams) =>
  request.get<IPAMResponse<IPSubnetListResponse>>('/DC-Control/v1/ip-subnets', { params })

/** 获取IP子网详情 */
export const fetchIPSubnetDetail = (id: number) => request.get<IPAMResponse<IPSubnetItem>>(`/DC-Control/v1/ip-subnets/${id}`)

/** 创建IP子网 */
export const createIPSubnet = (data: IPSubnetFormData) => request.post<IPAMResponse<IPSubnetItem>>('/DC-Control/v1/ip-subnets', data)

/** 更新IP子网 */
export const updateIPSubnet = (id: number, data: Partial<IPSubnetFormData>) =>
  request.put<IPAMResponse<{ message: string }>>(`/DC-Control/v1/ip-subnets/${id}`, data)

/** 删除IP子网 */
export const deleteIPSubnet = (id: number) => request.delete<IPAMResponse<{ message: string }>>(`/DC-Control/v1/ip-subnets/${id}`)

/** ========== DHCP租约 API ========== */

/** 获取DHCP租约列表 */
export const fetchDHCPLeaseList = (params?: DHCPLeaseListParams) =>
  request.get<IPAMResponse<DHCPLeaseListResponse>>('/DC-Control/v1/ipam/dhcp-leases', { params })

/** 创建DHCP租约 */
export const createDHCPLease = (data: DHCPLeaseFormData) =>
  request.post<IPAMResponse<DHCPLeaseItem>>('/DC-Control/v1/ipam/dhcp-leases', data)

/** ========== IP冲突 API ========== */

/** 获取IP冲突列表 */
export const fetchIPConflictList = (params?: IPConflictListParams) =>
  request.get<IPAMResponse<IPConflictListResponse>>('/DC-Control/v1/ipam/conflicts', { params })

/** 创建IP冲突记录 */
export const createIPConflict = (data: IPConflictFormData) =>
  request.post<IPAMResponse<IPConflictItem>>('/DC-Control/v1/ipam/conflicts', data)

/** ========== IP变更 API ========== */

/** 获取IP变更列表 */
export const fetchIPChangeList = (params?: IPChangeListParams) =>
  request.get<IPAMResponse<IPChangeListResponse>>('/DC-Control/v1/ipam/changes', { params })

/** 创建IP变更记录 */
export const createIPChange = (data: IPChangeFormData) => request.post<IPAMResponse<IPChangeItem>>('/DC-Control/v1/ipam/changes', data)

/** ========== IP异常 API ========== */

/** 获取IP异常列表 */
export const fetchIPAnomalyList = (params?: IPAnomalyListParams) =>
  request.get<IPAMResponse<IPAnomalyListResponse>>('/DC-Control/v1/ipam/anomalies', { params })

/** 创建IP异常记录 */
export const createIPAnomaly = (data: IPAnomalyFormData) => request.post<IPAMResponse<IPAnomalyItem>>('/DC-Control/v1/ipam/anomalies', data)
