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";



const EditAi_models = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    provider: '',
    

    

    

    

    
    
    
    'model_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'base_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    default_config: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    typical_latency_ms: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_enabled: false,
    

    

    

    

    

    

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

  const { ai_models } = useAppSelector((state) => state.ai_models)
  

  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="ModelName"
    >
        <Field
            name="name"
            placeholder="ModelName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Provider" labelFor="provider">
        <Field name="provider" id="provider" component="select">
          
            <option value="groq">groq</option>
          
            <option value="gemini">gemini</option>
          
            <option value="deepseek">deepseek</option>
          
            <option value="custom">custom</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="ModelVersion"
    >
        <Field
            name="model_name"
            placeholder="ModelVersion"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="APIBaseURL"
    >
        <Field
            name="base_url"
            placeholder="APIBaseURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="DefaultConfiguration" hasTextareaHeight>
        <Field name="default_config" as="textarea" placeholder="DefaultConfiguration" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TypicalLatency(ms)"
    >
        <Field
            type="number"
            name="typical_latency_ms"
            placeholder="TypicalLatency(ms)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Enabled' labelFor='is_enabled'>
        <Field
            name='is_enabled'
            id='is_enabled'
            component={SwitchField}
        ></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
