import { Section } from '@/components/Section/Section'
import { fetchPopePrayerIntentions } from '@/fetch/popePrayerIntentions'
import { Container } from '@/components/Container/Container'
import { Title } from '@/components/Title/Title'
import { P } from '@/components/Text/Paragraph'
import { PageHeader } from '@/compositions/PageHeader/PageHeader'
import { NextPrevButtons } from '@/components/NextPrevButtons/NextPrevButtons'
import { notFound } from 'next/navigation'

const months: Record<string, string> = {
  "01": "Januar",
  "02": "Februar",
  "03": "März",
  "04": "April",
  "05": "Mai",
  "06": "Juni",
  "07": "Juli",
  "08": "August",
  "09": "September",
  "10": "Oktober",
  "11": "November",
  "12": "Dezember",
}

const getMonthAsString = (month: string): string => {
  if (months.hasOwnProperty(month)) {
    return months[month];
  }

  return "Unknown";
}


/**
 * Get next month
 * eg: "12" => "01"
 *
 * @param month
 */
function nextMonth(month: string) {
  let monthNumber = parseInt(month, 10);
  monthNumber++;

  if (monthNumber > 12) {
    monthNumber = 1;
  }

  return monthNumber.toString().padStart(2, '0');
}

/**
 * Get previous month
 * eg: "03" => "02"
 *
 * @param month
 */
function prevMonth(month: string) {
  let monthNumber = parseInt(month, 10);
  monthNumber--;

  if (monthNumber == 0) {
    monthNumber = 12;
  }

  return monthNumber.toString().padStart(2, '0');
}

/**
 * Get next url
 *
 * @param year
 * @param month
 */
function nextUrl(year: number, month: string) {
  const next = nextMonth(month);
  if (next == "01") {
    year++;
  }

  return `/gebetsanliegen-des-papstes/${year}/${next}`;
}

/**
 * Get previous url
 *
 * @param year
 * @param month
 */
function prevUrl(year: number, month: string) {
  const prev = prevMonth(month);
  if (prev == "12") {
    year--;
  }

  return `/gebetsanliegen-des-papstes/${year}/${prev}`;
}

export default async function PrayerIntentionPage({ params }: { params: Promise<{year: string, month: string}>}) {
  const {year, month} = await params;
  const yearInt = parseInt(year, 10);
  let prayer = await fetchPopePrayerIntentions(yearInt, month)
  const monthString = getMonthAsString(month)

  if(!prayer) {
    notFound();
  }

  return (
    <>
      <PageHeader
        title={"Gebetsanliegen des Papstes"}
        description={"Der Papst lädt uns ein, uns mit ihm im Gebet zu verbinden und gemeinsam die Welt vor Gott zu tragen."}
      />
      <Section padding={"small"}>
        <Container>
          <Title
            color={"contrast"}
            title={`${monthString} ${year}: ${prayer.title}`}
            size={"sm"}
          />

          <P width={"3/4"}>
            { prayer.prayer}
          </P>
        </Container>
      </Section>
      <Section>
      </Section>
      <Section padding={"small"}>
        <NextPrevButtons
          prev={{
            href: prevUrl(yearInt, month),
            text: getMonthAsString(prevMonth(month))
          }}
          next={{
            href: nextUrl(yearInt, month),
            text: getMonthAsString(nextMonth(month))
          }}
        />
      </Section>
    </>

  )
}