import type { EditableStructureNode } from '~/composables/useStructureNodeLogic'

export interface StructureNodeCrudDeps {
  node: EditableStructureNode
  canManageSubcomponents: () => boolean
}

export function useStructureNodeCrud(props: StructureNodeCrudDeps) {
  // --- Helpers ---
  const ensureArray = (key: 'customFields' | 'pieces' | 'products' | 'subcomponents') => {
    if (!Array.isArray((props.node as any)[key])) {
      if (key === 'subcomponents') {
        props.node.subcomponents = []
      } else if (key === 'products') {
        props.node.products = []
      } else {
        (props.node as any)[key] = []
      }
    }
  }

  // --- Custom field reindex ---
  const reindexCustomFields = () => {
    if (!Array.isArray(props.node.customFields)) {
      return
    }
    props.node.customFields.forEach((field: any, index: number) => {
      if (!field || typeof field !== 'object') {
        return
      }
      field.orderIndex = index
    })
  }

  // --- Drag reorder ---
  const customFieldDrag = useDragReorder(
    () => props.node.customFields,
    { onReorder: reindexCustomFields },
  )

  const pieceDrag = useDragReorder(() => props.node.pieces)
  const productDrag = useDragReorder(() => props.node.products)
  const subcomponentDrag = useDragReorder(
    () => props.node.subcomponents,
    { draggingClass: 'ring-2 ring-primary', dropTargetClass: 'ring-2 ring-primary/70' },
  )

  // --- CRUD functions ---
  const addCustomField = () => {
    ensureArray('customFields')
    const fields = props.node.customFields!
    const nextIndex = fields.length
    fields.push({
      name: '',
      type: 'text',
      required: false,
      optionsText: '',
      options: [],
      machineContextOnly: false,
      orderIndex: nextIndex,
    })
    reindexCustomFields()
  }

  const removeCustomField = (index: number) => {
    if (!Array.isArray(props.node.customFields)) return
    props.node.customFields.splice(index, 1)
    reindexCustomFields()
  }

  const addPiece = () => {
    ensureArray('pieces')
    props.node.pieces!.push({
      typePieceId: '',
      typePieceLabel: '',
      reference: '',
      familyCode: '',
      role: '',
      quantity: 1,
    })
  }

  const removePiece = (index: number) => {
    if (!Array.isArray(props.node.pieces)) return
    props.node.pieces.splice(index, 1)
  }

  const addProduct = () => {
    ensureArray('products')
    props.node.products!.push({
      typeProductId: '',
      typeProductLabel: '',
      familyCode: '',
    })
  }

  const removeProduct = (index: number) => {
    if (!Array.isArray(props.node.products)) return
    props.node.products.splice(index, 1)
  }

  const addSubComponent = () => {
    if (!props.canManageSubcomponents()) {
      return
    }
    ensureArray('subcomponents')
    props.node.subcomponents.push({
      typeComposantId: '',
      typeComposantLabel: '',
      modelId: '',
      familyCode: '',
      alias: '',
      subcomponents: [],
    })
  }

  const removeSubComponent = (index: number) => {
    if (!Array.isArray(props.node.subcomponents)) return
    props.node.subcomponents.splice(index, 1)
  }

  return {
    // Helpers exposed for watchers
    reindexCustomFields,
    // CRUD
    addCustomField,
    removeCustomField,
    addPiece,
    removePiece,
    addProduct,
    removeProduct,
    addSubComponent,
    removeSubComponent,
    // Drag reorder — custom fields
    onCustomFieldDragStart: customFieldDrag.onDragStart,
    onCustomFieldDragEnter: customFieldDrag.onDragEnter,
    onCustomFieldDrop: customFieldDrag.onDrop,
    onCustomFieldDragEnd: customFieldDrag.onDragEnd,
    customFieldReorderClass: customFieldDrag.reorderClass,
    // Drag reorder — pieces
    onPieceDragStart: pieceDrag.onDragStart,
    onPieceDragEnter: pieceDrag.onDragEnter,
    onPieceDragOver: pieceDrag.onDragOver,
    onPieceDrop: pieceDrag.onDrop,
    onPieceDragEnd: pieceDrag.onDragEnd,
    pieceReorderClass: pieceDrag.reorderClass,
    // Drag reorder — products
    onProductDragStart: productDrag.onDragStart,
    onProductDragEnter: productDrag.onDragEnter,
    onProductDragOver: productDrag.onDragOver,
    onProductDrop: productDrag.onDrop,
    onProductDragEnd: productDrag.onDragEnd,
    productReorderClass: productDrag.reorderClass,
    // Drag reorder — subcomponents
    onSubcomponentDragStart: subcomponentDrag.onDragStart,
    onSubcomponentDragEnter: subcomponentDrag.onDragEnter,
    onSubcomponentDragOver: subcomponentDrag.onDragOver,
    onSubcomponentDrop: subcomponentDrag.onDrop,
    onSubcomponentDragEnd: subcomponentDrag.onDragEnd,
    subcomponentReorderClass: subcomponentDrag.reorderClass,
  }
}
