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/generation_runs/generation_runsSlice'
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";


const Generation_runsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { generation_runs } = useAppSelector((state) => state.generation_runs)
    

    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 generation_runs')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View generation_runs')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/generation_runs/generation_runs-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AdProject</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{generation_runs?.ad_project?.project_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RequestedBy</p>
                    
                    
                        <p>{generation_runs?.requested_by?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RunType</p>
                    <p>{generation_runs?.run_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RunStatus</p>
                    <p>{generation_runs?.run_status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ModelLabel</p>
                    <p>{generation_runs?.model_label}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Temperature</p>
                  <p>{generation_runs?.temperature || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>RetrievedChunkCount</p>
                  <p>{generation_runs?.retrieved_chunk_count || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Generation_outputs GenerationRun</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>OutputType</th>
                                
                                
                                
                                <th>OutputTitle</th>
                                
                                
                                
                                
                                
                                <th>IsExported</th>
                                
                                
                                
                                <th>ExportedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {generation_runs.generation_outputs_generation_run && Array.isArray(generation_runs.generation_outputs_generation_run) &&
                              generation_runs.generation_outputs_generation_run.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/generation_outputs/generation_outputs-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="output_type">
                                        { item.output_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="output_title">
                                        { item.output_title }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="is_exported">
                                        { dataFormatter.booleanFormatter(item.is_exported) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="exported_at">
                                        { dataFormatter.dateTimeFormatter(item.exported_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!generation_runs?.generation_outputs_generation_run?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Generation_runsView;