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

vi.mock('@inertiajs/react', async () => {
  const actual = await vi.importActual('@inertiajs/react');
  return {
    ...actual,
    usePage: vi.fn(() => ({
      props: {
        auth: { user: { name: 'Test User', email: 'test@example.com' } },
        features: { billing: false, notifications: false },
        sites: [],
        limits: null,
        ai_defaults: { model: 'gpt-4o-mini', temperature: 0.7 },
        polling_interval_ms: 5000,
      },
    })),
    Head: ({ title }: { title: string }) => <title>{title}</title>,
    Link: ({ children, href }: { children: React.ReactNode; href: string }) => (
      <a href={href}>{children}</a>
    ),
    router: { get: vi.fn(), post: vi.fn(), visit: vi.fn(), on: vi.fn(() => vi.fn()) },
  };
});

vi.mock('@/Components/theme/use-theme', () => ({
  useTheme: vi.fn(() => ({
    theme: 'system',
    setTheme: vi.fn(),
    resolvedTheme: 'light',
  })),
}));

vi.mock('@/Components/Navigation/SiteNav', () => ({
  default: () => <div data-testid="site-nav" />,
}));

vi.mock('@/Layouts/DashboardLayout', () => ({
  default: ({ children }: { children: React.ReactNode }) => (
    <div data-testid="dashboard-layout">{children}</div>
  ),
}));

import Index from './Index';

function createProps(overrides: Record<string, unknown> = {}) {
  return {
    site: { id: 1, name: 'Test Site', domain: 'example.com' },
    briefs: [] as Array<{
      id: number;
      target_keyword: string;
      source_type: string;
      source_id: number;
      status: string;
      generated_at: string | null;
      created_at: string;
      suggested_publish_date: string | null;
    }>,
    filters: { status: null, source_type: null, sort: null },
    counts: {
      total: 0,
      pending: 0,
      generating: 0,
      completed: 0,
      failed: 0,
      TopicCluster: 0,
      TopicGapSuggestion: 0,
      FreshnessRecommendation: 0,
    },
    pagination: {
      current_page: 1,
      last_page: 1,
      per_page: 25,
      total: 0,
      links: [],
    },
    ...overrides,
  };
}

describe('ContentBriefs/Index', () => {
  it('renders empty state when no briefs exist', () => {
    render(<Index {...createProps()} />);
    expect(screen.getByText('Start a new content brief')).toBeInTheDocument();
  });

  it('renders brief cards with target keyword', () => {
    const props = createProps({
      briefs: [
        {
          id: 1,
          target_keyword: 'summer vacation tips',
          source_type: 'TopicCluster',
          source_id: 10,
          status: 'completed',
          generated_at: '2026-02-15T00:00:00.000Z',
          created_at: '2026-02-10T00:00:00.000Z',
          suggested_publish_date: null,
        },
      ],
      counts: {
        total: 1,
        pending: 0,
        generating: 0,
        completed: 1,
        failed: 0,
        TopicCluster: 1,
        TopicGapSuggestion: 0,
        FreshnessRecommendation: 0,
      },
      pagination: { current_page: 1, last_page: 1, per_page: 25, total: 1, links: [] },
    });

    render(<Index {...props} />);
    expect(screen.getByText('summer vacation tips')).toBeInTheDocument();
  });

  it('shows seasonal publish suggestion badge when suggested_publish_date is present', () => {
    const props = createProps({
      briefs: [
        {
          id: 1,
          target_keyword: 'holiday shopping deals',
          source_type: 'App\\Models\\KeywordOpportunity',
          source_id: 5,
          status: 'completed',
          generated_at: '2026-02-15T00:00:00.000Z',
          created_at: '2026-02-10T00:00:00.000Z',
          suggested_publish_date: '2026-11-02T00:00:00.000Z',
        },
      ],
      counts: {
        total: 1,
        pending: 0,
        generating: 0,
        completed: 1,
        failed: 0,
        TopicCluster: 0,
        TopicGapSuggestion: 0,
        FreshnessRecommendation: 0,
      },
      pagination: { current_page: 1, last_page: 1, per_page: 25, total: 1, links: [] },
    });

    render(<Index {...props} />);
    expect(screen.getByText(/Best time to publish:/)).toBeInTheDocument();
  });

  it('does not show seasonal badge when suggested_publish_date is null', () => {
    const props = createProps({
      briefs: [
        {
          id: 1,
          target_keyword: 'flat keyword',
          source_type: 'TopicCluster',
          source_id: 10,
          status: 'pending',
          generated_at: null,
          created_at: '2026-02-10T00:00:00.000Z',
          suggested_publish_date: null,
        },
      ],
      counts: {
        total: 1,
        pending: 1,
        generating: 0,
        completed: 0,
        failed: 0,
        TopicCluster: 1,
        TopicGapSuggestion: 0,
        FreshnessRecommendation: 0,
      },
      pagination: { current_page: 1, last_page: 1, per_page: 25, total: 1, links: [] },
    });

    render(<Index {...props} />);
    expect(screen.queryByText(/Best time to publish:/)).not.toBeInTheDocument();
  });
});
