import * as icon from '@mdi/js';
import Head from 'next/head';
import React from 'react';
import axios from 'axios';
import type { ReactElement } from 'react';
import LayoutAuthenticated from '../layouts/Authenticated';
import SectionMain from '../components/SectionMain';
import SectionTitleLineWithButton from '../components/SectionTitleLineWithButton';
import BaseIcon from '../components/BaseIcon';
import { getPageTitle } from '../config';
import Link from 'next/link';
import { useTranslation } from 'next-i18next';

import { hasPermission } from '../helpers/userPermissions';
import { fetchWidgets } from '../stores/roles/rolesSlice';
import { WidgetCreator } from '../components/WidgetCreator/WidgetCreator';
import { SmartWidget } from '../components/SmartWidget/SmartWidget';

import { useAppDispatch, useAppSelector } from '../stores/hooks';
const Dashboard = () => {
  const { t } = useTranslation('common');
  const dispatch = useAppDispatch();
  const iconsColor = useAppSelector((state) => state.style.iconsColor);
  const corners = useAppSelector((state) => state.style.corners);
  const cardsStyle = useAppSelector((state) => state.style.cardsStyle);

  const loadingMessage = t('pages.dashboard.loading', {
    defaultValue: 'Loading...',
  });

  const [users, setUsers] = React.useState(loadingMessage);
  const [cameras_ptz, setCameras_ptz] = React.useState(loadingMessage);
  const [epcs, setEpcs] = React.useState(loadingMessage);
  const [epis, setEpis] = React.useState(loadingMessage);
  const [equipes_trabalho, setEquipes_trabalho] =
    React.useState(loadingMessage);
  const [estrutura_projeto, setEstrutura_projeto] =
    React.useState(loadingMessage);
  const [etapas_ou_trechos, setEtapas_ou_trechos] =
    React.useState(loadingMessage);
  const [frames_imagem, setFrames_imagem] = React.useState(loadingMessage);
  const [gemeos_digitais, setGemeos_digitais] = React.useState(loadingMessage);
  const [historico_alteracoes_projeto, setHistorico_alteracoes_projeto] =
    React.useState(loadingMessage);
  const [inspecao_aerea, setInspecao_aerea] = React.useState(loadingMessage);
  const [maquinas_atividade, setMaquinas_atividade] =
    React.useState(loadingMessage);
  const [materiais_atividade, setMateriais_atividade] =
    React.useState(loadingMessage);
  const [membros_equipe, setMembros_equipe] = React.useState(loadingMessage);
  const [memoriais_descritivos_asbuilt, setMemoriais_descritivos_asbuilt] =
    React.useState(loadingMessage);
  const [modelos_bim_asbuilt, setModelos_bim_asbuilt] =
    React.useState(loadingMessage);
  const [obras, setObras] = React.useState(loadingMessage);
  const [permissoes_modulo, setPermissoes_modulo] =
    React.useState(loadingMessage);
  const [pessoas_atividade, setPessoas_atividade] =
    React.useState(loadingMessage);
  const [pessoas_frame, setPessoas_frame] = React.useState(loadingMessage);
  const [plantas_asbuilt_2d, setPlantas_asbuilt_2d] =
    React.useState(loadingMessage);
  const [relatorio_andamento_atual, setRelatorio_andamento_atual] =
    React.useState(loadingMessage);
  const [relatorios_analiticos_video, setRelatorios_analiticos_video] =
    React.useState(loadingMessage);
  const [relatorios_diarios, setRelatorios_diarios] =
    React.useState(loadingMessage);
  const [relatorios_mensais, setRelatorios_mensais] =
    React.useState(loadingMessage);
  const [relatorios_semanais, setRelatorios_semanais] =
    React.useState(loadingMessage);
  const [subcontratadas, setSubcontratadas] = React.useState(loadingMessage);
  const [usuarios, setUsuarios] = React.useState(loadingMessage);
  const [roles, setRoles] = React.useState(loadingMessage);
  const [permissions, setPermissions] = React.useState(loadingMessage);
  const [clientes, setClientes] = React.useState(loadingMessage);

  const [widgetsRole, setWidgetsRole] = React.useState({
    role: { value: '', label: '' },
  });
  const { currentUser } = useAppSelector((state) => state.auth);
  const { isFetchingQuery } = useAppSelector((state) => state.openAi);

  const { rolesWidgets, loading } = useAppSelector((state) => state.roles);

  const organizationId = currentUser?.clientes?.id;

  async function loadData() {
    const entities = [
      'users',
      'cameras_ptz',
      'epcs',
      'epis',
      'equipes_trabalho',
      'estrutura_projeto',
      'etapas_ou_trechos',
      'frames_imagem',
      'gemeos_digitais',
      'historico_alteracoes_projeto',
      'inspecao_aerea',
      'maquinas_atividade',
      'materiais_atividade',
      'membros_equipe',
      'memoriais_descritivos_asbuilt',
      'modelos_bim_asbuilt',
      'obras',
      'permissoes_modulo',
      'pessoas_atividade',
      'pessoas_frame',
      'plantas_asbuilt_2d',
      'relatorio_andamento_atual',
      'relatorios_analiticos_video',
      'relatorios_diarios',
      'relatorios_mensais',
      'relatorios_semanais',
      'subcontratadas',
      'usuarios',
      'roles',
      'permissions',
      'clientes',
    ];
    const fns = [
      setUsers,
      setCameras_ptz,
      setEpcs,
      setEpis,
      setEquipes_trabalho,
      setEstrutura_projeto,
      setEtapas_ou_trechos,
      setFrames_imagem,
      setGemeos_digitais,
      setHistorico_alteracoes_projeto,
      setInspecao_aerea,
      setMaquinas_atividade,
      setMateriais_atividade,
      setMembros_equipe,
      setMemoriais_descritivos_asbuilt,
      setModelos_bim_asbuilt,
      setObras,
      setPermissoes_modulo,
      setPessoas_atividade,
      setPessoas_frame,
      setPlantas_asbuilt_2d,
      setRelatorio_andamento_atual,
      setRelatorios_analiticos_video,
      setRelatorios_diarios,
      setRelatorios_mensais,
      setRelatorios_semanais,
      setSubcontratadas,
      setUsuarios,
      setRoles,
      setPermissions,
      setClientes,
    ];

    const requests = entities.map((entity, index) => {
      if (hasPermission(currentUser, `READ_${entity.toUpperCase()}`)) {
        return axios.get(`/${entity.toLowerCase()}/count`);
      } else {
        fns[index](null);
        return Promise.resolve({ data: { count: null } });
      }
    });

    Promise.allSettled(requests).then((results) => {
      results.forEach((result, i) => {
        if (result.status === 'fulfilled') {
          fns[i](result.value.data.count);
        } else {
          fns[i](result.reason.message);
        }
      });
    });
  }

  async function getWidgets(roleId) {
    await dispatch(fetchWidgets(roleId));
  }
  React.useEffect(() => {
    if (!currentUser) return;
    loadData().then();
    setWidgetsRole({
      role: {
        value: currentUser?.app_role?.id,
        label: currentUser?.app_role?.name,
      },
    });
  }, [currentUser]);

  React.useEffect(() => {
    if (!currentUser || !widgetsRole?.role?.value) return;
    getWidgets(widgetsRole?.role?.value || '').then();
  }, [widgetsRole?.role?.value]);

  return (
    <>
      <Head>
        <title>
          {getPageTitle(
            t('pages.dashboard.pageTitle', { defaultValue: 'Overview' }),
          )}
        </title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={icon.mdiChartTimelineVariant}
          title={t('pages.dashboard.overview', { defaultValue: 'Overview' })}
          main
        >
          {''}
        </SectionTitleLineWithButton>

        {hasPermission(currentUser, 'CREATE_ROLES') && (
          <WidgetCreator
            currentUser={currentUser}
            isFetchingQuery={isFetchingQuery}
            setWidgetsRole={setWidgetsRole}
            widgetsRole={widgetsRole}
          />
        )}
        {!!rolesWidgets.length &&
          hasPermission(currentUser, 'CREATE_ROLES') && (
            <p className='  text-gray-500 dark:text-gray-400 mb-4'>
              {`${widgetsRole?.role?.label || 'Users'}'s widgets`}
            </p>
          )}

        <div className='grid grid-cols-1 gap-6 lg:grid-cols-4 mb-6 grid-flow-dense'>
          {(isFetchingQuery || loading) && (
            <div
              className={` ${
                corners !== 'rounded-full' ? corners : 'rounded-3xl'
              } dark:bg-dark-900 text-lg leading-tight   text-gray-500 flex items-center ${cardsStyle} dark:border-dark-700 p-6`}
            >
              <BaseIcon
                className={`${iconsColor} animate-spin mr-5`}
                w='w-16'
                h='h-16'
                size={48}
                path={icon.mdiLoading}
              />{' '}
              {t('pages.dashboard.loadingWidgets', {
                defaultValue: 'Loading widgets...',
              })}
            </div>
          )}

          {rolesWidgets &&
            rolesWidgets.map((widget) => (
              <SmartWidget
                key={widget.id}
                userId={currentUser?.id}
                widget={widget}
                roleId={widgetsRole?.role?.value || ''}
                admin={hasPermission(currentUser, 'CREATE_ROLES')}
              />
            ))}
        </div>

        {!!rolesWidgets.length && <hr className='my-6  ' />}

        <div
          id='dashboard'
          className='grid grid-cols-1 gap-6 lg:grid-cols-3 mb-6'
        >
          {hasPermission(currentUser, 'READ_USERS') && (
            <Link href={'/users/users-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Users
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {users}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={icon.mdiAccountGroup || icon.mdiTable}
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_CAMERAS_PTZ') && (
            <Link href={'/cameras_ptz/cameras_ptz-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Cameras ptz
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {cameras_ptz}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiCameraOutline' in icon
                          ? icon['mdiCameraOutline' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_EPCS') && (
            <Link href={'/epcs/epcs-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Epcs
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {epcs}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiShieldCheck' in icon
                          ? icon['mdiShieldCheck' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_EPIS') && (
            <Link href={'/epis/epis-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Epis
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {epis}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiShield' in icon
                          ? icon['mdiShield' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_EQUIPES_TRABALHO') && (
            <Link href={'/equipes_trabalho/equipes_trabalho-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Equipes trabalho
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {equipes_trabalho}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiAccountGroup' in icon
                          ? icon['mdiAccountGroup' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_ESTRUTURA_PROJETO') && (
            <Link href={'/estrutura_projeto/estrutura_projeto-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Estrutura projeto
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {estrutura_projeto}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiProjectorScreen' in icon
                          ? icon['mdiProjectorScreen' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_ETAPAS_OU_TRECHOS') && (
            <Link href={'/etapas_ou_trechos/etapas_ou_trechos-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Etapas ou trechos
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {etapas_ou_trechos}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiTimeline' in icon
                          ? icon['mdiTimeline' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_FRAMES_IMAGEM') && (
            <Link href={'/frames_imagem/frames_imagem-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Frames imagem
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {frames_imagem}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiImage' in icon
                          ? icon['mdiImage' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_GEMEOS_DIGITAIS') && (
            <Link href={'/gemeos_digitais/gemeos_digitais-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Gemeos digitais
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {gemeos_digitais}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiCubeOutline' in icon
                          ? icon['mdiCubeOutline' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_HISTORICO_ALTERACOES_PROJETO') && (
            <Link
              href={
                '/historico_alteracoes_projeto/historico_alteracoes_projeto-list'
              }
            >
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Historico alteracoes projeto
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {historico_alteracoes_projeto}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiHistory' in icon
                          ? icon['mdiHistory' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_INSPECAO_AEREA') && (
            <Link href={'/inspecao_aerea/inspecao_aerea-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Inspecao aerea
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {inspecao_aerea}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiDrone' in icon
                          ? icon['mdiDrone' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_MAQUINAS_ATIVIDADE') && (
            <Link href={'/maquinas_atividade/maquinas_atividade-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Maquinas atividade
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {maquinas_atividade}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiTractor' in icon
                          ? icon['mdiTractor' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_MATERIAIS_ATIVIDADE') && (
            <Link href={'/materiais_atividade/materiais_atividade-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Materiais atividade
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {materiais_atividade}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiPackageVariant' in icon
                          ? icon['mdiPackageVariant' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_MEMBROS_EQUIPE') && (
            <Link href={'/membros_equipe/membros_equipe-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Membros equipe
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {membros_equipe}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiAccount' in icon
                          ? icon['mdiAccount' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_MEMORIAIS_DESCRITIVOS_ASBUILT') && (
            <Link
              href={
                '/memoriais_descritivos_asbuilt/memoriais_descritivos_asbuilt-list'
              }
            >
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Memoriais descritivos asbuilt
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {memoriais_descritivos_asbuilt}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiFileDocumentOutline' in icon
                          ? icon['mdiFileDocumentOutline' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_MODELOS_BIM_ASBUILT') && (
            <Link href={'/modelos_bim_asbuilt/modelos_bim_asbuilt-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Modelos bim asbuilt
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {modelos_bim_asbuilt}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiCubeScan' in icon
                          ? icon['mdiCubeScan' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_OBRAS') && (
            <Link href={'/obras/obras-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Obras
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {obras}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiCity' in icon
                          ? icon['mdiCity' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_PERMISSOES_MODULO') && (
            <Link href={'/permissoes_modulo/permissoes_modulo-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Permissoes modulo
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {permissoes_modulo}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiLock' in icon
                          ? icon['mdiLock' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_PESSOAS_ATIVIDADE') && (
            <Link href={'/pessoas_atividade/pessoas_atividade-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Pessoas atividade
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {pessoas_atividade}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiAccountTie' in icon
                          ? icon['mdiAccountTie' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_PESSOAS_FRAME') && (
            <Link href={'/pessoas_frame/pessoas_frame-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Pessoas frame
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {pessoas_frame}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiAccountHardHat' in icon
                          ? icon['mdiAccountHardHat' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_PLANTAS_ASBUILT_2D') && (
            <Link href={'/plantas_asbuilt_2d/plantas_asbuilt_2d-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Plantas asbuilt 2d
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {plantas_asbuilt_2d}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiMapOutline' in icon
                          ? icon['mdiMapOutline' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_RELATORIO_ANDAMENTO_ATUAL') && (
            <Link
              href={'/relatorio_andamento_atual/relatorio_andamento_atual-list'}
            >
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Relatorio andamento atual
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {relatorio_andamento_atual}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiProgressCheck' in icon
                          ? icon['mdiProgressCheck' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_RELATORIOS_ANALITICOS_VIDEO') && (
            <Link
              href={
                '/relatorios_analiticos_video/relatorios_analiticos_video-list'
              }
            >
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Relatorios analiticos video
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {relatorios_analiticos_video}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiFileDocument' in icon
                          ? icon['mdiFileDocument' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_RELATORIOS_DIARIOS') && (
            <Link href={'/relatorios_diarios/relatorios_diarios-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Relatorios diarios
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {relatorios_diarios}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiCalendarDay' in icon
                          ? icon['mdiCalendarDay' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_RELATORIOS_MENSAIS') && (
            <Link href={'/relatorios_mensais/relatorios_mensais-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Relatorios mensais
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {relatorios_mensais}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiCalendarMonth' in icon
                          ? icon['mdiCalendarMonth' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_RELATORIOS_SEMANAIS') && (
            <Link href={'/relatorios_semanais/relatorios_semanais-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Relatorios semanais
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {relatorios_semanais}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiCalendarWeek' in icon
                          ? icon['mdiCalendarWeek' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_SUBCONTRATADAS') && (
            <Link href={'/subcontratadas/subcontratadas-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Subcontratadas
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {subcontratadas}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiFactory' in icon
                          ? icon['mdiFactory' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_USUARIOS') && (
            <Link href={'/usuarios/usuarios-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Usuarios
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {usuarios}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        'mdiAccountCircle' in icon
                          ? icon['mdiAccountCircle' as keyof typeof icon]
                          : icon.mdiTable || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_ROLES') && (
            <Link href={'/roles/roles-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Roles
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {roles}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={
                        icon.mdiShieldAccountVariantOutline || icon.mdiTable
                      }
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_PERMISSIONS') && (
            <Link href={'/permissions/permissions-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Permissions
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {permissions}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={icon.mdiShieldAccountOutline || icon.mdiTable}
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}

          {hasPermission(currentUser, 'READ_CLIENTES') && (
            <Link href={'/clientes/clientes-list'}>
              <div
                className={`${
                  corners !== 'rounded-full' ? corners : 'rounded-3xl'
                } dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
              >
                <div className='flex justify-between align-center'>
                  <div>
                    <div className='text-lg leading-tight   text-gray-500 dark:text-gray-400'>
                      Clientes
                    </div>
                    <div className='text-3xl leading-tight font-semibold'>
                      {clientes}
                    </div>
                  </div>
                  <div>
                    <BaseIcon
                      className={`${iconsColor}`}
                      w='w-16'
                      h='h-16'
                      size={48}
                      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                      // @ts-ignore
                      path={icon.mdiTable || icon.mdiTable}
                    />
                  </div>
                </div>
              </div>
            </Link>
          )}
        </div>
      </SectionMain>
    </>
  );
};

Dashboard.getLayout = function getLayout(page: ReactElement) {
  return <LayoutAuthenticated>{page}</LayoutAuthenticated>;
};

export default Dashboard;
