import { useApi } from '~/composables/useApi'
import type { ConstructeurLinkEntry } from '~/shared/constructeurUtils'
import { extractCollection } from '~/shared/utils/apiHelpers'

type EntityType = 'machine' | 'piece' | 'composant' | 'product'

const ENDPOINTS: Record<EntityType, string> = {
  machine: '/machine_constructeur_links',
  piece: '/piece_constructeur_links',
  composant: '/composant_constructeur_links',
  product: '/product_constructeur_links',
}

const ENTITY_KEYS: Record<EntityType, string> = {
  machine: 'machine',
  piece: 'piece',
  composant: 'composant',
  product: 'product',
}

const ENTITY_PLURALS: Record<EntityType, string> = {
  machine: 'machines',
  piece: 'pieces',
  composant: 'composants',
  product: 'products',
}

export function useConstructeurLinks() {
  const { get, post, patch, delete: del } = useApi()

  const fetchLinks = async (
    entityType: EntityType,
    entityId: string,
  ): Promise<ConstructeurLinkEntry[]> => {
    const endpoint = ENDPOINTS[entityType]
    const key = ENTITY_KEYS[entityType]
    const plural = ENTITY_PLURALS[entityType]
    const url = `${endpoint}?${key}=/api/${plural}/${entityId}`
    const result = await get(url)
    if (!result.success || !result.data) return []

    const members = extractCollection(result.data)
    if (!Array.isArray(members)) return []

    return members.map((link: any) => ({
      linkId: link.id ?? (typeof link['@id'] === 'string' ? link['@id'].split('/').pop() : undefined),
      constructeurId: typeof link.constructeur === 'string'
        ? link.constructeur.split('/').pop()!
        : link.constructeur?.id ?? '',
      constructeur: typeof link.constructeur === 'object' ? link.constructeur : null,
      supplierReference: link.supplierReference ?? null,
    }))
  }

  const syncLinks = async (
    entityType: EntityType,
    entityId: string,
    originalLinks: ConstructeurLinkEntry[],
    formLinks: ConstructeurLinkEntry[],
  ): Promise<void> => {
    const endpoint = ENDPOINTS[entityType]
    const key = ENTITY_KEYS[entityType]
    const plural = ENTITY_PLURALS[entityType]
    const entityIri = `/api/${plural}/${entityId}`

    const originalMap = new Map(originalLinks.map(l => [l.constructeurId, l]))
    const formMap = new Map(formLinks.map(l => [l.constructeurId, l]))

    const promises: Promise<any>[] = []

    // Delete removed links
    for (const [cId, orig] of originalMap) {
      if (!formMap.has(cId) && orig.linkId) {
        promises.push(del(`${endpoint}/${orig.linkId}`))
      }
    }

    // Create new links
    for (const [cId, form] of formMap) {
      if (!originalMap.has(cId)) {
        promises.push(post(endpoint, {
          [key]: entityIri,
          constructeur: `/api/constructeurs/${cId}`,
          supplierReference: form.supplierReference || null,
        }))
      }
    }

    // Patch modified supplierReference
    for (const [cId, form] of formMap) {
      const orig = originalMap.get(cId)
      if (orig?.linkId && (orig.supplierReference ?? null) !== (form.supplierReference ?? null)) {
        promises.push(patch(`${endpoint}/${orig.linkId}`, {
          supplierReference: form.supplierReference || null,
        }))
      }
    }

    await Promise.allSettled(promises)
  }

  return { fetchLinks, syncLinks }
}
