import type { Metadata } from 'next';
import ModPageClient from '@/components/pages/ModPageClient';
import { getMod, getUnitsForMod, getBuildingsForMod } from '@/lib/api-server';
import { IRaceBuildings } from '@/src/types/IBuildingShort';

export const dynamic = 'force-dynamic';

interface Props {
    params: { modId: string };
}

export async function generateMetadata({ params }: Props): Promise<Metadata> {
    const mod = await getMod(params.modId);
    if (!mod) {
        return { title: 'Mod' };
    }
    const title = `${mod.name} (${mod.version})`;
    const description = `Dawn of War wiki. ${mod.name} mod. Units, races, buildings and stats.`;
    return {
        title,
        description,
        alternates: {
            canonical: `/mod/${params.modId}`,
        },
        openGraph: {
            title,
            description,
        },
    };
}

export default async function ModPage({ params }: Props) {
    const [mod, racesUnits] = await Promise.all([
        getMod(params.modId),
        getUnitsForMod(params.modId),
    ]);
    const units = racesUnits ?? [];
    let racesBuildings: IRaceBuildings[] = [];
    if (units.length <= 10) {
        racesBuildings = (await getBuildingsForMod(params.modId)) ?? [];
    }
    return <ModPageClient initialMod={mod} racesUnits={units} racesBuildings={racesBuildings} />;
}