export interface MembershipApplicationApplicantForm {
  name: string
  email: string
  ic_number: string
  birth_date: string
  birth_place: string
  gender: string
  marriage_status: string
  address: string
  phone_number: string
  office_number: string
  postcode: string
  employer_name: string
  employer_address: string
  current_position: string
  start_work_date: string
  stock_monthly_contribution: string
  fee_monthly_contribution: string
}

export interface MembershipApplicationHeirForm {
  name: string
  ic_number: string
  relationship: string
  phone_number: string
}

export interface MembershipApplicationReferenceForm {
  ic_number: string
  user_id: string
  name: string
}

export interface MembershipApplicationReferencesForm {
  proposer: MembershipApplicationReferenceForm
  supporter: MembershipApplicationReferenceForm
}

export interface MembershipApplicationDocumentsForm {
  ic_copy: File | null
  photo: File | null
  salary_slip: File | null
  employer_letter: File | null
}

export interface MembershipApplicationFormState {
  applicant: MembershipApplicationApplicantForm
  heirs: MembershipApplicationHeirForm[]
  references: MembershipApplicationReferencesForm
  documents: MembershipApplicationDocumentsForm
}

export interface MemberLookupResponse {
  success: boolean
  message: string
  data: {
    id: string
    name: string
    ic_number: string
  } | null
}

export interface MembershipApplicationSubmitResponse {
  success: boolean
  message: string
  data: {
    id: string
    application_number: string
    status: string
    submitted_at: string
  }
}

export type MembershipApplicationStatus =
  | 'SUBMITTED'
  | 'PENDING_BOARD'
  | 'MANAGEMENT_REJECTED'
  | 'PENDING_NOTIFICATION'
  | 'COMPLETED'

export type MembershipApplicationBoardResult = 'PASS' | 'FAIL'

export interface MembershipApplicationApplicantSummary {
  name: string
  email: string
  ic_number: string
}

export interface MembershipApplicationListItem {
  id: string
  application_number: string
  status: MembershipApplicationStatus
  board_result: MembershipApplicationBoardResult | null
  submitted_at: string | null
  applicant: MembershipApplicationApplicantSummary | null
  created_at: string | null
}

export interface ListMembershipApplicationsParams {
  page?: number
  per_page?: number
  sort_by?: string
  sort_order?: 'asc' | 'desc'
  search?: string
  status?: string
}

export interface MembershipApplicationApplicantDetail {
  id: string
  name: string
  email: string
  ic_number: string
  birth_date: string | null
  birth_place: string | null
  gender: string | null
  marriage_status: string | null
  address: string | null
  phone_number: string | null
  office_number: string | null
  postcode: string | null
  employer_name: string | null
  employer_address: string | null
  current_position: string | null
  start_work_date: string | null
  stock_monthly_contribution: string | number | null
  fee_monthly_contribution: string | number | null
}

export interface MembershipApplicationHeirDetail {
  id: string
  name: string
  ic_number: string
  relationship: string
  phone_number: string
}

export interface MembershipApplicationReferenceMember {
  id: string
  name: string
  ic_number: string
}

export interface MembershipApplicationReferenceDetail {
  id: string
  reference_type: 'PROPOSER' | 'SUPPORTER'
  user_id: string | null
  member: MembershipApplicationReferenceMember | null
  assigned_by: string | null
  assigned_at: string | null
}

export interface MembershipApplicationDocumentDetail {
  id: string
  name: string
  type: string
  mime_type: string | null
  file_size: number | null
}

export interface MembershipApplicationReviewDetail {
  id: string
  stage: 'MANAGEMENT' | 'BOARD'
  decision: string | null
  remarks: string | null
  reviewer: { id: string; name: string } | null
  reviewed_at: string | null
}

export interface MembershipApplicationDetail {
  id: string
  application_number: string
  status: MembershipApplicationStatus
  board_result: MembershipApplicationBoardResult | null
  submitted_at: string | null
  completed_at: string | null
  applicant: MembershipApplicationApplicantDetail | null
  heirs: MembershipApplicationHeirDetail[]
  references: MembershipApplicationReferenceDetail[]
  documents: MembershipApplicationDocumentDetail[]
  reviews: MembershipApplicationReviewDetail[]
  created_at: string | null
  updated_at: string | null
}

export type MembershipApplicationApiResponse = {
  success: boolean
  data: MembershipApplicationDetail
  message?: string
}

export interface UpdateMembershipApplicationPayload {
  applicant: Omit<MembershipApplicationApplicantForm, 'stock_monthly_contribution' | 'fee_monthly_contribution'> & {
    stock_monthly_contribution: number
    fee_monthly_contribution: number
  }
  heirs: MembershipApplicationHeirForm[]
  references: {
    proposer_ic_number: string
    supporter_ic_number: string
  }
}

export type MembershipApplicationUpdateResponse = MembershipApplicationApiResponse & {
  message: string
}

export type DocumentUploadType = 'ic_copy' | 'photo' | 'salary_slip' | 'employer_letter'

export type ManagementReviewDecision = 'APPROVED' | 'REJECTED'
export type BoardReviewDecision = 'PASS' | 'FAIL'

export interface MembershipApplicationReviewPayload {
  decision: ManagementReviewDecision | BoardReviewDecision
  remarks?: string | null
}

export type MembershipApplicationReviewResponse = MembershipApplicationApiResponse & {
  message: string
}

export interface BatchCompleteFailedItem {
  id: string
  application_number: string | null
  message: string
}

export type BatchCompleteResponse = {
  success: boolean
  message: string
  data: {
    succeeded: MembershipApplicationDetail[]
    failed: BatchCompleteFailedItem[]
  }
}
