import type {
  CreateApplicationFormDto,
  CreateFormElementDto,
  ApplicationFormDto,
  PagedApplicationFormDto
} from '~~/.api-client'
import { useApplicationFormApi } from './useApplicationFormApi'

export function useApplicationForm() {
  const applicationFormApi = useApplicationFormApi()

  async function createApplicationForm(
    createApplicationFormDto: CreateApplicationFormDto
  ): Promise<ApplicationFormDto> {
    try {
      return await applicationFormApi.createApplicationForm(createApplicationFormDto)
    } catch (e: unknown) {
      console.error('Failed creating application form:', e)
      return Promise.reject(e)
    }
  }

  async function getAllApplicationForms(organizationId: string): Promise<PagedApplicationFormDto> {
    try {
      return await applicationFormApi.getAllApplicationForms(organizationId)
    } catch (e: unknown) {
      console.error('Failed retrieving application forms:', e, JSON.stringify(e))
      return Promise.reject(e)
    }
  }

  async function getApplicationFormById(id: string): Promise<ApplicationFormDto> {
    try {
      return await applicationFormApi.getApplicationFormById(id)
    } catch (e: unknown) {
      console.error(`Failed retrieving application form with ID ${id}:`, e)
      return Promise.reject(e)
    }
  }

  async function updateApplicationForm(
    id?: string,
    applicationFormDto?: ApplicationFormDto
  ): Promise<ApplicationFormDto> {
    if (!id || !applicationFormDto) {
      return Promise.reject(new Error('ID or application form DTO missing'))
    }

    try {
      return await applicationFormApi.updateApplicationForm(id, applicationFormDto)
    } catch (e: unknown) {
      console.error(`Failed updating application form with ID ${id}:`, e)
      return Promise.reject(e)
    }
  }

  async function deleteApplicationFormById(id: string): Promise<void> {
    try {
      return await applicationFormApi.deleteApplicationFormById(id)
    } catch (e: unknown) {
      console.error(`Failed deleting application form with ID ${id}:`, e)
      return Promise.reject(e)
    }
  }

  async function submitApplicationForm(id: string): Promise<ApplicationFormDto> {
    if (!id) {
      return Promise.reject(new Error('ID missing'))
    }

    try {
      return await applicationFormApi.submitApplicationForm(id)
    } catch (e: unknown) {
      console.error(`Failed submitting application form with ID ${id}:`, e)
      return Promise.reject(e)
    }
  }

  async function addFormElementToSection(
    applicationFormId: string,
    sectionId: string,
    createFormElementDto: CreateFormElementDto,
    position: number
  ): Promise<ApplicationFormDto> {
    if (!applicationFormId || !sectionId) {
      return Promise.reject(new Error('Application form ID or section ID missing'))
    }

    try {
      return await applicationFormApi.addFormElementToSection(
        applicationFormId,
        sectionId,
        createFormElementDto,
        position
      )
    } catch (e: unknown) {
      console.error(`Failed adding form element to section ${sectionId}:`, e)
      return Promise.reject(e)
    }
  }

  return {
    createApplicationForm,
    getAllApplicationForms,
    getApplicationFormById,
    updateApplicationForm,
    deleteApplicationFormById,
    submitApplicationForm,
    addFormElementToSection
  }
}
