import client from "./client"
import type { ApiResponse } from "../types"

export type SecurityGroupType = "host" | "lightHost" | "hyperv" | "adsl"

export interface SecurityGroupCreatePayload {
  name: string
  hostid?: number
  type?: SecurityGroupType
  create_default_rule?: 0 | 1
}

export interface SecurityGroupCreateResult {
  id: number | null
}

export interface SecurityGroupUpdatePayload {
  name: string
}

export interface SecurityGroupItem {
  id: number | string
  name: string
  cloud_num: number
  rule_num: number
  username?: string | null
  create_time?: string | null
  type?: SecurityGroupType | string | null
}

export interface SecurityGroupRule {
  id: number | string
  description?: string | null
  direction?: string | null
  protocol?: string | null
  port?: string | null
  ip?: string | null
  lock?: number | string | null
  create_time?: string | null
}

export interface SecurityGroupRulePayload {
  description?: string
  direction: "in" | "out"
  protocol: string
  port: string
  ip: string
  lock?: 0 | 1
}

export interface SecurityGroupRuleProtocol {
  name: string
  value: string
  port?: string | null
}

export interface SecurityGroupDetail extends SecurityGroupItem {
  update_time?: string | null
  rule: SecurityGroupRule[]
}

export interface SecurityGroupMeta {
  total: number
  total_page: number
  page: number
  per_page: number
}

export interface SecurityGroupList {
  list: SecurityGroupItem[]
  meta: SecurityGroupMeta
}

export interface SecurityGroupListQuery {
  page?: number
  per_page?: number
  orderby?: "id" | "name" | "uid" | "cloud_num" | "rule_num"
  sort?: "asc" | "desc"
  search?: string
  list_type?: "all" | "page"
  type?: SecurityGroupType
  instance_id?: number
}

export interface SecurityGroupLinkPayload {
  instance_id: number
  type?: 0 | 1
}

export function listSecurityGroups(params?: SecurityGroupListQuery) {
  return client.get<ApiResponse<SecurityGroupList>>("/v1/security-groups", { params })
}

export function getSecurityGroup(id: number | string, getAllRule = true) {
  return client.get<ApiResponse<SecurityGroupDetail>>(
    `/v1/security-groups/${id}`,
    { params: { get_all_rule: getAllRule ? 1 : 0 } },
  )
}

export function createSecurityGroup(payload: SecurityGroupCreatePayload) {
  return client.post<ApiResponse<SecurityGroupCreateResult>>("/v1/security-groups", payload)
}

export function updateSecurityGroup(id: number | string, payload: SecurityGroupUpdatePayload) {
  return client.put<ApiResponse<SecurityGroupItem>>(`/v1/security-groups/${id}`, payload)
}

export function deleteSecurityGroup(id: number | string) {
  return client.delete<ApiResponse<Record<string, unknown>>>(`/v1/security-groups/${id}`)
}

export function linkSecurityGroup(id: number | string, payload: SecurityGroupLinkPayload) {
  return client.post<ApiResponse<Record<string, unknown>>>(`/v1/security-groups/${id}/links`, payload)
}

export function unlinkInstanceSecurityGroup(instanceId: number | string) {
  return client.delete<ApiResponse<Record<string, unknown>>>(`/v1/security-groups/instance/${instanceId}/link`)
}

export function listSecurityGroupRuleProtocols() {
  return client.get<ApiResponse<SecurityGroupRuleProtocol[]>>("/v1/security-group-rule-protocols")
}

export function createSecurityGroupRule(securityGroupId: number | string, payload: SecurityGroupRulePayload) {
  return client.post<ApiResponse<Record<string, unknown>>>(
    `/v1/security-groups/${securityGroupId}/rules`,
    payload,
  )
}

export function updateSecurityGroupRule(ruleId: number | string, payload: SecurityGroupRulePayload) {
  return client.put<ApiResponse<Record<string, unknown>>>(
    `/v1/security-group-rules/${ruleId}`,
    payload,
  )
}

export function deleteSecurityGroupRule(ruleId: number | string) {
  return client.delete<ApiResponse<Record<string, unknown>>>(`/v1/security-group-rules/${ruleId}`)
}
