import { mdiChartTimelineVariant, mdiUpload } from '@mdi/js'
import Head from 'next/head'
import React, { ReactElement, useEffect, useState } from 'react'
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import dayjs from "dayjs";

import CardBox from '../../components/CardBox'
import LayoutAuthenticated from '../../layouts/Authenticated'
import SectionMain from '../../components/SectionMain'
import SectionTitleLineWithButton from '../../components/SectionTitleLineWithButton'
import { getPageTitle } from '../../config'

import { Field, Form, Formik } from 'formik'
import FormField from '../../components/FormField'
import BaseDivider from '../../components/BaseDivider'
import BaseButtons from '../../components/BaseButtons'
import BaseButton from '../../components/BaseButton'
import FormCheckRadio from '../../components/FormCheckRadio'
import FormCheckRadioGroup from '../../components/FormCheckRadioGroup'
import FormFilePicker from '../../components/FormFilePicker'
import FormImagePicker from '../../components/FormImagePicker'
import { SelectField } from "../../components/SelectField";
import { SelectFieldMany } from "../../components/SelectFieldMany";
import { SwitchField } from '../../components/SwitchField'
import {RichTextField} from "../../components/RichTextField";

import { update, fetch } from '../../stores/llm_api_keys/llm_api_keysSlice'
import { useAppDispatch, useAppSelector } from '../../stores/hooks'
import { useRouter } from 'next/router'
import {saveFile} from "../../helpers/fileSaver";
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from "../../components/ImageField";

import {hasPermission} from "../../helpers/userPermissions";



const EditLlm_api_keys = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    provider: null,
    

    
    
    
    'label': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'key_fingerprint': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    key_status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    last_rotated_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    expires_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_primary: false,
    

    

    

    

    

    

    
    
    

    

    

    
    requests_per_minute_limit: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    daily_request_limit: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

    
    
    
  }
  const [initialValues, setInitialValues] = useState(initVals)

  const { llm_api_keys } = useAppSelector((state) => state.llm_api_keys)
  
  const { currentUser } = useAppSelector((state) => state.auth);
  

  const { llm_api_keysId } = router.query

  useEffect(() => {
    dispatch(fetch({ id: llm_api_keysId }))
  }, [llm_api_keysId])

  useEffect(() => {
    if (typeof llm_api_keys === 'object') {
      setInitialValues(llm_api_keys)
    }
  }, [llm_api_keys])

  useEffect(() => {
      if (typeof llm_api_keys === 'object') {

          const newInitialVal = {...initVals};

          Object.keys(initVals).forEach(el => newInitialVal[el] = (llm_api_keys)[el])

          setInitialValues(newInitialVal);
      }
  }, [llm_api_keys])

  const handleSubmit = async (data) => {
    await dispatch(update({ id: llm_api_keysId, data }))
    await router.push('/llm_api_keys/llm_api_keys-list')
  }

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit llm_api_keys')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit llm_api_keys'} main>
        {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            enableReinitialize
            initialValues={initialValues}
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Provider' labelFor='provider'>
        <Field
            name='provider'
            id='provider'
            component={SelectField}
            options={initialValues.provider}
            itemRef={'llm_providers'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="Label"
    >
        <Field
            name="label"
            placeholder="Label"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="KeyFingerprint"
    >
        <Field
            name="key_fingerprint"
            placeholder="KeyFingerprint"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="KeyStatus" labelFor="key_status">
        <Field name="key_status" id="key_status" component="select">
          
            <option value="active">active</option>
          
            <option value="disabled">disabled</option>
          
            <option value="rate_limited">rate_limited</option>
          
            <option value="compromised">compromised</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="LastRotatedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.last_rotated_at ?
                  new Date(
                      dayjs(initialValues.last_rotated_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'last_rotated_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="ExpiresAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.expires_at ?
                  new Date(
                      dayjs(initialValues.expires_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'expires_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsPrimary' labelFor='is_primary'>
        <Field
            name='is_primary'
            id='is_primary'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  
    <FormField
        label="RequestsPerMinuteLimit"
    >
        <Field
            type="number"
            name="requests_per_minute_limit"
            placeholder="RequestsPerMinuteLimit"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DailyRequestLimit"
    >
        <Field
            type="number"
            name="daily_request_limit"
            placeholder="DailyRequestLimit"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='organizations' labelFor='organizations'>
        <Field
            name='organizations'
            id='organizations'
            component={SelectField}
            options={initialValues.organizations}
            itemRef={'organizations'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  


  

              <BaseDivider />
              <BaseButtons>
                <BaseButton type="submit" color="info" label="Submit" />
                <BaseButton type="reset" color="info" outline label="Reset" />
                <BaseButton type='reset' color='danger' outline label='Cancel' onClick={() => router.push('/llm_api_keys/llm_api_keys-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditLlm_api_keys
