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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>BaseURL</p>
                    <p>{llm_providers?.base_url}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ProviderType</p>
                    <p>{llm_providers?.provider_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RegionPolicy</p>
                    <p>{llm_providers?.region_policy ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='SupportsStreaming'>
                    <SwitchField
                      field={{name: 'supports_streaming', value: llm_providers?.supports_streaming}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='SupportsTools'>
                    <SwitchField
                      field={{name: 'supports_tools', value: llm_providers?.supports_tools}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsActive'>
                    <SwitchField
                      field={{name: 'is_active', value: llm_providers?.is_active}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>APIKeys</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                    <th>Label</th>
                                
                                
                                
                                    <th>KeyFingerprint</th>
                                
                                
                                
                                    <th>KeyStatus</th>
                                
                                
                                
                                    <th>LastRotatedAt</th>
                                
                                
                                
                                    <th>ExpiresAt</th>
                                
                                
                                
                                    <th>IsPrimary</th>
                                
                                
                                
                                    <th>RequestsPerMinuteLimit</th>
                                
                                
                                
                                    <th>DailyRequestLimit</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {llm_providers.api_keys && Array.isArray(llm_providers.api_keys) &&
                              llm_providers.api_keys.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/llm_api_keys/llm_api_keys-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                  
                                    
                                    
                                    
                                    <td data-label="label">
                                        { item.label }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="key_fingerprint">
                                        { item.key_fingerprint }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="key_status">
                                        { item.key_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_rotated_at">
                                        { dataFormatter.dateTimeFormatter(item.last_rotated_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="expires_at">
                                        { dataFormatter.dateTimeFormatter(item.expires_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_primary">
                                        { dataFormatter.booleanFormatter(item.is_primary) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="requests_per_minute_limit">
                                        { item.requests_per_minute_limit }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="daily_request_limit">
                                        { item.daily_request_limit }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!llm_providers?.api_keys?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>Models</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                    <th>ModelName</th>
                                
                                
                                
                                    <th>ModelCode</th>
                                
                                
                                
                                    <th>MaxInputTokens</th>
                                
                                
                                
                                    <th>MaxOutputTokens</th>
                                
                                
                                
                                    <th>SupportsVision</th>
                                
                                
                                
                                    <th>SupportsFunctionCalling</th>
                                
                                
                                
                                    <th>PricePerMillionInputTokens</th>
                                
                                
                                
                                    <th>PricePerMillionOutputTokens</th>
                                
                                
                                
                                    <th>IsActive</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {llm_providers.models && Array.isArray(llm_providers.models) &&
                              llm_providers.models.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/llm_models/llm_models-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                  
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="model_code">
                                        { item.model_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="max_input_tokens">
                                        { item.max_input_tokens }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="max_output_tokens">
                                        { item.max_output_tokens }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="supports_vision">
                                        { dataFormatter.booleanFormatter(item.supports_vision) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="supports_function_calling">
                                        { dataFormatter.booleanFormatter(item.supports_function_calling) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="price_per_million_input_tokens">
                                        { item.price_per_million_input_tokens }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="price_per_million_output_tokens">
                                        { item.price_per_million_output_tokens }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!llm_providers?.models?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Llm_models Provider</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>ModelName</th>
                                
                                
                                
                                <th>ModelCode</th>
                                
                                
                                
                                <th>MaxInputTokens</th>
                                
                                
                                
                                <th>MaxOutputTokens</th>
                                
                                
                                
                                <th>SupportsVision</th>
                                
                                
                                
                                <th>SupportsFunctionCalling</th>
                                
                                
                                
                                <th>PricePerMillionInputTokens</th>
                                
                                
                                
                                <th>PricePerMillionOutputTokens</th>
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {llm_providers.llm_models_provider && Array.isArray(llm_providers.llm_models_provider) &&
                              llm_providers.llm_models_provider.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/llm_models/llm_models-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="model_code">
                                        { item.model_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="max_input_tokens">
                                        { item.max_input_tokens }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="max_output_tokens">
                                        { item.max_output_tokens }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="supports_vision">
                                        { dataFormatter.booleanFormatter(item.supports_vision) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="supports_function_calling">
                                        { dataFormatter.booleanFormatter(item.supports_function_calling) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="price_per_million_input_tokens">
                                        { item.price_per_million_input_tokens }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="price_per_million_output_tokens">
                                        { item.price_per_million_output_tokens }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!llm_providers?.llm_models_provider?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Llm_api_keys Provider</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Label</th>
                                
                                
                                
                                <th>KeyFingerprint</th>
                                
                                
                                
                                <th>KeyStatus</th>
                                
                                
                                
                                <th>LastRotatedAt</th>
                                
                                
                                
                                <th>ExpiresAt</th>
                                
                                
                                
                                <th>IsPrimary</th>
                                
                                
                                
                                <th>RequestsPerMinuteLimit</th>
                                
                                
                                
                                <th>DailyRequestLimit</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {llm_providers.llm_api_keys_provider && Array.isArray(llm_providers.llm_api_keys_provider) &&
                              llm_providers.llm_api_keys_provider.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/llm_api_keys/llm_api_keys-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="label">
                                        { item.label }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="key_fingerprint">
                                        { item.key_fingerprint }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="key_status">
                                        { item.key_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_rotated_at">
                                        { dataFormatter.dateTimeFormatter(item.last_rotated_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="expires_at">
                                        { dataFormatter.dateTimeFormatter(item.expires_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_primary">
                                        { dataFormatter.booleanFormatter(item.is_primary) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="requests_per_minute_limit">
                                        { item.requests_per_minute_limit }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="daily_request_limit">
                                        { item.daily_request_limit }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!llm_providers?.llm_api_keys_provider?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Key_rotation_policies Provider</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>PolicyName</th>
                                
                                
                                
                                <th>Strategy</th>
                                
                                
                                
                                <th>RotateEverySeconds</th>
                                
                                
                                
                                <th>AutoDisableOnErrors</th>
                                
                                
                                
                                <th>ErrorThreshold</th>
                                
                                
                                
                                <th>CooldownSeconds</th>
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {llm_providers.key_rotation_policies_provider && Array.isArray(llm_providers.key_rotation_policies_provider) &&
                              llm_providers.key_rotation_policies_provider.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/key_rotation_policies/key_rotation_policies-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="strategy">
                                        { item.strategy }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rotate_every_seconds">
                                        { item.rotate_every_seconds }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="auto_disable_on_errors">
                                        { dataFormatter.booleanFormatter(item.auto_disable_on_errors) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="error_threshold">
                                        { item.error_threshold }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="cooldown_seconds">
                                        { item.cooldown_seconds }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!llm_providers?.key_rotation_policies_provider?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Llm_providersView;