import type { ReactElement } from "react";
import Container from "@/components/Container/Container";
import Section from "@/components/Section/Section";
import Marquee from "@/components/Marquee/Marquee";
import TitleCta from "@/components/TitleCta/TitleCta";

import Accenture from "../../../public/image/clients/accenture.svg";
import Apo from "../../../public/image/clients/apo.svg";
import Bbc from "../../../public/image/clients/bbc.svg";
import Butcombe from "../../../public/image/clients/butcombe.svg";
import Coates from "../../../public/image/clients/coates.svg";
import Dsaa from "../../../public/image/clients/DSAA-Logo.svg";
import DigitalSomerset from "../../../public/image/clients/digitalsomerset.svg";
import Discworld from "../../../public/image/clients/discworld.svg";
import ExmoorTea from "../../../public/image/clients/exmoortea.svg";
import Huish from "../../../public/image/clients/huish.svg";
import Hestercombe from "../../../public/image/clients/hestercombe.svg";
import GoldenHooves from "../../../public/image/clients/goldenhooves.svg";
import OpenMentalHealth from "../../../public/image/clients/open.svg";
import Palm from "../../../public/image/clients/palm.svg";
import Sba from "../../../public/image/clients/sba.svg";
import Spar from "../../../public/image/clients/spar.svg";
import TauntonBrewhouse from "../../../public/image/clients/tauntonbrewhouse.svg";
import WellingtonSchool from "../../../public/image/clients/welly.svg";
import Nhs from "../../../public/image/clients/nhs.svg";
import Queens from "../../../public/image/clients/queens-logo.svg";
import SomersetCouncil from "../../../public/image/clients/somerset-council.svg";
import Millfield from "../../../public/image/clients/millfield.svg";
import Sheppys from "../../../public/image/clients/sheppys-logo.svg";

export type ClientLogoKey =
  | "accenture"
  | "apo"
  | "bbc"
  | "butcombe"
  | "dsaa"
  | "digitalsomerset"
  | "discworld"
  | "exmoortea"
  | "huish"
  | "nhs"
  | "somerset-council"
  | "millfield"
  | "coates"
  | "hestercombe"
  | "goldenhooves"
  | "open-mental-health"
  | "palm"
  | "sba"
  | "spar"
  | "taunton-brewhouse"
  | "wellington-school"
  | "queens"
  | "sheppys";

type ClientLogo = {
  key: ClientLogoKey;
  alt: string;
  renderItem: () => ReactElement;
  source: string;
  height: number;
  width: number;
};

const firstRowLogos: ClientLogo[] = [
  {
    key: "accenture",
    alt: "Accenture logo",
    renderItem: () => <Accenture height={73} width={278} />,
    source: "/image/clients/accenture.svg",
    height: 73,
    width: 278,
  },
  {
    key: "apo",
    alt: "Apo logo",
    renderItem: () => <Apo height={75} width={237} />,
    source: "/image/clients/apo.svg",
    height: 75,
    width: 237,
  },
  {
    key: "bbc",
    alt: "BBC logo",
    renderItem: () => <Bbc height={66} width={232} />,
    source: "/image/clients/bbc.svg",
    height: 66,
    width: 232,
  },
  {
    key: "butcombe",
    alt: "Butcombe logo",
    renderItem: () => <Butcombe height={117} width={229} />,
    source: "/image/clients/butcombe.svg",
    height: 117,
    width: 229,
  },
  {
    key: "dsaa",
    alt: "DSAA logo",
    renderItem: () => <Dsaa height={117} width={117} />,
    source: "/image/clients/DSAA-Logo.svg",
    height: 117,
    width: 117,
  },
  {
    key: "digitalsomerset",
    alt: "Digital Somerset logo",
    renderItem: () => <DigitalSomerset height={100} width={207} />,
    source: "/image/clients/digitalsomerset.svg",
    height: 100,
    width: 207,
  },
  {
    key: "discworld",
    alt: "Discworld logo",
    renderItem: () => <Discworld height={100} width={275} />,
    source: "/image/clients/discworld.svg",
    height: 100,
    width: 275,
  },
  {
    key: "exmoortea",
    alt: "Exmoor Tea logo",
    renderItem: () => <ExmoorTea height={84} width={237} />,
    source: "/image/clients/exmoortea.svg",
    height: 84,
    width: 237,
  },
  {
    key: "huish",
    alt: "Huish logo",
    renderItem: () => <Huish height={90} width={165} />,
    source: "/image/clients/huish.svg",
    height: 90,
    width: 165,
  },
  {
    key: "nhs",
    alt: "NHS logo",
    renderItem: () => <Nhs height={88} width={219} />,
    source: "/image/clients/nhs.svg",
    height: 88,
    width: 219,
  },
  {
    key: "somerset-council",
    alt: "Somerset Council logo",
    renderItem: () => <SomersetCouncil height={96} width={347} />,
    source: "/image/clients/somerset-council.svg",
    height: 96,
    width: 347,
  },
  {
    key: "millfield",
    alt: "Millfield logo",
    renderItem: () => <Millfield height={77} width={335} />,
    source: "/image/clients/millfield.svg",
    height: 77,
    width: 335,
  },
];

