import { cookies, headers } from "next/headers";

import VoiceAccentTrainerLanding from "@/components/self-paced/VoiceAccentTrainerLanding";
import { getAuthenticatedUserFromToken } from "@/lib/adminAccess";
import { getCheckoutPageBySlug, getCheckoutPlans } from "@/lib/checkoutData";
import pool from "@/lib/db";
import { getProgrammeCourseAccessForUser } from "@/lib/programmeCheckout";
import { getContentMetadataForRequest, getRequestMetadataInput } from "@/lib/siteSeo";

const PATHNAME = "/self-paced-programmes/voice-and-accent-trainer-programme";
const FALLBACK_TITLE = "Voice & Accent Foundation Programme | Cambridge Veritas Teacher Training";
const FALLBACK_DESCRIPTION =
  "Clear your Voice & Accent fundamentals with 16 self-paced recorded classes. Build the content knowledge and pronunciation drills you need to start teaching Voice & Accent — then level up to certified-expert status with our in-person, trainer-led programme.";
const FALLBACK_KEYWORDS = [
  "voice and accent foundation programme",
  "voice and accent fundamentals",
  "pronunciation training for teachers",
  "phonetics for trainers",
  "accent neutralisation basics",
  "start teaching voice and accent",
  "recorded voice and accent course",
  "in-person voice and accent certification",
  "voice and accent training success stories",
  "teacher video testimonials",
  "voice and accent course reviews",
];

export async function generateMetadata() {
  return getContentMetadataForRequest({
    ...getRequestMetadataInput(await headers(), { pathname: PATHNAME }),
    pathname: PATHNAME,
    title: FALLBACK_TITLE,
    description: FALLBACK_DESCRIPTION,
    keywords: FALLBACK_KEYWORDS,
  });
}

export default async function VoiceAccentTrainerProgrammePage() {
  const cookieStore = await cookies();
  const user = await getAuthenticatedUserFromToken(cookieStore.get('token')?.value || '')
    .catch(() => null);
  const initialProgrammeAccess = await getProgrammeCourseAccessForUser(
    user,
    'cvat-foundation',
    pool,
  ).catch(() => ({
    hasAccess: false,
    dashboardUrl: '/dashboard/course/voice-and-accent-trainer',
  }));
  const checkoutPage = await getCheckoutPageBySlug('cvat-foundation');
  const checkoutPlans = checkoutPage ? await getCheckoutPlans(checkoutPage.id) : [];
  const programmePrice = Number(checkoutPlans[0]?.plan_amount || 4999);

  return (
    <VoiceAccentTrainerLanding
      initialProgrammeAccess={initialProgrammeAccess}
      programmePricing={{
        planId: checkoutPlans[0]?.id || 'programme:cvat-foundation:annual',
        price: programmePrice,
        originalPrice: Number(checkoutPlans[0]?.original_amount || 9999),
      }}
    />
  );
}
