/**
 * New Publish Events Page
 * Cleaned up version
 */

import { mdiChartTimelineVariant } from '@mdi/js';
import Head from 'next/head';
import React, { ReactElement } from 'react';

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 { SelectField } from '../../components/SelectField';

import { create } from '../../stores/publish_events/publish_eventsSlice';
import { useAppDispatch } from '../../stores/hooks';
import { useRouter } from 'next/router';
import type { PublishEvent } from '../../types/entities';

const initialValues = {
  project: '',
  user: '',
  from_environment: 'dev',
  to_environment: 'dev',
  started_at: '',
  finished_at: '',
  status: 'queued',
  error_message: '',
  pages_copied: '',
  transitions_copied: '',
  audios_copied: '',
};

const Publish_eventsNew = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();

  const handleSubmit = async (data: typeof initialValues) => {
    await dispatch(create(data as unknown as Partial<PublishEvent>));
    await router.push('/publish_events/publish_events-list');
  };

  return (
    <>
      <Head>
        <title>{getPageTitle('New Publish Event')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title='New Publish Event'
          main
        >
          {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            initialValues={initialValues}
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>
              <FormField label='Project' labelFor='project'>
                <Field
                  name='project'
                  id='project'
                  component={SelectField}
                  options={[]}
                  itemRef='projects'
                />
              </FormField>

              <FormField label='User' labelFor='user'>
                <Field
                  name='user'
                  id='user'
                  component={SelectField}
                  options={[]}
                  itemRef='users'
                />
              </FormField>

              <FormField label='From Environment' labelFor='from_environment'>
                <Field
                  name='from_environment'
                  id='from_environment'
                  component='select'
                >
                  <option value='dev'>dev</option>
                  <option value='stage'>stage</option>
                  <option value='production'>production</option>
                </Field>
              </FormField>

              <FormField label='To Environment' labelFor='to_environment'>
                <Field
                  name='to_environment'
                  id='to_environment'
                  component='select'
                >
                  <option value='dev'>dev</option>
                  <option value='stage'>stage</option>
                  <option value='production'>production</option>
                </Field>
              </FormField>

              <FormField label='Started At'>
                <Field
                  type='datetime-local'
                  name='started_at'
                  placeholder='Started At'
                />
              </FormField>

              <FormField label='Finished At'>
                <Field
                  type='datetime-local'
                  name='finished_at'
                  placeholder='Finished At'
                />
              </FormField>

              <FormField label='Status' labelFor='status'>
                <Field name='status' id='status' component='select'>
                  <option value='queued'>queued</option>
                  <option value='running'>running</option>
                  <option value='success'>success</option>
                  <option value='failed'>failed</option>
                </Field>
              </FormField>

              <FormField label='Error Message' hasTextareaHeight>
                <Field
                  name='error_message'
                  as='textarea'
                  placeholder='Error Message'
                />
              </FormField>

              <FormField label='Pages Copied'>
                <Field
                  type='number'
                  name='pages_copied'
                  placeholder='Pages Copied'
                />
              </FormField>

              <FormField label='Transitions Copied'>
                <Field
                  type='number'
                  name='transitions_copied'
                  placeholder='Transitions Copied'
                />
              </FormField>

              <FormField label='Audios Copied'>
                <Field
                  type='number'
                  name='audios_copied'
                  placeholder='Audios Copied'
                />
              </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('/publish_events/publish_events-list')
                  }
                />
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  );
};

Publish_eventsNew.getLayout = function getLayout(page: ReactElement) {
  return (
    <LayoutAuthenticated permission='CREATE_PUBLISH_EVENTS'>
      {page}
    </LayoutAuthenticated>
  );
};

export default Publish_eventsNew;
