export interface SearchParams {
  q: string
  from: string
  to: string
  subject: string
  hasWords: string
  doesNotHave: string
  has: string[]
  after: string
  before: string
  within: string
  size: string
  sizeUnit: "Mo" | "Ko"
  sizeOp: "gt" | "lt"
  /** Folder/label to search in. "all" = all except spam, "all-spam" = everything. */
  in: string
  excludeChats: boolean
}

export const EMPTY_SEARCH_PARAMS: SearchParams = {
  q: "",
  from: "",
  to: "",
  subject: "",
  hasWords: "",
  doesNotHave: "",
  has: [],
  after: "",
  before: "",
  within: "",
  size: "",
  sizeUnit: "Mo",
  sizeOp: "gt",
  in: "all",
  excludeChats: false,
}

export const DATE_RANGE_OPTIONS = [
  { value: "1d", label: "1 jour" },
  { value: "3d", label: "3 jours" },
  { value: "1w", label: "7 jours" },
  { value: "2w", label: "2 semaines" },
  { value: "1m", label: "1 mois" },
  { value: "2m", label: "2 mois" },
  { value: "6m", label: "6 mois" },
  { value: "1y", label: "1 an" },
] as const

export const SEARCH_IN_OPTIONS = [
  { value: "all", label: "Tous les messages" },
  { value: "all-spam", label: "Tous les messages, Spam et Corbeille inclus" },
  { value: "inbox", label: "Boîte de réception" },
  { value: "starred", label: "Messages suivis" },
  { value: "sent", label: "Messages envoyés" },
  { value: "drafts", label: "Brouillons" },
  { value: "spam", label: "Indésirables" },
  { value: "trash", label: "Corbeille" },
] as const

export function parseSearchParams(urlParams: URLSearchParams): SearchParams {
  return {
    q: urlParams.get("q") ?? "",
    from: urlParams.get("from") ?? "",
    to: urlParams.get("to") ?? "",
    subject: urlParams.get("subject") ?? "",
    hasWords: urlParams.get("hasWords") ?? "",
    doesNotHave: urlParams.get("doesNotHave") ?? "",
    has: urlParams.getAll("has"),
    after: urlParams.get("after") ?? "",
    before: urlParams.get("before") ?? "",
    within: urlParams.get("within") ?? "",
    size: urlParams.get("size") ?? "",
    sizeUnit: (urlParams.get("sizeUnit") as "Mo" | "Ko") || "Mo",
    sizeOp: (urlParams.get("sizeOp") as "gt" | "lt") || "gt",
    in: urlParams.get("in") ?? "all",
    excludeChats: urlParams.get("excludeChats") === "true",
  }
}

export function searchParamsToURLSearchParams(
  params: Partial<SearchParams>
): URLSearchParams {
  const out = new URLSearchParams()
  if (params.q) out.set("q", params.q)
  if (params.from) out.set("from", params.from)
  if (params.to) out.set("to", params.to)
  if (params.subject) out.set("subject", params.subject)
  if (params.hasWords) out.set("hasWords", params.hasWords)
  if (params.doesNotHave) out.set("doesNotHave", params.doesNotHave)
  if (params.has?.length) {
    for (const h of params.has) out.append("has", h)
  }
  if (params.after) out.set("after", params.after)
  if (params.before) out.set("before", params.before)
  if (params.within) out.set("within", params.within)
  if (params.size) {
    out.set("size", params.size)
    out.set("sizeUnit", params.sizeUnit ?? "Mo")
    out.set("sizeOp", params.sizeOp ?? "gt")
  }
  if (params.in && params.in !== "all") out.set("in", params.in)
  if (params.excludeChats) out.set("excludeChats", "true")
  return out
}

export function buildSearchUrl(params: Partial<SearchParams>): string {
  const qs = searchParamsToURLSearchParams(params).toString()
  return `/mail/search${qs ? `?${qs}` : ""}`
}

export function isSearchEmpty(params: SearchParams): boolean {
  return (
    !params.q &&
    !params.from &&
    !params.to &&
    !params.subject &&
    !params.hasWords &&
    !params.doesNotHave &&
    params.has.length === 0 &&
    !params.after &&
    !params.before &&
    !params.within &&
    !params.size
  )
}
