import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import FloatingHelpButton from '../FloatingHelpButton';

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

describe('FloatingHelpButton', () => {
  const renderWithRouter = (initialPath: string) => {
    return render(
      <MemoryRouter initialEntries={[initialPath]}>
        <FloatingHelpButton />
      </MemoryRouter>
    );
  };

  describe('tenant dashboard routes (prefixed with /dashboard)', () => {
    it('renders help link on tenant dashboard', () => {
      renderWithRouter('/dashboard');
      const link = screen.getByRole('link');
      expect(link).toBeInTheDocument();
    });

    it('links to /dashboard/help/dashboard for /dashboard', () => {
      renderWithRouter('/dashboard');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/dashboard/help/dashboard');
    });

    it('links to /dashboard/help/scheduler for /dashboard/scheduler', () => {
      renderWithRouter('/dashboard/scheduler');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/dashboard/help/scheduler');
    });

    it('links to /dashboard/help/services for /dashboard/services', () => {
      renderWithRouter('/dashboard/services');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/dashboard/help/services');
    });

    it('links to /dashboard/help/resources for /dashboard/resources', () => {
      renderWithRouter('/dashboard/resources');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/dashboard/help/resources');
    });

    it('links to /dashboard/help/settings/general for /dashboard/settings/general', () => {
      renderWithRouter('/dashboard/settings/general');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/dashboard/help/settings/general');
    });

    it('links to /dashboard/help/customers for /dashboard/customers/123', () => {
      renderWithRouter('/dashboard/customers/123');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/dashboard/help/customers');
    });

    it('returns null on /dashboard/help pages', () => {
      const { container } = renderWithRouter('/dashboard/help/dashboard');
      expect(container.firstChild).toBeNull();
    });

    it('links to /dashboard/help for unknown dashboard routes', () => {
      renderWithRouter('/dashboard/unknown-route');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/dashboard/help');
    });

    it('links to /dashboard/help/site-builder for /dashboard/site-editor', () => {
      renderWithRouter('/dashboard/site-editor');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/dashboard/help/site-builder');
    });

    it('links to /dashboard/help/site-builder for /dashboard/gallery', () => {
      renderWithRouter('/dashboard/gallery');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/dashboard/help/site-builder');
    });

    it('links to /dashboard/help/locations for /dashboard/locations', () => {
      renderWithRouter('/dashboard/locations');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/dashboard/help/locations');
    });

    it('links to /dashboard/help/settings/business-hours for /dashboard/settings/business-hours', () => {
      renderWithRouter('/dashboard/settings/business-hours');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/dashboard/help/settings/business-hours');
    });

    it('links to /dashboard/help/settings/email-templates for /dashboard/settings/email-templates', () => {
      renderWithRouter('/dashboard/settings/email-templates');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/dashboard/help/settings/email-templates');
    });

    it('links to /dashboard/help/settings/embed-widget for /dashboard/settings/embed-widget', () => {
      renderWithRouter('/dashboard/settings/embed-widget');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/dashboard/help/settings/embed-widget');
    });

    it('links to /dashboard/help/settings/staff-roles for /dashboard/settings/staff-roles', () => {
      renderWithRouter('/dashboard/settings/staff-roles');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/dashboard/help/settings/staff-roles');
    });

    it('links to /dashboard/help/settings/communication for /dashboard/settings/sms-calling', () => {
      renderWithRouter('/dashboard/settings/sms-calling');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/dashboard/help/settings/communication');
    });
  });

  describe('non-dashboard routes (public/platform)', () => {
    it('links to /help/scheduler for /scheduler', () => {
      renderWithRouter('/scheduler');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/help/scheduler');
    });

    it('links to /help/services for /services', () => {
      renderWithRouter('/services');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/help/services');
    });

    it('links to /help/resources for /resources', () => {
      renderWithRouter('/resources');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/help/resources');
    });

    it('links to /help/settings/general for /settings/general', () => {
      renderWithRouter('/settings/general');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/help/settings/general');
    });

    it('links to /help/locations for /locations', () => {
      renderWithRouter('/locations');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/help/locations');
    });

    it('links to /help/settings/business-hours for /settings/business-hours', () => {
      renderWithRouter('/settings/business-hours');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/help/settings/business-hours');
    });

    it('links to /help/settings/email-templates for /settings/email-templates', () => {
      renderWithRouter('/settings/email-templates');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/help/settings/email-templates');
    });

    it('links to /help/settings/embed-widget for /settings/embed-widget', () => {
      renderWithRouter('/settings/embed-widget');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/help/settings/embed-widget');
    });

    it('links to /help/settings/staff-roles for /settings/staff-roles', () => {
      renderWithRouter('/settings/staff-roles');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/help/settings/staff-roles');
    });

    it('links to /help/settings/communication for /settings/sms-calling', () => {
      renderWithRouter('/settings/sms-calling');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/help/settings/communication');
    });

    it('returns null on /help pages', () => {
      const { container } = renderWithRouter('/help/dashboard');
      expect(container.firstChild).toBeNull();
    });

    it('links to /help for unknown routes', () => {
      renderWithRouter('/unknown-route');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/help');
    });

    it('handles dynamic routes by matching prefix', () => {
      renderWithRouter('/customers/123');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('href', '/help/customers');
    });
  });

  describe('accessibility', () => {
    it('has aria-label', () => {
      renderWithRouter('/dashboard');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('aria-label', 'Help');
    });

    it('has title attribute', () => {
      renderWithRouter('/dashboard');
      const link = screen.getByRole('link');
      expect(link).toHaveAttribute('title', 'Help');
    });
  });
});
