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/chats/chatsSlice'
import {saveFile} from "../../helpers/fileSaver";
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from "../../components/ImageField";
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 {SwitchField} from "../../components/SwitchField";
import FormField from "../../components/FormField";

import {hasPermission} from "../../helpers/userPermissions";


const ChatsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { chats } = useAppSelector((state) => state.chats)
    
    const { currentUser } = useAppSelector((state) => state.auth);
    

    const { id } = router.query;
    
    function removeLastCharacter(str) {
      console.log(str,`str`)
      return str.slice(0, -1);
    }

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


    return (
      <>
          <Head>
              <title>{getPageTitle('View chats')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View chats')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/chats/chats-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Workspace</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{chats?.workspace?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>User</p>
                    
                    
                        <p>{chats?.user?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ChatTitle</p>
                    <p>{chats?.title}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ChatStatus</p>
                    <p>{chats?.chat_status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Model</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{chats?.model?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsEncryptedE2EE'>
                    <SwitchField
                      field={{name: 'is_encrypted_e2ee', value: chats?.is_encrypted_e2ee}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>Messages</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                    <th>SenderType</th>
                                
                                
                                
                                    <th>SenderLabel</th>
                                
                                
                                
                                    <th>ContentCiphertext</th>
                                
                                
                                
                                    <th>ContentPreview</th>
                                
                                
                                
                                    <th>EncryptionKeyId</th>
                                
                                
                                
                                    <th>SentAt</th>
                                
                                
                                
                                    <th>TokenCountInput</th>
                                
                                
                                
                                    <th>TokenCountOutput</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {chats.messages && Array.isArray(chats.messages) &&
                              chats.messages.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/chat_messages/chat_messages-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                  
                                    
                                    
                                    
                                    <td data-label="sender_type">
                                        { item.sender_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sender_label">
                                        { item.sender_label }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="content_ciphertext">
                                        { item.content_ciphertext }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="content_preview">
                                        { item.content_preview }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="encryption_key_id">
                                        { item.encryption_key_id }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sent_at">
                                        { dataFormatter.dateTimeFormatter(item.sent_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="token_count_input">
                                        { item.token_count_input }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="token_count_output">
                                        { item.token_count_output }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!chats?.messages?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>organizations</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{chats?.organizations?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Chat_messages Chat</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>SenderType</th>
                                
                                
                                
                                <th>SenderLabel</th>
                                
                                
                                
                                <th>ContentCiphertext</th>
                                
                                
                                
                                <th>ContentPreview</th>
                                
                                
                                
                                <th>EncryptionKeyId</th>
                                
                                
                                
                                <th>SentAt</th>
                                
                                
                                
                                <th>TokenCountInput</th>
                                
                                
                                
                                <th>TokenCountOutput</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {chats.chat_messages_chat && Array.isArray(chats.chat_messages_chat) &&
                              chats.chat_messages_chat.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/chat_messages/chat_messages-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="sender_type">
                                        { item.sender_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sender_label">
                                        { item.sender_label }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="content_ciphertext">
                                        { item.content_ciphertext }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="content_preview">
                                        { item.content_preview }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="encryption_key_id">
                                        { item.encryption_key_id }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sent_at">
                                        { dataFormatter.dateTimeFormatter(item.sent_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="token_count_input">
                                        { item.token_count_input }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="token_count_output">
                                        { item.token_count_output }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!chats?.chat_messages_chat?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Encryption_sessions Chat</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>SessionKeyId</th>
                                
                                
                                
                                <th>Algorithm</th>
                                
                                
                                
                                <th>KeyLengthBits</th>
                                
                                
                                
                                <th>ValidFrom</th>
                                
                                
                                
                                <th>ValidUntil</th>
                                
                                
                                
                                <th>IsDiscarded</th>
                                
                                
                                
                                <th>DiscardedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {chats.encryption_sessions_chat && Array.isArray(chats.encryption_sessions_chat) &&
                              chats.encryption_sessions_chat.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/encryption_sessions/encryption_sessions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="session_key_id">
                                        { item.session_key_id }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="algorithm">
                                        { item.algorithm }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="key_length_bits">
                                        { item.key_length_bits }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="valid_from">
                                        { dataFormatter.dateTimeFormatter(item.valid_from) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="valid_until">
                                        { dataFormatter.dateTimeFormatter(item.valid_until) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_discarded">
                                        { dataFormatter.booleanFormatter(item.is_discarded) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="discarded_at">
                                        { dataFormatter.dateTimeFormatter(item.discarded_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!chats?.encryption_sessions_chat?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

ChatsView.getLayout = function getLayout(page: ReactElement) {
    return (
      <LayoutAuthenticated
        
        permission={'READ_CHATS'}
        
      >
          {page}
      </LayoutAuthenticated>
    )
}

export default ChatsView;