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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PipelineType</p>
                    <p>{ingestion_pipelines?.pipeline_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>DataSources</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                    <th>DataSourceName</th>
                                
                                
                                
                                    <th>SourceType</th>
                                
                                
                                
                                    <th>Status</th>
                                
                                
                                
                                    <th>ConnectorType</th>
                                
                                
                                
                                    <th>EndpointURL</th>
                                
                                
                                
                                    <th>AuthMethod</th>
                                
                                
                                
                                    <th>PollIntervalSeconds</th>
                                
                                
                                
                                    <th>LastIngestedAt</th>
                                
                                
                                
                                    <th>Notes</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {ingestion_pipelines.data_sources && Array.isArray(ingestion_pipelines.data_sources) &&
                              ingestion_pipelines.data_sources.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/data_sources/data_sources-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                    
                                    
                                    
                                  
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="source_type">
                                        { item.source_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="connector_type">
                                        { item.connector_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="endpoint_url">
                                        { item.endpoint_url }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="auth_method">
                                        { item.auth_method }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="poll_interval_seconds">
                                        { item.poll_interval_seconds }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_ingested_at">
                                        { dataFormatter.dateTimeFormatter(item.last_ingested_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!ingestion_pipelines?.data_sources?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>KafkaTopic</p>
                    <p>{ingestion_pipelines?.kafka_topic}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>DataLakePath</p>
                    <p>{ingestion_pipelines?.data_lake_path}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>ThroughputEPS</p>
                  <p>{ingestion_pipelines?.throughput_eps || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>ErrorRatePercent</p>
                  <p>{ingestion_pipelines?.error_rate_percent || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Ingestion_pipelinesView;