<template>
  <Fluid>
    <div class="flex mt-6">
      <div class="card flex flex-col gap-4 w-full">
        <div>
          <h2 class="text-3xl font-bold mb-4">Ks document</h2>
        </div>
        <form @submit.prevent="submitForm" class="p-fluid">
          <div class="col-12 md:col-6 mb-4">
            <span class="p-float-label">
              <label for="description" v-tooltip="'A brief overview of the document.'">Document
                Description</label>
              <InputText id="description" type="text" v-model="formData.description" required class="w-full" />
            </span>
          </div>

          <div class="col-12 md:col-6 mb-4">
            <span class="p-float-label">
              <label for="type"
                v-tooltip="'Specify the type of file here. e.g, PDF Document, DOCX, TXT, MD Document etc..'">File
                Type</label>
              <InputText id="type" v-model="formData.type" required class="w-full" />
            </span>
          </div>
          <div class="col-12 md:col-6 mb-4">
            <span class="p-float-label">
              <label for="ksProjectName" v-tooltip="'Enter the project name here.'">KS Project Name</label>
              <InputText id="ksProjectName" v-model="formData.ksProjectName" required class="w-full" />
            </span>
          </div>
          <div class="col-12 md:col-6 mb-4">
            <span class="p-float-label">
              <label for="ksApplicationName" v-tooltip="'Enter the application name here.'">KS Application Name</label>
              <InputText id="ksApplicationName" v-model="formData.ksApplicationName" required class="w-full" />
            </span>
          </div>

          <div class="col-12 md:col-6 mb-4">
            <span class="p-float-label">
              <label for="ksDocType" v-tooltip="'Specify the type of document e.g functional, code_istruction'">KS
                Document Type</label>+
              <Dropdown id="ksDocType" v-model="formData.ksDocType" :options="documentTypeOptions" optionLabel="name">
              </Dropdown>
            </span>
          </div>

          <div class="col-12 md:col-6 mb-4">
            <span class="p-float-label">
              <label for="ksDocSource"
                v-tooltip="'The KS Document Source field is intended to capture the origin or source from where the document was obtained or associated.  ex.. Retrieved from DevopsJ2Cloud Git Repository - CSV System Configuration '">KS
                Document Source</label>
              <InputText id="ksDocSource" v-model="formData.ksDocSource" required class="w-full" />
            </span>
          </div>

          <div class="col-12 md:col-6 mb-4">
            <span class="p-float-label">
              <label for="defaultChunkSize" v-tooltip="'Define the default size for chunks of data.'">Default Chunk
                Size</label>
              <InputNumber id="defaultChunkSize" v-model="formData.defaultChunkSize" required class="w-full" />
            </span>
          </div>

          <div class="col-12 md:col-6 mb-4">
            <span class="p-float-label">
              <label for="minChunkSize" v-tooltip="'Specify the minimum allowable size for chunks'">Min Chunk
                Size</label>
              <InputNumber id="minChunkSize" v-model="formData.minChunkSize" required class="w-full" />
            </span>
          </div>

          <div class="col-12 md:col-6 mb-4">
            <span class="p-float-label">
              <label for="maxNumberOfChunks" v-tooltip="'Set the maximum number of chunks allowed.'">Max Number of
                Chunks</label>
              <InputNumber id="maxNumberOfChunks" v-model="formData.maxNumberOfChunks" required class="w-full" />
            </span>
          </div>

          <div class="col-12 md:col-6 mb-4">
            <span class="p-float-label">
              <label for="minChunkSizeToEmbed" v-tooltip="'Define the minimum chunk size that can be embedded.'">Min
                Chunk Size to
                Embed</label>
              <InputNumber id="minChunkSizeToEmbed" v-model="formData.minChunkSizeToEmbed" required class="w-full" />
            </span>
          </div>

          <div class="col-12 mb-4">
            <label for="file" class="block text-lg mb-2" v-tooltip="'Upload the file here.'">File</label>
            <div class="flex align-items-center">
              <FileUpload ref="fileUpload" mode="basic" :maxFileSize="10000000000" chooseLabel="Select File"
                class="p-button-rounded" @select="onFileSelect" />
            </div>
          </div>
          <Button type="submit" label="Submit" :fluid="false"></Button>
        </form>
      </div>
    </div>
  </Fluid>
</template>

<script setup>
import axios from 'axios';
import { useToast } from 'primevue/usetoast';
import { ref } from 'vue';
import { useRouter } from 'vue-router';

const toast = useToast();
const router = useRouter();

const documentTypeOptions = ref([
  { name: 'Functional', value: 'functional' },
  { name: 'Code Instruction', value: 'code_instruction' },
  { name: 'Specification', value: 'specification' },
  { name: 'Other', value: 'other' }
]);

const formData = ref({
  description: '',
  type: '',
  ksProjectName: '',
  ksApplicationName: '',
  ksDocType: 'functional',
  ksDocSource: '',
  defaultChunkSize: 2000,
  minChunkSize: 50,
  maxNumberOfChunks: 1000,
  minChunkSizeToEmbed: 50
});

const fileUpload = ref(null);
const selectedFile = ref(null);

const onFileSelect = (event) => {
  selectedFile.value = event.files[0];
};

const submitForm = async () => {
  const formDataToSend = new FormData();

  if (selectedFile.value) {
    formDataToSend.append('file', selectedFile.value);
  }

  formDataToSend.append('description', formData.value.description);
  formDataToSend.append('type', formData.value.type);
  formDataToSend.append('ksApplicationName', formData.value.ksApplicationName);
  formDataToSend.append('ksProjectName', formData.value.ksProjectName);
  formDataToSend.append('ksDocType', formData.value.ksDocType.value);
  formDataToSend.append('ksDocSource', formData.value.ksDocSource);
  formDataToSend.append('defaultChunkSize', formData.value.defaultChunkSize);
  formDataToSend.append('minChunkSize', formData.value.minChunkSize);
  formDataToSend.append('maxNumberOfChunks', formData.value.maxNumberOfChunks);
  formDataToSend.append('minChunkSizeToEmbed', formData.value.minChunkSizeToEmbed);

  try {
    const response = await axios.post('/upload', formDataToSend, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    });
    console.log('Upload successful:', response.data);
    toast.add({ severity: 'success', summary: 'Success', detail: 'File uploaded successfully', life: 3000 });

    // Redirect to desktop.vue
    router.push({ name: 'ks-document' });
  } catch (error) {
    console.error('Upload failed:', error);
    toast.add({ severity: 'error', summary: 'Error', detail: 'File upload failed', life: 3000 });
  }
};

</script>

<style scoped></style>