const secondRowLogos: ClientLogo[] = [
  {
    key: "coates",
    alt: "Coates logo",
    renderItem: () => <Coates height={100} width={148} />,
    source: "/image/clients/coates.svg",
    height: 100,
    width: 148,
  },
  {
    key: "hestercombe",
    alt: "Hestercombe logo",
    renderItem: () => <Hestercombe height={100} width={100} />,
    source: "/image/clients/hestercombe.svg",
    height: 100,
    width: 100,
  },
  {
    key: "goldenhooves",
    alt: "Golden Hooves logo",
    renderItem: () => <GoldenHooves height={98} width={221} />,
    source: "/image/clients/goldenhooves.svg",
    height: 98,
    width: 221,
  },
  {
    key: "open-mental-health",
    alt: "Open Mental Health logo",
    renderItem: () => <OpenMentalHealth height={100} width={90} />,
    source: "/image/clients/open.svg",
    height: 100,
    width: 90,
  },
  {
    key: "palm",
    alt: "Palm logo",
    renderItem: () => <Palm height={100} width={126} />,
    source: "/image/clients/palm.svg",
    height: 100,
    width: 126,
  },
  {
    key: "sba",
    alt: "Somerset Business Awards logo",
    renderItem: () => <Sba height={100} width={231} />,
    source: "/image/clients/sba.svg",
    height: 100,
    width: 231,
  },
  {
    key: "spar",
    alt: "Spar logo",
    renderItem: () => <Spar height={132} width={89} />,
    source: "/image/clients/spar.svg",
    height: 132,
    width: 89,
  },
  {
    key: "taunton-brewhouse",
    alt: "Taunton Brewhouse logo",
    renderItem: () => <TauntonBrewhouse height={99} width={201} />,
    source: "/image/clients/tauntonbrewhouse.svg",
    height: 99,
    width: 201,
  },
  {
    key: "wellington-school",
    alt: "Wellington School logo",
    renderItem: () => <WellingtonSchool height={60} width={200} />,
    source: "/image/clients/welly.svg",
    height: 60,
    width: 200,
  },
  {
    key: "queens",
    alt: "Queen's logo",
    renderItem: () => <Queens height={67} width={332} />,
    source: "/image/clients/queens-logo.svg",
    height: 67,
    width: 332,
  },
  {
    key: "sheppys",
    alt: "Sheppy's logo",
    renderItem: () => <Sheppys height={100} width={250} />,
    source: "/image/clients/sheppys-logo.svg",
    height: 100,
    width: 250,
  },
];

const allLogosOrdered: ClientLogo[] = [...firstRowLogos, ...secondRowLogos];

const normalizeLogoKey = (key: string) => key.trim().toLowerCase();

export const getMarqueeLogoRows = (logoKeys?: string[]) => {
  if (logoKeys === undefined) {
    return { firstRow: firstRowLogos, secondRow: secondRowLogos };
  }

  const selectedKeys = new Set(logoKeys.map(normalizeLogoKey));
  const selected = allLogosOrdered.filter((logo) =>
    selectedKeys.has(logo.key),
  );

  if (selected.length <= 1) {
    return { firstRow: selected, secondRow: [] };
  }

  const splitAt = Math.ceil(selected.length / 2);
  return {
    firstRow: selected.slice(0, splitAt),
    secondRow: selected.slice(splitAt),
  };
};

type CollaborateSectionProps = {
  title?: string;
  ctaLabel?: string;
  ctaHref?: string;
  anchor?: string;
  titleSize?: "default" | "sm";
  /** When set, filters logos and splits them across marquees (homepage omits this). */
  logoKeys?: string[];
};

const CollaborateSection = ({
  title,
  ctaLabel,
  ctaHref,
  anchor,
  titleSize = "default",
  logoKeys,
}: CollaborateSectionProps = {}) => {
  const { firstRow, secondRow } = getMarqueeLogoRows(logoKeys);

  return (
    <Container bgColor="primary" id={anchor}>
      <Section gridArea="full" className="m-none overflow-hidden">
        <TitleCta
          title={title}
          ctaLabel={ctaLabel}
          ctaHref={ctaHref}
          titleSize={titleSize}
        />
        {firstRow.length > 0 && (
          <div className="mb-sm lg:mb-md">
            <Marquee logos={firstRow} />
          </div>
        )}
        {secondRow.length > 0 && (
          <div className="mb-sm lg:mb-lg">
            <Marquee direction="right" logos={secondRow} />
          </div>
        )}
      </Section>
    </Container>
  );
};

export default CollaborateSection;
