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

export type TicketStatus =
  | "open"
  | "pending"
  | "processing"
  | "waiting_user"
  | "answered"
  | "resolved"
  | "closed"
export type TicketPriority = "low" | "normal" | "high" | "urgent"
export type TicketSenderType = "user" | "admin" | "system"
export type TicketEventType =
  | "created"
  | "user_replied"
  | "admin_replied"
  | "internal_note_added"
  | "assigned"
  | "status_changed"
  | "priority_changed"
  | "department_changed"
  | "closed"
  | "reopened"
  | "rated"

export interface TicketActor {
  id: number
  username: string
  nickname: string | null
  email: string | null
  phone: string | null
}

export interface TicketDepartment {
  id: number
  code: string | null
  name: string
  description: string | null
  is_enabled: boolean
  sort_order: number
  min_member_level: number | null
  auto_reply_enabled: boolean
  auto_reply_content: string | null
  created_at: string | null
  updated_at: string | null
}

export interface TicketInstance {
  id: number
  status: string
  hostname: string | null
  main_ip: string | null
  cpu: number | null
  memory: number | null
  in_bw: number | null
  out_bw: number | null
  expires_at: string | null
}

export interface TicketMessage {
  id: number
  ticket_id: number
  sender_type: TicketSenderType
  sender_id: number | null
  sender_name: string | null
  content: string
  attachments: Array<Record<string, unknown>>
  is_internal: boolean
  created_at: string | null
}

export interface TicketEvent {
  id: number
  ticket_id: number
  actor_type: TicketSenderType
  actor_id: number | null
  actor_name: string | null
  event_type: TicketEventType
  from_value: string | null
  to_value: string | null
  message: string | null
  meta: Record<string, unknown>
  created_at: string | null
}

export interface TicketSatisfaction {
  rating: number
  comment: string | null
  rated_at: string | null
}

export interface TicketSummary {
  id: number
  ticket_no: string
  subject: string
  status: TicketStatus
  priority: TicketPriority
  department_id: number | null
  department_name: string | null
  instance: TicketInstance | null
  assignee: TicketActor | null
  assigned_at: string | null
  last_message_at: string | null
  last_reply_by: TicketSenderType | null
  unread_count: number
  message_count: number
  satisfaction: TicketSatisfaction | null
  created_at: string | null
  updated_at: string | null
}

export interface TicketDetail extends TicketSummary {
  messages: TicketMessage[]
  events?: TicketEvent[]
}

export interface TicketCreatePayload {
  department_id: number
  subject: string
  content: string
  priority: TicketPriority
  instance_id?: number | null
  attachments?: Array<Record<string, unknown>>
}

export interface TicketReplyPayload {
  content: string
  attachments?: Array<Record<string, unknown>>
}

export interface TicketRatingPayload {
  rating: number
  comment?: string | null
}

export interface TicketAttachmentUploadResult {
  id: number
  attachment_id: number
  token: string
  name: string
  url: string
  thumbnail_url: string
  legacy_url: string
  path: string
  size: number
  content_type: string
}

export function listTicketDepartments() {
  return client.get<ApiResponse<TicketDepartment[]>>("/v1/tickets/departments")
}

export function listTicketInstances() {
  return client.get<ApiResponse<ServerItem[]>>("/v1/tickets/instances")
}

export function uploadTicketImage(file: File) {
  const form = new FormData()
  form.append("file", file)
  return client.post<ApiResponse<TicketAttachmentUploadResult>>("/v1/tickets/attachments/images", form, {
    headers: { "Content-Type": "multipart/form-data" },
  })
}

export function listTickets(params?: { status?: TicketStatus; limit?: number; offset?: number }) {
  return client.get<ApiResponse<TicketSummary[]>>("/v1/tickets", { params })
}

export function createTicket(payload: TicketCreatePayload) {
  return client.post<ApiResponse<TicketSummary>>("/v1/tickets", payload)
}

export function getTicket(ticketId: number | string) {
  return client.get<ApiResponse<TicketDetail>>(`/v1/tickets/${ticketId}`)
}

export function replyTicket(ticketId: number | string, payload: TicketReplyPayload) {
  return client.post<ApiResponse<TicketMessage>>(`/v1/tickets/${ticketId}/messages`, payload)
}

export function closeTicket(ticketId: number | string) {
  return client.post<ApiResponse<TicketSummary>>(`/v1/tickets/${ticketId}/close`)
}

export function rateTicket(ticketId: number | string, payload: TicketRatingPayload) {
  return client.post<ApiResponse<TicketSummary>>(`/v1/tickets/${ticketId}/rating`, payload)
}

export function reopenTicket(ticketId: number | string) {
  return client.post<ApiResponse<TicketSummary>>(`/v1/tickets/${ticketId}/reopen`)
}
