import { create } from 'zustand'
import type { Attachment } from '../domain/inquiry'
import type { ReportObjectFieldSelection } from '../domain/inquiryReport'

export type OfferStep = 'select_properties' | 'select_fields' | 'pdf_review' | 'checked' | 'send'

interface OfferWizardState {
  isOpen: boolean
  selectedNeedId: string | null
  needTitle: string
  selectedPropertyIds: string[]
  currentStep: OfferStep
  offerDraftId: string | null
  editableFields: Record<string, string>
  fieldSelections: ReportObjectFieldSelection[]
  pdfPreviewReady: boolean
  messageDraft: string
  messageSubject: string
  attachments: Attachment[]
  open(needId: string, needTitle: string): void
  close(): void
  toggleProperty(id: string): void
  setSelectedProperties(ids: string[]): void
  setStep(step: OfferStep): void
  setOfferDraftId(id: string): void
  updateField(fieldId: string, value: string): void
  setFieldSelections(selections: ReportObjectFieldSelection[]): void
  updateFieldSelection(propertyId: string, selection: ReportObjectFieldSelection): void
  setPdfReady(): void
  setMessageDraft(text: string): void
  setMessageSubject(s: string): void
  addAttachment(a: Attachment): void
  removeAttachment(id: string): void
  reset(): void
}

export const useOfferWizardStore = create<OfferWizardState>((set) => ({
  isOpen: false,
  selectedNeedId: null,
  needTitle: '',
  selectedPropertyIds: [],
  currentStep: 'select_properties',
  offerDraftId: null,
  editableFields: {},
  fieldSelections: [],
  pdfPreviewReady: false,
  messageDraft: '',
  messageSubject: '',
  attachments: [],
  open: (needId, needTitle) =>
    set((s) => ({
      isOpen: true,
      selectedNeedId: needId,
      needTitle,
      selectedPropertyIds: s.selectedPropertyIds,
      currentStep: 'select_properties',
      offerDraftId: null,
      editableFields: {},
      fieldSelections: [],
      pdfPreviewReady: false,
      messageDraft: '',
      messageSubject: '',
      attachments: [],
    })),
  close: () => set({ isOpen: false }),
  toggleProperty: (id) =>
    set((s) => ({
      selectedPropertyIds: s.selectedPropertyIds.includes(id)
        ? s.selectedPropertyIds.filter((p) => p !== id)
        : [...s.selectedPropertyIds, id],
    })),
  setSelectedProperties: (ids) => set({ selectedPropertyIds: ids }),
  setStep: (currentStep) => set({ currentStep }),
  setOfferDraftId: (id) => set({ offerDraftId: id }),
  updateField: (fieldId, value) =>
    set((s) => ({ editableFields: { ...s.editableFields, [fieldId]: value } })),
  setFieldSelections: (fieldSelections) => set({ fieldSelections }),
  updateFieldSelection: (propertyId, selection) =>
    set((s) => ({
      fieldSelections: s.fieldSelections.map(fs => fs.propertyId === propertyId ? selection : fs),
    })),
  setPdfReady: () => set({ pdfPreviewReady: true }),
  setMessageDraft: (messageDraft) => set({ messageDraft }),
  setMessageSubject: (messageSubject) => set({ messageSubject }),
  addAttachment: (a) => set((s) => ({ attachments: [...s.attachments, a] })),
  removeAttachment: (id) =>
    set((s) => ({ attachments: s.attachments.filter((a) => a.id !== id) })),
  reset: () =>
    set({
      isOpen: false,
      selectedNeedId: null,
      needTitle: '',
      selectedPropertyIds: [],
      currentStep: 'select_properties',
      offerDraftId: null,
      editableFields: {},
      fieldSelections: [],
      pdfPreviewReady: false,
      messageDraft: '',
      messageSubject: '',
      attachments: [],
    }),
}))
