// src/components/Header.test.tsx
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { MemoryRouter } from 'react-router-dom';
import { Header } from './Header';
import type { User, Profile } from '../types'; // Path remains the same

const mockUser: User = { user_id: 'user-123', email: 'test@example.com' };
const mockProfile: Profile = { user_id: 'user-123', role: 'user', points: 0 };
const mockAdminProfile: Profile = { user_id: 'user-123', role: 'admin', points: 0 };

const mockOnOpenProfile = vi.fn();
const mockOnOpenVoiceAssistant = vi.fn();
const mockOnSignOut = vi.fn();

const defaultProps = {
  isDarkMode: false,
  unitSystem: 'imperial' as const,
  user: null,
  authStatus: 'SIGNED_OUT' as const,
  profile: null,
  onOpenProfile: mockOnOpenProfile,
  onOpenVoiceAssistant: mockOnOpenVoiceAssistant,
  onSignOut: mockOnSignOut,
};

// Helper to render with router context
const renderWithRouter = (props: Partial<React.ComponentProps<typeof Header>>) => {
  return render(
    <MemoryRouter>
      <Header {...defaultProps} {...props} />
    </MemoryRouter>
  );
};

describe('Header', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('should render the application title', () => {
    renderWithRouter({});
    expect(screen.getByRole('heading', { name: /flyer crawler/i })).toBeInTheDocument();
  });

  it('should display unit system and theme mode', () => {
    renderWithRouter({ isDarkMode: true, unitSystem: 'metric' });
    expect(screen.getByText(/metric/i)).toBeInTheDocument();
    expect(screen.getByText(/dark mode/i)).toBeInTheDocument();
  });

  describe('When user is logged out', () => {
    it('should show a Login button', () => {
      renderWithRouter({ user: null, authStatus: 'SIGNED_OUT' });
      expect(screen.getByRole('button', { name: /login/i })).toBeInTheDocument();
    });

    it('should call onOpenProfile when Login button is clicked', () => {
      renderWithRouter({ user: null, authStatus: 'SIGNED_OUT' });
      fireEvent.click(screen.getByRole('button', { name: /login/i }));
      expect(mockOnOpenProfile).toHaveBeenCalledTimes(1);
    });

    it('should not show user-specific buttons', () => {
      renderWithRouter({ user: null, authStatus: 'SIGNED_OUT' });
      expect(screen.queryByLabelText(/open voice assistant/i)).not.toBeInTheDocument();
      expect(screen.queryByLabelText(/open my account settings/i)).not.toBeInTheDocument();
      expect(screen.queryByRole('button', { name: /logout/i })).not.toBeInTheDocument();
    });
  });

  describe('When user is authenticated', () => {
    it('should display the user email', () => {
      renderWithRouter({ user: mockUser, authStatus: 'AUTHENTICATED' });
      expect(screen.getByText(mockUser.email)).toBeInTheDocument();
    });

    it('should display "Guest" for anonymous users', () => {
      renderWithRouter({ user: mockUser, authStatus: 'SIGNED_OUT' });
      expect(screen.getByText(/guest/i)).toBeInTheDocument();
    });

    it('should call onOpenVoiceAssistant when microphone icon is clicked', () => {
      renderWithRouter({ user: mockUser, authStatus: 'AUTHENTICATED' });
      fireEvent.click(screen.getByLabelText(/open voice assistant/i));
      expect(mockOnOpenVoiceAssistant).toHaveBeenCalledTimes(1);
    });

    it('should call onOpenProfile when cog icon is clicked', () => {
      renderWithRouter({ user: mockUser, authStatus: 'AUTHENTICATED' });
      fireEvent.click(screen.getByLabelText(/open my account settings/i));
      expect(mockOnOpenProfile).toHaveBeenCalledTimes(1);
    });

    it('should call onSignOut when Logout button is clicked', () => {
      renderWithRouter({ user: mockUser, authStatus: 'AUTHENTICATED' });
      fireEvent.click(screen.getByRole('button', { name: /logout/i }));
      expect(mockOnSignOut).toHaveBeenCalledTimes(1);
    });
  });

  describe('Admin user', () => {
    it('should show the Admin Area link for admin users', () => {
      renderWithRouter({ user: mockUser, authStatus: 'AUTHENTICATED', profile: mockAdminProfile });
      const adminLink = screen.getByTitle(/admin area/i);
      expect(adminLink).toBeInTheDocument();
      expect(adminLink.closest('a')).toHaveAttribute('href', '/admin');
    });

    it('should not show the Admin Area link for non-admin users', () => {
      renderWithRouter({ user: mockUser, authStatus: 'AUTHENTICATED', profile: mockProfile });
      expect(screen.queryByTitle(/admin area/i)).not.toBeInTheDocument();
    });
  });
});