import type { Metadata } from 'next';
import UnitPageClient from '@/components/pages/UnitPageClient';
import { getMod, getUnit } from '@/lib/api-server';
import { IUnit } from '@/src/types/IUnit';
import { IMod } from '@/src/types/Imod';

export const dynamic = 'force-dynamic';

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

export async function generateMetadata({ params }: Props): Promise<Metadata> {
    const [mod, unit] = await Promise.all([getMod(params.modId), getUnit(params.unitId)]);
    if (!mod || !unit) {
        return { title: 'Unit' };
    }
    const title = `${unit.name} — ${mod.name}`;
    const description = unit.description
        ? `${unit.name} unit in ${mod.name} mod. ${unit.description}`
        : `${unit.name} unit in ${mod.name} mod. Stats, weapons, abilities.`;
    return {
        title,
        description,
        alternates: {
            canonical: `/mod/${params.modId}/race/${params.raceId}/unit/${params.unitId}`,
        },
        openGraph: {
            title,
            description,
        },
    };
}

export default async function UnitPage({ params }: Props) {
    const [mod, unit] = await Promise.all([
        getMod(params.modId),
        getUnit(params.unitId),
    ]);
    return (
        <UnitPageClient
            key={params.unitId}
            initialUnit={unit as IUnit | null}
            initialMod={mod as IMod | null}
        />
    );
}