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

vi.mock('@inertiajs/react', () => ({
  Link: ({ children, href, 'aria-label': ariaLabel }: { children: React.ReactNode; href: string; 'aria-label'?: string }) => (
    <a href={href} aria-label={ariaLabel}>{children}</a>
  ),
}));

vi.stubGlobal('route', vi.fn(() => '/mock-route'));

import { DailyActionHero } from './DailyActionHero';

const baseSite = { id: '1' };

// ─── UI-003: DailyActionHero always renders one actionable CTA ───────────────

describe('DailyActionHero — UI-003', () => {
  it('shows "Review N recommendations" CTA when pending_recommendations > 0', () => {
    render(
      <DailyActionHero
        siteId={baseSite.id}
        pendingRecommendations={5}
        keywordOpportunities={0}
        hasRecentAnalysis
      />,
    );
    // UX-215: sr-only span in stretched Link + visible span both contain the text
    expect(screen.getAllByText(/review 5 recommendations/i).length).toBeGreaterThan(0);
    const cta = screen.getByRole('link', { name: /review 5 recommendations/i });
    expect(cta).toBeInTheDocument();
    expect(cta.getAttribute('href')).toBeTruthy();
  });

  it('shows "N keywords one step from page 1" CTA when keyword_opportunities > 0 and no pending recs (UX-220)', () => {
    render(
      <DailyActionHero
        siteId={baseSite.id}
        pendingRecommendations={0}
        keywordOpportunities={3}
        hasRecentAnalysis
      />,
    );
    // UX-220: plain language instead of "striking-distance keywords ready"
    // UX-215: sr-only span in Link + visible span both carry the text
    expect(screen.getAllByText(/3 keywords one step from page 1/i).length).toBeGreaterThan(0);
    const cta = screen.getByRole('link', { name: /3 keywords one step from page 1/i });
    expect(cta).toBeInTheDocument();
  });

  it('shows "Run analysis" CTA when no pending recs, no opportunities, and no recent analysis', () => {
    render(
      <DailyActionHero
        siteId={baseSite.id}
        pendingRecommendations={0}
        keywordOpportunities={0}
        hasRecentAnalysis={false}
      />,
    );
    // UX-215: sr-only span + visible span both carry the text
    expect(screen.getAllByText(/run analysis/i).length).toBeGreaterThan(0);
    const cta = screen.getByRole('link', { name: /run analysis/i });
    expect(cta).toBeInTheDocument();
  });

  it('always renders exactly one primary CTA card', () => {
    render(
      <DailyActionHero
        siteId={baseSite.id}
        pendingRecommendations={5}
        keywordOpportunities={3}
        hasRecentAnalysis
      />,
    );
    // Only the highest priority action renders — pending_recs takes priority
    const links = screen.getAllByRole('link');
    // Should have at least one CTA link
    expect(links.length).toBeGreaterThanOrEqual(1);
    // Pending recs CTA takes priority (sr-only + visible span both carry the text)
    expect(screen.getAllByText(/review 5 recommendations/i).length).toBeGreaterThan(0);
    // No "keywords one step from page 1" shown because pending recs wins
    expect(screen.queryByText(/keywords one step from page 1/i)).not.toBeInTheDocument();
  });

  it('renders "Run analysis" CTA when all three signals are zero but analysis was recent', () => {
    // When no pending and no opps but analysis was recent, we still show the analysis CTA
    // to encourage regular use
    render(
      <DailyActionHero
        siteId={baseSite.id}
        pendingRecommendations={0}
        keywordOpportunities={0}
        hasRecentAnalysis
      />,
    );
    // Falls through to "run analysis" as the deterministic default (sr-only + visible)
    expect(screen.getAllByText(/run analysis/i).length).toBeGreaterThan(0);
  });
});

// ─── UI-003: Dashboard DailyActionHero integration test ─────────────────────
// These are tested in Dashboard.test.tsx (the integration layer)
