import { notFound, redirect } from 'next/navigation';

import Api from '@/api';
import { checkUrlByLanguage } from '@/utils/checkUrlByLanguage';
import { formatFloor } from '@/utils/formatFloor';
import { Locale } from '@/i18n.config';
import { RoomType } from '@/types';

export default async function CodeResultPage({
  params: { code = '', lang },
}: {
  params: { code?: string; lang: Locale };
}) {
  if (!!code?.length && ['001', '002', '003'].includes(code)) {
    return redirect(checkUrlByLanguage(lang, `/`));
  }

  const rooms: RoomType[] = await Api.rooms.getAll(lang) || [];

  const current = rooms.find((r: RoomType) => r?.code === code);

  if (!!current?.slug?.length) {
    return redirect(
      checkUrlByLanguage(
        lang,
        `/${formatFloor(current?.floor || 0)}/${current.slug}`,
      ),
    );
  }

  return notFound();
};
