import { axiosInstance } from '@/libs/axios'

export interface SurveySalesBasicInfo {
  id?: number
  representative: string
  store: string | null
  construction_point: string | null
  investigation_date: string | null
  building_name: string | null
  customer_name: string | null
  post_code: string | null
  address: string | null
  address_detail: string | null
  submission_status: boolean
  submission_date?: string | null
  detail_info?: SurveySalesDetailInfo | null
}

export interface SurveySalesDetailInfo {
  id?: number
  contract_capacity: string | null
  retail_company: string | null
  supplementary_facilities: number | null
  supplementary_facilities_etc: string | null
  installation_system: number | null
  installation_system_etc: string | null
  construction_year: number | null
  construction_year_etc: string | null
  roof_material: number | null
  roof_material_etc: string | null
  roof_shape: number | null
  roof_shape_etc: string | null
  roof_slope: string | null
  house_structure: number | null
  house_structure_etc: string | null
  rafter_material: number | null
  rafter_material_etc: string | null
  rafter_size: number | null
  rafter_size_etc: string | null
  rafter_pitch: number | null
  rafter_pitch_etc: string | null
  rafter_direction: number | null
  open_field_plate_kind: number | null
  open_field_plate_kind_etc: string | null
  open_field_plate_thickness: string | null
  leak_trace: boolean | null
  waterproof_material: number | null
  waterproof_material_etc: string | null
  insulation_presence: number | null
  insulation_presence_etc: string | null
  structure_order: number | null
  structure_order_etc: string | null
  installation_availability: number | null
  installation_availability_etc: string | null
  memo: string | null
}

export const surveySalesApi = {
  create: async (data: SurveySalesBasicInfo): Promise<number> => {
    try {
      const response = await axiosInstance(null).post<SurveySalesBasicInfo>('/api/survey-sales', data)
      return response.data.id ?? 0
    } catch (error) {
      console.error(error)
      return 0
    }
  },
  getList: async (): Promise<SurveySalesBasicInfo[] | []> => {
    try {
      const response = await axiosInstance(null).get<SurveySalesBasicInfo[]>('/api/survey-sales')
      return response.data
    } catch (error) {
      console.error(error)
      return []
    }
  },
  getDetail: async (id: number): Promise<SurveySalesBasicInfo | null> => {
    try {
      const response = await axiosInstance(null).get<SurveySalesBasicInfo>(`/api/survey-sales/${id}`)
      return response.data
    } catch (error) {
      console.error(error)
      return null
    }
  },
  update: async (id: number, data: SurveySalesBasicInfo): Promise<SurveySalesBasicInfo | null> => {
    try {
      const response = await axiosInstance(null).put<SurveySalesBasicInfo>(`/api/survey-sales/${id}`, data)
      return response.data
    } catch (error) {
      console.error(error)
      return null
    }
  },
  delete: async (id: number, isDetail: boolean = false): Promise<boolean> => {
    try {
      await axiosInstance(null).delete(`/api/survey-sales/${id}`, {
        params: {
          detail_id: isDetail ? id : undefined,
        },
      })
      return true
    } catch (error) {
      throw error
    }
  },
  createDetail: async (surveyId: number, data: SurveySalesDetailInfo): Promise<boolean> => {
    try {
      await axiosInstance(null).post<SurveySalesDetailInfo>(`/api/survey-sales/${surveyId}`, data)
      return true
    } catch (error) {
      throw error
    }
  },
  confirm: async (id: number): Promise<boolean> => {
    try {
      await axiosInstance(null).patch<SurveySalesBasicInfo>(`/api/survey-sales/${id}`)
      return true
    } catch (error) {
      throw error
    }
  },
  // update: async (data: SurveySalesBasicInfo): Promise<SurveySalesBasicInfo> => {
  //   const response = await axiosInstance.put<SurveySalesBasicInfo>(`/api/survey-sales`, data)
  //   return response.data
  // },
}
