import { describe, it, expect } from 'vitest'
import {
    buildProviderAccountingPayload,
    buildProviderRibPayload,
    isBankRequiredForPaymentType,
    isRibBlank,
    isRibComplete,
    isRibRequiredForPaymentType,
} from '../providerAccounting'
import { emptyProviderAccounting, emptyProviderRib } from '~/modules/technique/types/providerForm'

/**
 * Helpers purs de l'onglet Comptabilite prestataire (ERP-144) : RG inter-champs
 * RG-3.07 (banque si VIREMENT) / RG-3.08 (RIB si LCR) + construction des payloads.
 */
describe('providerAccounting helpers', () => {
    describe('RG-3.07 / RG-3.08 — type de reglement', () => {
        it('banque requise uniquement pour VIREMENT', () => {
            expect(isBankRequiredForPaymentType('VIREMENT')).toBe(true)
            expect(isBankRequiredForPaymentType('LCR')).toBe(false)
            expect(isBankRequiredForPaymentType('CHEQUE')).toBe(false)
            expect(isBankRequiredForPaymentType(null)).toBe(false)
        })

        it('RIB requis uniquement pour LCR', () => {
            expect(isRibRequiredForPaymentType('LCR')).toBe(true)
            expect(isRibRequiredForPaymentType('VIREMENT')).toBe(false)
            expect(isRibRequiredForPaymentType(null)).toBe(false)
        })
    })

    describe('isRibBlank / isRibComplete', () => {
        it('un RIB vierge est vide et incomplet', () => {
            expect(isRibBlank(emptyProviderRib())).toBe(true)
            expect(isRibComplete(emptyProviderRib())).toBe(false)
        })

        it('un RIB partiel n\'est ni vide ni complet', () => {
            const rib = { ...emptyProviderRib(), iban: 'FR76...' }
            expect(isRibBlank(rib)).toBe(false)
            expect(isRibComplete(rib)).toBe(false)
        })

        it('un RIB avec libelle + BIC + IBAN est complet', () => {
            const rib = { ...emptyProviderRib(), label: 'Compte', bic: 'BNPAFRPP', iban: 'FR76...' }
            expect(isRibComplete(rib)).toBe(true)
        })
    })

    describe('buildProviderAccountingPayload (RG-3.07)', () => {
        it('envoie la banque si requise (VIREMENT)', () => {
            const payload = buildProviderAccountingPayload({
                ...emptyProviderAccounting(),
                paymentTypeIri: '/api/payment_types/3',
                bankIri: '/api/banks/2',
            }, true)
            expect(payload.bank).toBe('/api/banks/2')
            expect(payload.paymentType).toBe('/api/payment_types/3')
        })

        it('force la banque a null si non requise (hors VIREMENT)', () => {
            const payload = buildProviderAccountingPayload({
                ...emptyProviderAccounting(),
                bankIri: '/api/banks/2',
            }, false)
            expect(payload.bank).toBeNull()
        })
    })

    describe('buildProviderRibPayload', () => {
        it('omet les champs requis vides (NotBlank back joue sur le champ)', () => {
            const payload = buildProviderRibPayload(emptyProviderRib())
            expect(payload).not.toHaveProperty('label')
            expect(payload).not.toHaveProperty('bic')
            expect(payload).not.toHaveProperty('iban')
        })

        it('conserve les champs remplis', () => {
            const payload = buildProviderRibPayload({ ...emptyProviderRib(), label: 'Compte', bic: 'BNPAFRPP', iban: 'FR76...' })
            expect(payload).toEqual({ label: 'Compte', bic: 'BNPAFRPP', iban: 'FR76...' })
        })
    })
})
