export type FeedbackType = "bug" | "feature" | "docs" | "task" | "other";

export type FeedbackArea =
  | "reporting"
  | "followup"
  | "overview_graph"
  | "advisor"
  | "import_export"
  | "i18n"
  | "privacy"
  | "ui"
  | "accessibility"
  | "legal"
  | "api"
  | "backend"
  | "deployment"
  | "pwa"
  | "security"
  | "performance";

export type FeedbackChoice = {
  key: string;
  label: string;
  technical?: boolean;
  specialNotice?: boolean;
};

export const feedbackTypes: FeedbackChoice[] = [
  { key: "bug", label: "feedback.typeBug" },
  { key: "feature", label: "feedback.typeFeature" },
  { key: "docs", label: "feedback.typeDocs" },
  { key: "task", label: "feedback.typeTask" },
  { key: "other", label: "feedback.typeOther" }
];

export const feedbackAreas: FeedbackChoice[] = [
  { key: "reporting", label: "feedback.areaReporting" },
  { key: "followup", label: "feedback.areaFollowup" },
  { key: "overview_graph", label: "feedback.areaGraph" },
  { key: "advisor", label: "feedback.areaAdvisor" },
  { key: "import_export", label: "feedback.areaImportExport" },
  { key: "i18n", label: "feedback.areaI18n" },
  { key: "privacy", label: "feedback.areaPrivacy" },
  { key: "ui", label: "feedback.areaUi" },
  { key: "accessibility", label: "feedback.areaAccessibility" },
  { key: "legal", label: "feedback.areaLegal", specialNotice: true },
  { key: "api", label: "feedback.areaApi", technical: true },
  { key: "backend", label: "feedback.areaBackend", technical: true },
  { key: "deployment", label: "feedback.areaDeployment", technical: true },
  { key: "pwa", label: "feedback.areaPwa", technical: true },
  { key: "security", label: "feedback.areaSecurity", technical: true },
  { key: "performance", label: "feedback.areaPerformance", technical: true }
];

export const FEEDBACK_MIN_FORM_SECONDS = 5;
export const FEEDBACK_CONSENT_DELAY_MS = 1000;
export const FEEDBACK_MAX_CHARS = 4000;

export function isFeedbackEnabled() {
  return import.meta.env.VITE_FEEDBACK_ENABLED !== "false";
}

export function viewportCategory(width = window.innerWidth) {
  if (width < 640) return "mobile";
  if (width < 1024) return "tablet";
  return "desktop";
}

export function validateFeedbackMessage(message: string) {
  const trimmed = message.trim();
  const compact = trimmed.replace(/\s+/g, " ");
  const words = compact ? compact.split(" ").filter(Boolean) : [];
  const letterCount = (trimmed.match(/[A-Za-zÆØÅæøå]/g) ?? []).length;
  const errors: string[] = [];

  if (trimmed.length < 30) errors.push("feedback.validationMinChars");
  if (trimmed.length > FEEDBACK_MAX_CHARS) errors.push("feedback.validationMaxChars");
  if (words.length < 5) errors.push("feedback.validationMinWords");
  if (letterCount < 15) errors.push("feedback.validationMinLetters");
  if (/^[\d\W_]+$/u.test(trimmed)) errors.push("feedback.validationMeaningful");
  if (/(.)\1{11,}/u.test(trimmed)) errors.push("feedback.validationRepetition");
  if (/[^\s@]+@[^\s@]+\.[^\s@]+/.test(trimmed)) errors.push("feedback.validationContact");
  if (/(?:\+?\d[\s().-]*){8,}/.test(trimmed)) errors.push("feedback.validationContact");
  if (/\b\d{11}\b/.test(trimmed)) errors.push("feedback.validationContact");
  if (/\b\d{8,}\b/.test(trimmed)) errors.push("feedback.validationContact");
  if (/\b(?:\d{1,3}\.){3}\d{1,3}\b/.test(trimmed)) errors.push("feedback.validationContact");

  return {
    ok: errors.length === 0,
    errors: Array.from(new Set(errors)),
    normalized: compact
  };
}

export function validateFeedbackSelection(type: string, areas: string[]) {
  const errors: string[] = [];
  if (!feedbackTypes.some((item) => item.key === type)) errors.push("feedback.validationType");
  if (areas.length < 1 || areas.some((area) => !feedbackAreas.some((item) => item.key === area))) {
    errors.push("feedback.validationArea");
  }
  return errors;
}
