import {
  type CreateApplicationFormDto,
  type ApplicationFormDto,
  type PagedApplicationFormDto,
  ResponseError
} 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) {
      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 getAllApplicationForms(organizationId: string): Promise<PagedApplicationFormDto> {
    try {
      return await applicationFormApi.getAllApplicationForms(organizationId)
    } 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 getApplicationFormById(id: string): Promise<ApplicationFormDto> {
    try {
      return await applicationFormApi.getApplicationFormById(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 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) {
      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 deleteApplicationFormById(id: string): Promise<void> {
    try {
      return await applicationFormApi.deleteApplicationFormById(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)
    }
  }

  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) {
      if (e instanceof ResponseError) {
        console.error(`Failed submitting application form with ID ${id}:`, e.response)
      } else {
        console.error(`Failed submitting application form with ID ${id}:`, e)
      }
      return Promise.reject(e)
    }
  }

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