export type FeedbackType =
  | 'bug'
  | 'feature_request'
  | 'general_feedback'
  | 'ui_issue'
  | 'performance_issue'

export type FeedbackPriority = 'low' | 'medium' | 'high' | 'critical'

export type FeedbackStatus = 'open' | 'in_progress' | 'resolved' | 'closed' | 'rejected'

export type FeedbackDocument = {
  id: string
  name: string
  mime_type: string | null
  file_size: number | null
  type: 'image' | 'video' | string
  url?: string | null
}

export type FeedbackUserSummary = {
  id: string
  name: string
  email: string
  army_number?: string | null
}

export type FeedbackAssignedUser = {
  id: string
  name: string
  email: string
}

export type Feedback = {
  id: string
  type: FeedbackType
  title: string
  description: string
  priority: FeedbackPriority
  status: FeedbackStatus
  page_url: string | null
  browser_info: Record<string, unknown> | null
  images?: FeedbackDocument[]
  videos?: FeedbackDocument[]
  steps_to_reproduce: string | null
  expected_behavior: string | null
  actual_behavior: string | null
  additional_notes: string | null
  admin_notes: string | null
  resolved_at: string | null
  user?: FeedbackUserSummary | null
  assigned_user?: FeedbackAssignedUser | null
  created_at: string
  updated_at: string
}

export type FeedbackListItem = Feedback

export type FeedbackFormState = {
  type: FeedbackType
  title: string
  description: string
  priority: FeedbackPriority
  page_url: string
  steps_to_reproduce: string
  expected_behavior: string
  actual_behavior: string
  additional_notes: string
  images: File[]
  videos: File[]
}

export type SubmitFeedbackPayload = {
  type: FeedbackType
  title: string
  description: string
  priority: FeedbackPriority
  page_url?: string
  steps_to_reproduce?: string
  expected_behavior?: string
  actual_behavior?: string
  additional_notes?: string
  images?: File[]
  videos?: File[]
  screen_resolution?: string
  viewport_size?: string
  timezone?: string
}

export type UpdateFeedbackPayload = {
  type?: FeedbackType
  title?: string
  description?: string
  priority?: FeedbackPriority
  page_url?: string | null
  steps_to_reproduce?: string | null
  expected_behavior?: string | null
  actual_behavior?: string | null
  additional_notes?: string | null
  status?: FeedbackStatus
  assigned_to?: string | null
  admin_notes?: string | null
}

export type ListFeedbackParams = {
  page?: number
  per_page?: number
  search?: string
  sort_by?: string
  sort_order?: 'asc' | 'desc'
  type?: FeedbackType | ''
  status?: FeedbackStatus | ''
  priority?: FeedbackPriority | ''
}

export type FeedbackApiResponse = {
  success: boolean
  data: Feedback
  message?: string
}

export type FeedbackStatistics = {
  total: number
  open: number
  resolved: number
  by_type: Record<string, number>
  by_priority: Record<string, number>
  by_status: Record<string, number>
}

export const FEEDBACK_TYPE_OPTIONS: { label: string; value: FeedbackType }[] = [
  { label: 'Ralat / Bug', value: 'bug' },
  { label: 'Permintaan Ciri Baharu', value: 'feature_request' },
  { label: 'Maklum Balas Umum', value: 'general_feedback' },
  { label: 'Isu Antara Muka', value: 'ui_issue' },
  { label: 'Isu Prestasi', value: 'performance_issue' },
]

export const FEEDBACK_PRIORITY_OPTIONS: { label: string; value: FeedbackPriority }[] = [
  { label: 'Rendah', value: 'low' },
  { label: 'Sederhana', value: 'medium' },
  { label: 'Tinggi', value: 'high' },
  { label: 'Kritikal', value: 'critical' },
]

export const FEEDBACK_STATUS_OPTIONS: { label: string; value: FeedbackStatus }[] = [
  { label: 'Terbuka', value: 'open' },
  { label: 'Dalam Proses', value: 'in_progress' },
  { label: 'Diselesaikan', value: 'resolved' },
  { label: 'Ditutup', value: 'closed' },
  { label: 'Ditolak', value: 'rejected' },
]

export function feedbackTypeLabel(type: FeedbackType | string): string {
  return FEEDBACK_TYPE_OPTIONS.find((option) => option.value === type)?.label ?? type
}

export function feedbackPriorityLabel(priority: FeedbackPriority | string): string {
  return FEEDBACK_PRIORITY_OPTIONS.find((option) => option.value === priority)?.label ?? priority
}

export function feedbackStatusLabel(status: FeedbackStatus | string): string {
  return FEEDBACK_STATUS_OPTIONS.find((option) => option.value === status)?.label ?? status
}

export function createEmptyFeedbackForm(): FeedbackFormState {
  return {
    type: 'general_feedback',
    title: '',
    description: '',
    priority: 'medium',
    page_url: typeof window !== 'undefined' ? window.location.href : '',
    steps_to_reproduce: '',
    expected_behavior: '',
    actual_behavior: '',
    additional_notes: '',
    images: [],
    videos: [],
  }
}
