import { describe, it, expect } from 'vitest'
import {
    buildProviderAddressPayload,
    isProviderAddressValid,
} from '../providerAddress'
import { emptyProviderAddress } from '~/modules/technique/types/providerForm'

/**
 * Helpers purs de l'onglet Adresse prestataire (ERP-143). RG-3.05 (>= 1 site) et
 * construction du payload de sous-ressource (relations en IRI, requis vides omis,
 * pas de type d'adresse / bennes / triage — difference M2).
 */
describe('providerAddress helpers', () => {
    const SITE = '/api/sites/1'
    const CAT = '/api/categories/7'

    describe('isProviderAddressValid (RG-3.05 / RG-3.09)', () => {
        it('false sans site', () => {
            const address = { ...emptyProviderAddress(), categoryIris: [CAT] }
            expect(isProviderAddressValid(address)).toBe(false)
        })

        it('false sans categorie', () => {
            const address = { ...emptyProviderAddress(), siteIris: [SITE] }
            expect(isProviderAddressValid(address)).toBe(false)
        })

        it('true avec au moins un site ET une categorie', () => {
            const address = { ...emptyProviderAddress(), siteIris: [SITE], categoryIris: [CAT] }
            expect(isProviderAddressValid(address)).toBe(true)
        })
    })

    describe('buildProviderAddressPayload', () => {
        it('mappe les relations en IRI et n\'embarque PAS type/bennes/triage (difference M2)', () => {
            const payload = buildProviderAddressPayload({
                ...emptyProviderAddress(),
                postalCode: '86100',
                city: 'Châtellerault',
                street: '1 rue du Test',
                siteIris: [SITE],
                categoryIris: [CAT],
                contactIris: ['/api/provider_contacts/9'],
            })
            expect(payload).toEqual({
                country: 'France',
                postalCode: '86100',
                city: 'Châtellerault',
                street: '1 rue du Test',
                streetComplement: null,
                categories: [CAT],
                sites: [SITE],
                contacts: ['/api/provider_contacts/9'],
            })
            expect(payload).not.toHaveProperty('addressType')
            expect(payload).not.toHaveProperty('bennes')
            expect(payload).not.toHaveProperty('triageProvider')
        })

        it('omet les scalaires requis vides (NotBlank back joue sur le champ)', () => {
            const payload = buildProviderAddressPayload({
                ...emptyProviderAddress(),
                siteIris: [SITE],
                categoryIris: [CAT],
            })
            expect(payload).not.toHaveProperty('postalCode')
            expect(payload).not.toHaveProperty('city')
            expect(payload).not.toHaveProperty('street')
            // streetComplement n'est PAS requis -> reste present a null.
            expect(payload).toHaveProperty('streetComplement', null)
        })
    })
})
