export const statusOptions = [
  { label: 'Tous', value: 'all' },
  { label: 'Non fermes', value: 'not_closed' },
  { label: 'Ouverts', value: 'Open' },
  { label: 'Repondus', value: 'Replied' },
  { label: 'Resolus', value: 'Resolved' },
  { label: 'Fermes', value: 'Closed' },
]

export const priorityOptions = [
  { label: 'Urgent', value: 'Urgent' },
  { label: 'Haute', value: 'High' },
  { label: 'Moyenne', value: 'Medium' },
  { label: 'Basse', value: 'Low' },
]

export const columns = [
  { name: 'important', label: '', field: 'is_important', align: 'center', style: 'width:30px;padding:0' },
  { name: 'legacy_id', label: '#', field: 'legacy_ticket_id', align: 'left', sortable: true, style: 'width:70px' },
  { name: 'subject', label: 'Sujet', field: 'subject', align: 'left' },
  { name: 'customer_name', label: 'Client', field: 'customer_name', align: 'left', sortable: true },
  { name: 'issue_type', label: 'Département', field: 'issue_type', align: 'left' },
  { name: 'assigned', label: 'Assigné', field: 'assigned_staff', align: 'center', style: 'width:56px' },
  { name: 'opening_date', label: 'Date', field: 'opening_date', align: 'left', sortable: true },
  { name: 'priority', label: 'Priorite', field: 'priority', align: 'center', sortable: true },
  { name: 'sla', label: 'SLA', align: 'left' },
  { name: 'status', label: 'Statut', field: 'status', align: 'center' },
]

export function buildFilters ({ statusFilter, typeFilter, priorityFilter, search, ownerFilter, me, deptIssueTypes }) {
  const filters = {}
  if (statusFilter === 'not_closed') {
    filters.status = ['!=', 'Closed']
  } else if (statusFilter !== 'all') {
    filters.status = statusFilter
  }
  if (typeFilter) filters.issue_type = typeFilter
  if (priorityFilter) filters.priority = priorityFilter
  if (search?.trim()) {
    const q = search.trim()
    if (/^\d+$/.test(q)) {
      filters.legacy_ticket_id = parseInt(q)
    } else {
      filters.subject = ['like', '%' + q + '%']
    }
  }
  if (ownerFilter === 'mine' && me) {
    filters._assign = ['like', '%' + me + '%'] // « Mes tickets » = ASSIGNÉS à moi (comme assign_to de F), pas seulement créés (owner)
  } else if (ownerFilter === 'depts' && !typeFilter && deptIssueTypes && deptIssueTypes.length) {
    filters.issue_type = ['in', deptIssueTypes] // « Mes départements » = issue_types des FILES dont je suis membre (un type explicite, lui, a priorité)
  }
  return filters
}

export function getSortField (col) {
  // La colonne « Date » AFFICHE opening_date (vraie date du ticket) → on TRIE sur opening_date,
  // pas sur creation (= date d'import ERPNext, regroupée à la migration → dates affichées en désordre).
  if (col === 'opening_date') return 'opening_date'
  if (col === 'legacy_id') return 'legacy_ticket_id'
  return col || 'opening_date'
}
