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/chatbot_configs/chatbot_configsSlice'
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 EditChatbot_configsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    store: null,
    

    
    
    
    'bot_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    model_provider: '',
    

    

    

    

    
    
    
    'model_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    system_prompt: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'temperature': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_enabled: false,
    

    

    

    

    

    

    
    
    

    
    welcome_message: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    handoff_mode: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { id } = router.query

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

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

  useEffect(() => {
      if (typeof chatbot_configs === 'object') {
          const newInitialVal = {...initVals};
          Object.keys(initVals).forEach(el => newInitialVal[el] = (chatbot_configs)[el])
          setInitialValues(newInitialVal);
      }
  }, [chatbot_configs])

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  
    <FormField
        label="BotName"
    >
        <Field
            name="bot_name"
            placeholder="BotName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ModelProvider" labelFor="model_provider">
        <Field name="model_provider" id="model_provider" component="select">
          
            <option value="openai">openai</option>
          
            <option value="anthropic">anthropic</option>
          
            <option value="google">google</option>
          
            <option value="azure_openai">azure_openai</option>
          
            <option value="custom">custom</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="SystemPrompt" hasTextareaHeight>
        <Field name="system_prompt" as="textarea" placeholder="SystemPrompt" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Temperature"
    >
        <Field
            type="number"
            name="temperature"
            placeholder="Temperature"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsEnabled' labelFor='is_enabled'>
        <Field
            name='is_enabled'
            id='is_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  
    <FormField label="WelcomeMessage" hasTextareaHeight>
        <Field name="welcome_message" as="textarea" placeholder="WelcomeMessage" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="HandoffMode" labelFor="handoff_mode">
        <Field name="handoff_mode" id="handoff_mode" component="select">
          
            <option value="none">none</option>
          
            <option value="email">email</option>
          
            <option value="live_chat_ticket">live_chat_ticket</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('/chatbot_configs/chatbot_configs-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditChatbot_configsPage
