import React from 'react';
import { describe, expect, it, beforeEach, afterEach, vi } from 'vitest';
import { render, screen } from '@testing-library/react';

const consentMock = vi.fn();

vi.mock('@/contexts/consent', () => ({
  useConsent: () => consentMock(),
}));

vi.mock('react-i18next', () => ({
  useTranslation: () => ({
    t: (key: string) => key,
  }),
}));

vi.mock('@/components/ui/button', () => ({
  Button: ({ children, ...props }: { children: React.ReactNode }) => (
    <button type="button" {...props}>
      {children}
    </button>
  ),
}));

vi.mock('@/components/ui/dialog', () => ({
  Dialog: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  DialogContent: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  DialogDescription: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  DialogFooter: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  DialogHeader: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  DialogTitle: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
}));

vi.mock('@/components/ui/switch', () => ({
  Switch: () => <input type="checkbox" />,
}));

vi.mock('@/components/ui/separator', () => ({
  Separator: () => <hr />,
}));

import CookieBanner from '../CookieBanner';

describe('CookieBanner', () => {
  beforeEach(() => {
    consentMock.mockReturnValue({
      showBanner: true,
      acceptAll: vi.fn(),
      rejectAll: vi.fn(),
      preferences: { analytics: false, functional: true },
      savePreferences: vi.fn(),
      isPreferencesOpen: false,
      openPreferences: vi.fn(),
      closePreferences: vi.fn(),
    });

    window.localStorage.removeItem('fotospiel.consent.skip');
  });

  afterEach(() => {
    window.localStorage.removeItem('fotospiel.consent.skip');
  });

  it('renders the banner by default', () => {
    render(<CookieBanner />);

    expect(screen.getByText('consent.banner.title')).toBeInTheDocument();
  });

  it('hides the banner when the dev skip flag is set', () => {
    window.localStorage.setItem('fotospiel.consent.skip', '1');

    render(<CookieBanner />);

    expect(screen.queryByText('consent.banner.title')).not.toBeInTheDocument();
  });
});
