/**
 * IntegrationStatusCard — GA4-CONNECT-REACHABLE tests
 *
 * Verifies:
 * 1. GA4 row is rendered regardless of ai_visibility_panel flag
 * 2. GA4 row shows Connect link when has_ga4=false
 * 3. GA4 row shows Manage link when has_ga4=true
 * 4. allDone condition includes GA4
 */
import { render, screen } from '@testing-library/react';
import { describe, it, expect, beforeEach, vi } from 'vitest';

import IntegrationStatusCard from './IntegrationStatusCard';

vi.mock('@inertiajs/react', () => ({
  Link: ({ href, children, ...rest }: { href: string; children: React.ReactNode; [key: string]: unknown }) => (
    <a href={href} {...rest}>{children}</a>
  ),
}));

const BASE_PROPS = {
  aiAvailable: true,
  hasSerpKey: true,
  hasWp: true,
  siteId: 'ulid-public-id-123',
};

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

  describe('GA4 row (GA4-CONNECT-REACHABLE)', () => {
    it('renders a GA4 entry when has_ga4 is false (not connected)', () => {
      render(<IntegrationStatusCard {...BASE_PROPS} hasGa4={false} />);

      expect(screen.getByText(/google analytics 4/i)).toBeInTheDocument();
    });

    it('renders a GA4 entry when has_ga4 is true (connected) and another integration is pending', () => {
      // GA4 connected but AI key not yet set up → card still renders showing both
      render(<IntegrationStatusCard {...BASE_PROPS} aiAvailable={false} hasGa4={true} />);

      expect(screen.getByText(/google analytics 4/i)).toBeInTheDocument();
    });

    it('shows a Connect GA4 link when hasGa4=false', () => {
      render(<IntegrationStatusCard {...BASE_PROPS} aiAvailable={false} hasGa4={false} />);

      const link = screen.getByTestId('ga4-integration-link');
      expect(link).toBeInTheDocument();
      expect(link).toHaveAttribute('href', '/sites/ulid-public-id-123/settings/ga4');
      expect(link).toHaveTextContent(/connect/i);
    });

    it('shows a Manage GA4 link when hasGa4=true', () => {
      render(<IntegrationStatusCard {...BASE_PROPS} aiAvailable={false} hasGa4={true} />);

      const link = screen.getByTestId('ga4-integration-link');
      expect(link).toBeInTheDocument();
      expect(link).toHaveAttribute('href', '/sites/ulid-public-id-123/settings/ga4');
      expect(link).toHaveTextContent(/manage/i);
    });

    it('does NOT gate GA4 row on any feature flag — renders unconditionally', () => {
      // Even when all other integrations are done, GA4 still renders
      render(<IntegrationStatusCard {...BASE_PROPS} hasGa4={false} />);

      expect(screen.getByText(/google analytics 4/i)).toBeInTheDocument();
    });

    it('returns null only when all integrations including GA4 are done', () => {
      const { container } = render(<IntegrationStatusCard {...BASE_PROPS} hasGa4={true} />);

      // All 4 done → null render (card hidden)
      expect(container.firstChild).toBeNull();
    });

    it('stays visible when GA4 is the only incomplete integration', () => {
      render(<IntegrationStatusCard {...BASE_PROPS} hasGa4={false} />);

      // Card visible because GA4 not connected
      expect(screen.getByText(/google analytics 4/i)).toBeInTheDocument();
    });
  });
});
