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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ModelType</p>
                    <p>{ai_models?.model_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Status</p>
                    <p>{ai_models?.status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Version</p>
                    <p>{ai_models?.version}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Framework</p>
                    <p>{ai_models?.framework}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ArtifactURI</p>
                    <p>{ai_models?.artifact_uri}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Threshold</p>
                  <p>{ai_models?.threshold || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Detections AIModel</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>DetectedAt</th>
                                
                                
                                
                                <th>DetectionType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Confidence</th>
                                
                                
                                
                                <th>RiskScore</th>
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                <th>Evidence</th>
                                
                                
                                
                                <th>RequiresResponse</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {ai_models.detections_model && Array.isArray(ai_models.detections_model) &&
                              ai_models.detections_model.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/detections/detections-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="detected_at">
                                        { dataFormatter.dateTimeFormatter(item.detected_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="detection_type">
                                        { item.detection_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="confidence">
                                        { item.confidence }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="risk_score">
                                        { item.risk_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="evidence">
                                        { item.evidence }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="requires_response">
                                        { dataFormatter.booleanFormatter(item.requires_response) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!ai_models?.detections_model?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Soc_agents PolicyModel</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>AgentName</th>
                                
                                
                                
                                <th>AgentType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                
                                
                                <th>Capabilities</th>
                                
                                
                                
                                <th>ApprovalRequiredByDefault</th>
                                
                                
                                
                                <th>LastHeartbeatAt</th>
                                
                                
                                
                                <th>ActionsSuccessRate</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {ai_models.soc_agents_model && Array.isArray(ai_models.soc_agents_model) &&
                              ai_models.soc_agents_model.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/soc_agents/soc_agents-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="agent_type">
                                        { item.agent_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="capabilities">
                                        { item.capabilities }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="approval_required_by_default">
                                        { dataFormatter.booleanFormatter(item.approval_required_by_default) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_heartbeat_at">
                                        { dataFormatter.dateTimeFormatter(item.last_heartbeat_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="actions_success_rate">
                                        { item.actions_success_rate }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!ai_models?.soc_agents_model?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Ai_recommendations AIModel</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>GeneratedAt</th>
                                
                                
                                
                                <th>RecommendationType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>EstimatedRiskReduction</th>
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {ai_models.ai_recommendations_model && Array.isArray(ai_models.ai_recommendations_model) &&
                              ai_models.ai_recommendations_model.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/ai_recommendations/ai_recommendations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="generated_at">
                                        { dataFormatter.dateTimeFormatter(item.generated_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="recommendation_type">
                                        { item.recommendation_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="estimated_risk_reduction">
                                        { item.estimated_risk_reduction }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!ai_models?.ai_recommendations_model?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                

                <BaseDivider />

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

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

export default Ai_modelsView;