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/ai_models/ai_modelsSlice'
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 EditAi_models = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'display_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'provider': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'model_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    capability_class: '',
    

    

    

    

    
    
    

    

    

    
    max_context_tokens: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'input_cost_per_1k': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'output_cost_per_1k': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    supports_streaming: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    supports_function_calls: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    supports_vision: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { ai_modelsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="DisplayName"
    >
        <Field
            name="display_name"
            placeholder="DisplayName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Provider"
    >
        <Field
            name="provider"
            placeholder="Provider"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ModelKey"
    >
        <Field
            name="model_key"
            placeholder="ModelKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="CapabilityClass" labelFor="capability_class">
        <Field name="capability_class" id="capability_class" component="select">
          
            <option value="chat">chat</option>
          
            <option value="code">code</option>
          
            <option value="vision">vision</option>
          
            <option value="tools">tools</option>
          
            <option value="reasoning">reasoning</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MaxContextTokens"
    >
        <Field
            type="number"
            name="max_context_tokens"
            placeholder="MaxContextTokens"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="InputCostPer1K"
    >
        <Field
            type="number"
            name="input_cost_per_1k"
            placeholder="InputCostPer1K"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="OutputCostPer1K"
    >
        <Field
            type="number"
            name="output_cost_per_1k"
            placeholder="OutputCostPer1K"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='SupportsStreaming' labelFor='supports_streaming'>
        <Field
            name='supports_streaming'
            id='supports_streaming'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='SupportsFunctionCalls' labelFor='supports_function_calls'>
        <Field
            name='supports_function_calls'
            id='supports_function_calls'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='SupportsVision' labelFor='supports_vision'>
        <Field
            name='supports_vision'
            id='supports_vision'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="active">active</option>
          
            <option value="deprecated">deprecated</option>
          
            <option value="disabled">disabled</option>
          
        </Field>
    </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('/ai_models/ai_models-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditAi_models
