'use client'

import { SurveyBasicRequest, SurveyDetailRequest, SurveyRegistRequest } from '@/types/Survey'
import FormButton from './formButton'
import { useEffect, useState } from 'react'
import BasicRegist from './basicRegist'
import RoofRegist from './roofRegist'
import { useSessionStore } from '@/store/session'
import { useSearchParams } from 'next/navigation'
import { useServey } from '@/hooks/useSurvey'

const roofInfoForm: SurveyDetailRequest = {
  CONTRACT_CAPACITY: null,
  RETAIL_COMPANY: null,
  SUPPLEMENTARY_FACILITIES: null,
  SUPPLEMENTARY_FACILITIES_ETC: null,
  INSTALLATION_SYSTEM: null,
  INSTALLATION_SYSTEM_ETC: null,
  CONSTRUCTION_YEAR: null,
  CONSTRUCTION_YEAR_ETC: null,
  ROOF_MATERIAL: null,
  ROOF_MATERIAL_ETC: null,
  ROOF_SHAPE: null,
  ROOF_SHAPE_ETC: null,
  ROOF_SLOPE: null,
  HOUSE_STRUCTURE: '1',
  HOUSE_STRUCTURE_ETC: null,
  RAFTER_MATERIAL: '1',
  RAFTER_MATERIAL_ETC: null,
  RAFTER_SIZE: null,
  RAFTER_SIZE_ETC: null,
  RAFTER_PITCH: null,
  RAFTER_PITCH_ETC: null,
  RAFTER_DIRECTION: '1',
  OPEN_FIELD_PLATE_KIND: null,
  OPEN_FIELD_PLATE_KIND_ETC: null,
  OPEN_FIELD_PLATE_THICKNESS: null,
  LEAK_TRACE: false,
  WATERPROOF_MATERIAL: null,
  WATERPROOF_MATERIAL_ETC: null,
  INSULATION_PRESENCE: '1',
  INSULATION_PRESENCE_ETC: null,
  STRUCTURE_ORDER: null,
  STRUCTURE_ORDER_ETC: null,
  INSTALLATION_AVAILABILITY: null,
  INSTALLATION_AVAILABILITY_ETC: null,
  MEMO: null,
}

const basicInfoForm: SurveyBasicRequest = {
  REPRESENTATIVE: '',
  STORE: null,
  CONSTRUCTION_POINT: null,
  INVESTIGATION_DATE: new Date().toLocaleDateString('en-CA'),
  BUILDING_NAME: null,
  CUSTOMER_NAME: null,
  POST_CODE: null,
  ADDRESS: null,
  ADDRESS_DETAIL: null,
  SUBMISSION_STATUS: false,
  SUBMISSION_DATE: null,
}

export default function RegistForm() {
  const searchParams = useSearchParams()
  const id = searchParams.get('id')

  const { session } = useSessionStore()
  const { surveyDetail } = useServey(Number(id))

  const [basicInfoData, setBasicInfoData] = useState<SurveyBasicRequest>(basicInfoForm)
  const [roofInfoData, setRoofInfoData] = useState<SurveyDetailRequest>(roofInfoForm)

  useEffect(() => {
    if (session) {
      setBasicInfoData({
        ...basicInfoForm,
        REPRESENTATIVE: session.userNm ?? '',
        STORE: session.role === 'T01' ? '' : session.storeNm ?? '',
        CONSTRUCTION_POINT: session.builderNo ?? '',
      })
    }
    if (id && surveyDetail) {
      const { ID, UPT_DT, REG_DT, DETAIL_INFO, ...rest } = surveyDetail
      setBasicInfoData(rest)
      if (surveyDetail?.DETAIL_INFO) {
        const { ID, UPT_DT, REG_DT, BASIC_INFO_ID, ...rest } = surveyDetail.DETAIL_INFO
        setRoofInfoData(rest)
      }
    }
  }, [session, surveyDetail])

  const surveyData = {
    basic: basicInfoData,
    roof: roofInfoData,
  }

  return (
    <>
      <BasicRegist basicInfoData={basicInfoData} setBasicInfoData={setBasicInfoData} />
      <RoofRegist roofInfoData={roofInfoData} setRoofInfoData={setRoofInfoData} />
      <FormButton surveyData={surveyData} idParam={id} />
    </>
  )
}
