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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AgentName</p>
                    <p>{ai_agents?.name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Handle</p>
                    <p>{ai_agents?.handle}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Description</p>
                  {ai_agents.description
                    ? <p dangerouslySetInnerHTML={{__html: ai_agents.description}}/>
                    : <p>No data</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>SystemPrompt</p>
                  {ai_agents.system_prompt
                    ? <p dangerouslySetInnerHTML={{__html: ai_agents.system_prompt}}/>
                    : <p>No data</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>DefaultModel</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{ai_agents?.default_model?.display_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PersonaType</p>
                    <p>{ai_agents?.persona_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>PriorityRank</p>
                  <p>{ai_agents?.priority_rank || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsEnabled'>
                    <SwitchField
                      field={{name: 'is_enabled', value: ai_agents?.is_enabled}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AvatarImages</p>
                    {ai_agents?.avatar_images?.length
                      ? (
                        <ImageField
                          name={'avatar_images'}
                          image={ai_agents?.avatar_images}
                          className='w-20 h-20'
                        />
                      ) : <p>No AvatarImages</p>
                    }
                </div>
              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Agent_skill_assignments Agent</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Proficiency</th>
                                
                                
                                
                                <th>IsPrimary</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {ai_agents.agent_skill_assignments_agent && Array.isArray(ai_agents.agent_skill_assignments_agent) &&
                              ai_agents.agent_skill_assignments_agent.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/agent_skill_assignments/agent_skill_assignments-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="proficiency">
                                        { item.proficiency }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_primary">
                                        { dataFormatter.booleanFormatter(item.is_primary) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!ai_agents?.agent_skill_assignments_agent?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Messages SenderAgent</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>Format</th>
                                
                                
                                
                                <th>IsStreaming</th>
                                
                                
                                
                                <th>TokenCount</th>
                                
                                
                                
                                <th>SentAt</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {ai_agents.messages_sender_agent && Array.isArray(ai_agents.messages_sender_agent) &&
                              ai_agents.messages_sender_agent.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="format">
                                        { item.format }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_streaming">
                                        { dataFormatter.booleanFormatter(item.is_streaming) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="token_count">
                                        { item.token_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sent_at">
                                        { dataFormatter.dateTimeFormatter(item.sent_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!ai_agents?.messages_sender_agent?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Debug_sessions AssignedAgent</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>ErrorSummary</th>
                                
                                
                                
                                <th>ErrorDetails</th>
                                
                                
                                
                                <th>OpenedAt</th>
                                
                                
                                
                                <th>ClosedAt</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {ai_agents.debug_sessions_assigned_agent && Array.isArray(ai_agents.debug_sessions_assigned_agent) &&
                              ai_agents.debug_sessions_assigned_agent.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/debug_sessions/debug_sessions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="error_summary">
                                        { item.error_summary }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="error_details">
                                        { item.error_details }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="opened_at">
                                        { dataFormatter.dateTimeFormatter(item.opened_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="closed_at">
                                        { dataFormatter.dateTimeFormatter(item.closed_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!ai_agents?.debug_sessions_assigned_agent?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Debug_fixes ProposedBy</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>PatchDiff</th>
                                
                                
                                
                                
                                
                                <th>ApplyStatus</th>
                                
                                
                                
                                <th>ProposedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {ai_agents.debug_fixes_proposed_by && Array.isArray(ai_agents.debug_fixes_proposed_by) &&
                              ai_agents.debug_fixes_proposed_by.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/debug_fixes/debug_fixes-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="patch_diff">
                                        { item.patch_diff }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="apply_status">
                                        { item.apply_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="proposed_at">
                                        { dataFormatter.dateTimeFormatter(item.proposed_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!ai_agents?.debug_fixes_proposed_by?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Ai_agentsView;