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/chat_sessions/chat_sessionsSlice'
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 Chat_sessionsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { chat_sessions } = useAppSelector((state) => state.chat_sessions)
    
    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 chat_sessions')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View chat_sessions')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/chat_sessions/chat_sessions-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SessionType</p>
                    <p>{chat_sessions?.session_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Patient</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{chat_sessions?.patient?.patient_identifier ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Doctor</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{chat_sessions?.doctor?.specialization ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SecondDoctor</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{chat_sessions?.second_doctor?.specialization ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CreatedByUser</p>
                    
                    
                        <p>{chat_sessions?.created_by_user?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SessionStatus</p>
                    <p>{chat_sessions?.session_status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Messages ChatSession</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>SentAt</th>
                                
                                
                                
                                <th>IsRead</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {chat_sessions.messages_chat_session && Array.isArray(chat_sessions.messages_chat_session) &&
                              chat_sessions.messages_chat_session.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/messages/messages-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="sender_type">
                                        { item.sender_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="sent_at">
                                        { dataFormatter.dateTimeFormatter(item.sent_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_read">
                                        { dataFormatter.booleanFormatter(item.is_read) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!chat_sessions?.messages_chat_session?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Video_calls ChatSession</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Provider</th>
                                
                                
                                
                                <th>ProviderRoomName</th>
                                
                                
                                
                                <th>ProviderSessionReference</th>
                                
                                
                                
                                <th>StartedAt</th>
                                
                                
                                
                                <th>EndedAt</th>
                                
                                
                                
                                <th>CallStatus</th>
                                
                                
                                
                                <th>CallNotes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {chat_sessions.video_calls_chat_session && Array.isArray(chat_sessions.video_calls_chat_session) &&
                              chat_sessions.video_calls_chat_session.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/video_calls/video_calls-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="provider">
                                        { item.provider }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="provider_room_name">
                                        { item.provider_room_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="provider_session_reference">
                                        { item.provider_session_reference }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ended_at">
                                        { dataFormatter.dateTimeFormatter(item.ended_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="call_status">
                                        { item.call_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="call_notes">
                                        { item.call_notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!chat_sessions?.video_calls_chat_session?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Chatbot_interactions ChatSession</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>AskedAt</th>
                                
                                
                                
                                <th>UserPrompt</th>
                                
                                
                                
                                
                                
                                <th>ModelName</th>
                                
                                
                                
                                <th>ConfidenceScore</th>
                                
                                
                                
                                <th>TriageLevel</th>
                                
                                
                                
                                <th>EscalatedToDoctor</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {chat_sessions.chatbot_interactions_chat_session && Array.isArray(chat_sessions.chatbot_interactions_chat_session) &&
                              chat_sessions.chatbot_interactions_chat_session.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/chatbot_interactions/chatbot_interactions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="asked_at">
                                        { dataFormatter.dateTimeFormatter(item.asked_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="user_prompt">
                                        { item.user_prompt }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="model_name">
                                        { item.model_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="confidence_score">
                                        { item.confidence_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="triage_level">
                                        { item.triage_level }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="escalated_to_doctor">
                                        { dataFormatter.booleanFormatter(item.escalated_to_doctor) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!chat_sessions?.chatbot_interactions_chat_session?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                

                <BaseDivider />

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

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

export default Chat_sessionsView;