import React, { ReactElement, useEffect } from 'react';
import Head from 'next/head';
import DatePicker from 'react-datepicker';
import 'react-datepicker/dist/react-datepicker.css';
import dayjs from 'dayjs';
import { useAppDispatch, useAppSelector } from '../../stores/hooks';
import { useRouter } from 'next/router';
import { fetch } from '../../stores/access_logs/access_logsSlice';
import LayoutAuthenticated from '../../layouts/Authenticated';
import { getPageTitle } from '../../config';
import SectionTitleLineWithButton from '../../components/SectionTitleLineWithButton';
import SectionMain from '../../components/SectionMain';
import CardBox from '../../components/CardBox';
import BaseButton from '../../components/BaseButton';
import BaseDivider from '../../components/BaseDivider';
import { mdiChartTimelineVariant } from '@mdi/js';
import FormField from '../../components/FormField';
import type { AccessLog } from '../../types/entities';

const Access_logsView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const accessLogsState = useAppSelector((state) => state.access_logs);
  // When fetching single item, it's stored as the entity object (not array)
  const access_logs = accessLogsState.access_logs as
    | AccessLog
    | AccessLog[]
    | undefined;
  const accessLog = Array.isArray(access_logs) ? access_logs[0] : access_logs;

  const { id } = router.query;
  const idStr = Array.isArray(id) ? id[0] : id;

  function removeLastCharacter(str: string) {
    return str.slice(0, -1);
  }

  useEffect(() => {
    if (idStr) {
      dispatch(fetch({ id: idStr }));
    }
  }, [dispatch, idStr]);

  return (
    <>
      <Head>
        <title>{getPageTitle('View access_logs')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title={removeLastCharacter('View access_logs')}
          main
        >
          <BaseButton
            color='info'
            label='Edit'
            href={`/access_logs/access_logs-edit/?id=${id}`}
          />
        </SectionTitleLineWithButton>
        <CardBox>
          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Project</p>

            <p>
              {(accessLog?.project as { name?: string } | undefined)?.name ??
                'No data'}
            </p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Environment</p>
            <p>{accessLog?.environment ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>User</p>

            <p>
              {(accessLog?.user as { firstName?: string } | undefined)
                ?.firstName ?? 'No data'}
            </p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Path</p>
            <p>{accessLog?.path}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>IPaddress</p>
            <p>{accessLog?.ip_address}</p>
          </div>

          <FormField label='Multi Text' hasTextareaHeight>
            <textarea
              className={'w-full'}
              disabled
              value={accessLog?.user_agent ?? ''}
            />
          </FormField>

          <FormField label='Accessedat'>
            {accessLog?.accessed_at ? (
              <DatePicker
                dateFormat='yyyy-MM-dd hh:mm'
                showTimeSelect
                selected={
                  accessLog.accessed_at
                    ? new Date(
                        dayjs(accessLog.accessed_at).format('YYYY-MM-DD hh:mm'),
                      )
                    : null
                }
                disabled
              />
            ) : (
              <p>No Accessedat</p>
            )}
          </FormField>

          <BaseDivider />

          <BaseButton
            color='info'
            label='Back'
            onClick={() => router.push('/access_logs/access_logs-list')}
          />
        </CardBox>
      </SectionMain>
    </>
  );
};

Access_logsView.getLayout = function getLayout(page: ReactElement) {
  return (
    <LayoutAuthenticated permission={'READ_ACCESS_LOGS'}>
      {page}
    </LayoutAuthenticated>
  );
};

export default Access_logsView;
