import { describe, it, expect, beforeEach } from 'vitest'
import { initCookieConsent } from '../../assets/modules/cookie-consent.js'

describe('initCookieConsent', () => {
    beforeEach(() => {
        document.cookie = 'e_ticket_consent=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/'
        document.body.innerHTML = `
            <div id="cookie-banner" class="hidden">
                <button id="cookie-accept"></button>
                <button id="cookie-refuse"></button>
            </div>
        `
        delete document.body.dataset.env
    })

    it('shows banner when no consent cookie', () => {
        initCookieConsent()
        const banner = document.getElementById('cookie-banner')
        expect(banner.classList.contains('hidden')).toBe(false)
    })

    it('hides banner and sets cookie on accept', () => {
        initCookieConsent()
        document.getElementById('cookie-accept').click()
        const banner = document.getElementById('cookie-banner')
        expect(banner.classList.contains('hidden')).toBe(true)
        expect(document.cookie).toContain('e_ticket_consent=accepted')
    })

    it('hides banner and sets cookie on refuse', () => {
        initCookieConsent()
        document.getElementById('cookie-refuse').click()
        const banner = document.getElementById('cookie-banner')
        expect(banner.classList.contains('hidden')).toBe(true)
        expect(document.cookie).toContain('e_ticket_consent=refused')
    })

    it('does not show banner if already accepted', () => {
        document.cookie = 'e_ticket_consent=accepted;path=/'
        initCookieConsent()
        const banner = document.getElementById('cookie-banner')
        expect(banner.classList.contains('hidden')).toBe(true)
    })

    it('does not show banner if already refused', () => {
        document.cookie = 'e_ticket_consent=refused;path=/'
        initCookieConsent()
        const banner = document.getElementById('cookie-banner')
        expect(banner.classList.contains('hidden')).toBe(true)
    })

    it('handles banner without buttons', () => {
        document.body.innerHTML = '<div id="cookie-banner" class="hidden"></div>'
        initCookieConsent()
        const banner = document.getElementById('cookie-banner')
        expect(banner.classList.contains('hidden')).toBe(false)
    })

    it('does nothing without banner element', () => {
        document.body.innerHTML = ''
        expect(() => initCookieConsent()).not.toThrow()
    })
})
