import {
  formatLocalizedSeoTitle,
  getListSeoTemplates,
  getSeoPage,
  interpolateTemplate,
} from "@/lib/i18n/seo";
import { DEFAULT_LANGUAGE, type AppLanguage } from "@/lib/i18n/registry";

export type ProjectListFilters = {
  expertise?: string;
  most_requests?: string;
  most_price?: string;
  age?: string;
  gender?: string;
};

export function getProjectsListDescription(lang: AppLanguage = DEFAULT_LANGUAGE) {
  return getSeoPage("projects", lang).description;
}

/** @deprecated Use getProjectsListDescription(lang) */
export const PROJECTS_LIST_DESCRIPTION = getProjectsListDescription();

function buildFilterParts(
  filters: ProjectListFilters,
  lang: AppLanguage
): string[] {
  const filterLabels = getListSeoTemplates(lang).projects.filters as Record<
    string,
    string
  >;
  const parts: string[] = [];

  if (filters.expertise) {
    parts.push(filters.expertise);
  }
  if (filters.age && filters.age !== "all") {
    if (filters.age === "18-25" && filterLabels.age18_25) {
      parts.push(filterLabels.age18_25);
    } else if (filters.age === "old" && filterLabels.ageOld) {
      parts.push(filterLabels.ageOld);
    }
  }
  if (filters.gender === "female" && filterLabels.genderFemale) {
    parts.push(filterLabels.genderFemale);
  } else if (filters.gender === "male" && filterLabels.genderMale) {
    parts.push(filterLabels.genderMale);
  }
  if (filters.most_price && filterLabels.mostPrice) {
    parts.push(filterLabels.mostPrice);
  }
  if (filters.most_requests && filterLabels.mostRequests) {
    parts.push(filterLabels.mostRequests);
  }

  return parts;
}

export function buildProjectsListPath(filters: ProjectListFilters): string {
  const params = new URLSearchParams();

  if (filters.expertise) params.set("expertise", filters.expertise);
  if (filters.most_requests) params.set("most_requests", filters.most_requests);
  if (filters.most_price) params.set("most_price", filters.most_price);
  if (filters.age) params.set("age", filters.age);
  if (filters.gender) params.set("gender", filters.gender);

  const query = params.toString();
  return query ? `/projects?${query}` : "/projects";
}

export function buildProjectsListSeo(
  filters: ProjectListFilters,
  lang: AppLanguage = DEFAULT_LANGUAGE
) {
  const projectsPage = getSeoPage("projects", lang);
  const templates = getListSeoTemplates(lang).projects as Record<string, string>;
  const filterParts = buildFilterParts(filters, lang);
  const hasFilters = filterParts.length > 0;

  if (!hasFilters) {
    return {
      title: projectsPage.title,
      description: projectsPage.description,
      path: projectsPage.path,
      h1: projectsPage.h1 ?? templates.baseTitle,
    };
  }

  const filterSegment = `${templates.filterPrefix}${filterParts.join(" ")}`;
  const titleCore = `${templates.baseTitle}${filterSegment}`.trim();

  return {
    title: formatLocalizedSeoTitle(titleCore, lang),
    description: interpolateTemplate(templates.filteredDescription, {
      filters: filterParts.join(" "),
    }),
    path: buildProjectsListPath(filters),
    h1: titleCore,
  };
}
