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.querySelectorAll('script[data-analytics]').forEach(s => s.remove())
        document.body.innerHTML = `
            <div id="cookie-banner" class="hidden">
                <button id="cookie-accept"></button>
                <button id="cookie-refuse"></button>
            </div>
        `
    })

    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()
    })

    it('loads analytics script on accept', () => {
        initCookieConsent()
        document.getElementById('cookie-accept').click()
        const script = document.querySelector('script[data-analytics]')
        expect(script).not.toBeNull()
        expect(script.src).toContain('tools-security.esy-web.dev/script.js')
        expect(script.dataset.websiteId).toBe('a1f85dd5-741f-4df7-840a-7ef0931ed0cc')
    })

    it('does not load analytics on refuse', () => {
        initCookieConsent()
        document.getElementById('cookie-refuse').click()
        const script = document.querySelector('script[data-analytics]')
        expect(script).toBeNull()
    })

    it('does not duplicate analytics script if already loaded', () => {
        document.cookie = 'e_ticket_consent=accepted;path=/'
        initCookieConsent()
        initCookieConsent()
        const scripts = document.querySelectorAll('script[data-analytics]')
        expect(scripts.length).toBe(1)
    })

    it('loads analytics immediately if already accepted', () => {
        document.cookie = 'e_ticket_consent=accepted;path=/'
        initCookieConsent()
        const script = document.querySelector('script[data-analytics]')
        expect(script).not.toBeNull()
    })

    it('does not load analytics in dev environment', () => {
        document.body.dataset.env = 'dev'
        document.cookie = 'e_ticket_consent=accepted;path=/'
        initCookieConsent()
        const script = document.querySelector('script[data-analytics]')
        expect(script).toBeNull()
    })

    it('loads cloudflare tunnel script on accept', () => {
        initCookieConsent()
        document.getElementById('cookie-accept').click()
        const script = document.querySelector('script[data-cf-beacon]')
        expect(script).not.toBeNull()
        expect(script.src).toContain('static.cloudflareinsights.com/beacon.min.js')
    })

    it('does not duplicate cloudflare script', () => {
        document.cookie = 'e_ticket_consent=accepted;path=/'
        initCookieConsent()
        initCookieConsent()
        const scripts = document.querySelectorAll('script[data-cf-beacon]')
        expect(scripts.length).toBe(1)
    })
})
