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/sandbox_scenarios/sandbox_scenariosSlice'
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 EditSandbox_scenarios = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'scenario_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    scenario_type: '',
    

    

    

    

    
    
    

    

    
    scenario_description: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    campaign_brain_config: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    safety_policy: null,
    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    last_run_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { sandbox_scenariosId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="ScenarioName"
    >
        <Field
            name="scenario_name"
            placeholder="ScenarioName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ScenarioType" labelFor="scenario_type">
        <Field name="scenario_type" id="scenario_type" component="select">
          
            <option value="mock_campaign">mock_campaign</option>
          
            <option value="mock_client_call">mock_client_call</option>
          
            <option value="policy_stress_test">policy_stress_test</option>
          
            <option value="competitor_teardown">competitor_teardown</option>
          
            <option value="compliance_drill">compliance_drill</option>
          
            <option value="prompt_regression">prompt_regression</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  
    <FormField label='ScenarioDescription' hasTextareaHeight>
        <Field
            name='scenario_description'
            id='scenario_description'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="draft">draft</option>
          
            <option value="running">running</option>
          
            <option value="completed">completed</option>
          
            <option value="approved">approved</option>
          
            <option value="rejected">rejected</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="LastRunAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.last_run_at ?
                  new Date(
                      dayjs(initialValues.last_run_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'last_run_at': date})}
          />
      </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('/sandbox_scenarios/sandbox_scenarios-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditSandbox_scenarios
