import Link from 'next/link';

import { createI18nServerInstance } from '@/lib/i18n/i18n.server';
import { pathsConfig } from '@/packages/shared/src/config';
import { formatCurrency } from '@/packages/shared/src/utils';
import { SIDEBAR_WIDTH_PROPERTY } from '@/packages/ui/src/shadcn/constants';
import { StoreCart } from '@medusajs/types';
import { ShoppingCart } from 'lucide-react';

import { AppLogo } from '@kit/shared/components/app-logo';
import { ProfileAccountDropdownContainer } from '@kit/shared/components/personal-account-dropdown-container';
import { Search } from '@kit/shared/components/ui/search';
import { Button } from '@kit/ui/button';
import { Card } from '@kit/ui/shadcn/card';
import { Trans } from '@kit/ui/trans';

import { UserNotifications } from '../_components/user-notifications';
import { type UserWorkspace } from '../_lib/server/load-user-workspace';

export async function HomeMenuNavigation(props: {
  workspace: UserWorkspace;
  cart: StoreCart | null;
}) {
  const { language } = await createI18nServerInstance();
  const { workspace, user, accounts } = props.workspace;
  const totalValue = props.cart?.total
    ? formatCurrency({
        currencyCode: props.cart.currency_code,
        locale: language,
        value: props.cart.total,
      })
    : 0;

  const cartItemsCount = props.cart?.items?.length ?? 0;
  const hasCartItems = cartItemsCount > 0;

  return (
    <div className={'flex w-full flex-1 items-center justify-between gap-3'}>
      <div className={`flex items-center ${SIDEBAR_WIDTH_PROPERTY}`}>
        <AppLogo href={pathsConfig.app.home} />
      </div>
      <Search
        className="flex grow"
        startElement={<Trans i18nKey="common:search" values={{ end: '...' }} />}
      />

      <div className="flex items-center justify-end gap-3">
        <Card className="px-6 py-2">
          <span>€ {Number(0).toFixed(2).replace('.', ',')}</span>
        </Card>
        {hasCartItems && (
          <Button
            className="relative mr-0 h-10 cursor-pointer border-1 px-4 py-2"
            variant="ghost"
          >
            <span className="flex items-center text-nowrap">{totalValue}</span>
          </Button>
        )}
        <Link href="/home/cart">
          <Button
            variant="ghost"
            className="relative mr-0 h-10 cursor-pointer border-1 px-4 py-2"
          >
            <ShoppingCart className="stroke-[1.5px]" />
            <Trans
              i18nKey="common:shoppingCartCount"
              values={{ count: cartItemsCount }}
            />
          </Button>
        </Link>
        <UserNotifications userId={user.id} />

        <div>
          <ProfileAccountDropdownContainer
            user={user}
            account={workspace}
            showProfileName
            accounts={accounts}
          />
        </div>
      </div>
    </div>
  );
}
