import { describe, it, expect } from 'vitest'
import {
  componentModelStructureValidator,
  createEmptyComponentModelStructure,
  createEmptyPieceModelStructure,
  createEmptyProductModelStructure,
} from '~/shared/types/inventory'

describe('createEmptyComponentModelStructure', () => {
  it('returns a valid empty structure', () => {
    const result = createEmptyComponentModelStructure()
    expect(result).toEqual({
      customFields: [],
      pieces: [],
      products: [],
      subcomponents: [],
    })
  })
})

describe('createEmptyPieceModelStructure', () => {
  it('returns a valid empty piece structure', () => {
    const result = createEmptyPieceModelStructure()
    expect(result).toEqual({
      customFields: [],
      products: [],
    })
  })
})

describe('createEmptyProductModelStructure', () => {
  it('returns a valid empty product structure', () => {
    const result = createEmptyProductModelStructure()
    expect(result).toEqual({
      customFields: [],
    })
  })
})

describe('componentModelStructureValidator', () => {
  it('parses a minimal valid structure', () => {
    const input = {
      customFields: [],
      pieces: [],
      subcomponents: [],
    }
    const result = componentModelStructureValidator.safeParse(input)
    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data.customFields).toEqual([])
      expect(result.data.pieces).toEqual([])
      expect(result.data.subcomponents).toEqual([])
    }
  })

  it('rejects a non-object input', () => {
    const result = componentModelStructureValidator.safeParse('not an object')
    expect(result.success).toBe(false)
    if (!result.success) {
      expect(result.issues.length).toBeGreaterThan(0)
    }
  })

  it('validates custom fields with name and type', () => {
    const input = {
      customFields: [
        { name: 'Color', type: 'text', required: true },
        { name: 'Size', type: 'select', required: false, options: ['S', 'M', 'L'] },
      ],
      pieces: [],
      subcomponents: [],
    }
    const result = componentModelStructureValidator.safeParse(input)
    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data.customFields).toHaveLength(2)
      expect(result.data.customFields[0].name).toBe('Color')
      expect(result.data.customFields[1].options).toEqual(['S', 'M', 'L'])
    }
  })

  it('rejects custom fields without name', () => {
    const input = {
      customFields: [{ type: 'text', required: false }],
      pieces: [],
      subcomponents: [],
    }
    const result = componentModelStructureValidator.safeParse(input)
    expect(result.success).toBe(false)
    if (!result.success) {
      expect(result.issues.some((i) => i.includes('name'))).toBe(true)
    }
  })

  it('validates nested subcomponents', () => {
    const input = {
      customFields: [],
      pieces: [],
      subcomponents: [
        {
          typeComposantId: 'abc-123',
          alias: 'Motor',
          subcomponents: [],
        },
      ],
    }
    const result = componentModelStructureValidator.safeParse(input)
    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data.subcomponents).toHaveLength(1)
      expect(result.data.subcomponents[0].alias).toBe('Motor')
    }
  })

  it('parse throws on invalid input', () => {
    expect(() => componentModelStructureValidator.parse(null)).toThrow()
  })
})
