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

const currentApplicationForm: Ref<ApplicationFormDto | undefined> = ref()

export function useApplicationFormTemplate() {
  const applicationFormApi = useApplicationFormTemplateApi()

  async function createApplicationFormTemplate(
    createApplicationFormDto: CreateApplicationFormDto
  ): Promise<ApplicationFormDto> {
    try {
      currentApplicationForm.value = await applicationFormApi.createApplicationFormTemplate(createApplicationFormDto)
      return currentApplicationForm.value
    } catch (e: unknown) {
      if (e instanceof ResponseError) {
        console.error('Failed creating application form:', e.response)
      } else {
        console.error('Failed creating application form:', e)
      }
      return Promise.reject(e)
    }
  }

  async function getAllApplicationFormTemplates(): Promise<PagedApplicationFormDto> {
    try {
      return await applicationFormApi.getAllApplicationFormTemplates()
    } catch (e: unknown) {
      if (e instanceof ResponseError) {
        console.error('Failed retrieving application forms:', e.response)
      } else {
        console.error('Failed retrieving application forms:', e)
      }
      return Promise.reject(e)
    }
  }

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

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

    try {
      currentApplicationForm.value = await applicationFormApi.updateApplicationFormTemplate(id, applicationFormDto)
      return currentApplicationForm.value
    } catch (e: unknown) {
      if (e instanceof ResponseError) {
        console.error(`Failed updating application form with ID ${id}:`, e.response)
      } else {
        console.error(`Failed updating application form with ID ${id}:`, e)
      }
      return Promise.reject(e)
    }
  }

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

  return {
    createApplicationFormTemplate,
    getAllApplicationFormTemplates,
    getApplicationFormTemplateById,
    updateApplicationFormTemplate,
    deleteApplicationFormTemplateById
  }
}
