import React from 'react';
import PageContainer from '@/components/layout/page-container';
import { Icons } from '@/components/icons';
import { Badge } from '@/components/ui/badge';
import { isOrganizationAdminRole } from '@/lib/auth/roles';
import { requireEmployeeDashboardAccess } from '@/lib/auth/page-guards';
import {
  Card,
  CardAction,
  CardDescription,
  CardFooter,
  CardHeader,
  CardTitle
} from '@/components/ui/card';
import { getOverviewDashboardData } from '@/features/overview/server/overview-data';

function getDirectionIcon(direction: 'up' | 'down' | 'neutral') {
  if (direction === 'up') {
    return <Icons.trendingUp />;
  }

  if (direction === 'down') {
    return <Icons.trendingDown />;
  }

  return <Icons.adjustments />;
}

export default async function OverViewLayout({
  children,
  sales,
  pie_stats,
  bar_stats,
  area_stats
}: {
  children: React.ReactNode;
  sales: React.ReactNode;
  pie_stats: React.ReactNode;
  bar_stats: React.ReactNode;
  area_stats: React.ReactNode;
}) {
  await requireEmployeeDashboardAccess('/dashboard');

  const { role, cards } = await getOverviewDashboardData();

  return (
    <PageContainer
      pageTitle='ภาพรวมการอบรม'
      pageDescription={
        isOrganizationAdminRole(role)
          ? 'สรุปผลการอบรม การอนุมัติ และความครบถ้วนของทั้งองค์กร'
          : 'สรุปรายการอบรมล่าสุด การอนุมัติ และความคืบหน้าของใบรับรองของคุณ'
      }
    >
      <div className='flex flex-1 flex-col space-y-4'>
        {children}
        <div className='*:data-[slot=card]:from-primary/5 *:data-[slot=card]:to-card dark:*:data-[slot=card]:bg-card grid grid-cols-1 gap-4 *:data-[slot=card]:bg-gradient-to-t *:data-[slot=card]:shadow-xs md:grid-cols-2 lg:grid-cols-4'>
          {cards.map((card) => (
            <Card key={card.title} className='@container/card'>
              <CardHeader>
                <CardDescription>{card.title}</CardDescription>
                <CardTitle className='text-2xl font-semibold tabular-nums @[250px]/card:text-3xl'>
                  {card.value}
                </CardTitle>
                <CardAction>
                  <Badge variant='outline'>
                    {getDirectionIcon(card.direction)}
                    {card.badge}
                  </Badge>
                </CardAction>
              </CardHeader>
              <CardFooter className='flex-col items-start gap-1.5 text-sm'>
                <div className='line-clamp-2 flex gap-2 font-medium'>{card.description}</div>
                <div className='text-muted-foreground'>{card.footer}</div>
              </CardFooter>
            </Card>
          ))}
        </div>
        {isOrganizationAdminRole(role) ? (
          <div className='grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-7'>
            <div className='col-span-4'>{bar_stats}</div>
            <div className='col-span-4 md:col-span-3'>{sales}</div>
            <div className='col-span-4'>{area_stats}</div>
            <div className='col-span-4 min-h-0 md:col-span-3'>{pie_stats}</div>
          </div>
        ) : (
          <div className='grid grid-cols-1 gap-4 lg:grid-cols-7'>
            <div className='col-span-4'>{area_stats}</div>
            <div className='col-span-4 min-h-0 lg:col-span-3'>{sales}</div>
          </div>
        )}
      </div>
    </PageContainer>
  );
}
