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

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

vi.mock('@/Components/ui/count-up', () => ({
  CountUp: ({ end }: { end: number }) => <span>{end ?? 0}</span>,
}));

vi.mock('@inertiajs/react', async () => {
  const actual = await vi.importActual('@inertiajs/react');
  return {
    ...actual,
    router: {
      post: vi.fn(),
      delete: vi.fn(),
      get: vi.fn(),
      on: vi.fn(() => vi.fn()),
    },
    Head: ({ title }: { title: string }) => <title>{title}</title>,
    Link: ({ children, href }: { children: React.ReactNode; href: string }) => (
      <a href={href}>{children}</a>
    ),
    usePage: vi.fn(() => ({
      url: '/admin/testimonials',
      props: {
        auth: { user: { id: 1, name: 'Admin', email: 'admin@test.com', is_admin: true }, impersonating: false },
        features: { billing: false, socialAuth: false, emailVerification: false, apiTokens: false, userSettings: false, notifications: false, onboarding: false, apiDocs: false, twoFactor: false, webhooks: false, admin: true },
        flash: {}, errors: {}, notifications_unread_count: 0, sites: [], limits: null, active_jobs: [], ai_max_batch_size: 10, ai_defaults: {}, polling_interval_ms: 5000, admin_failed_job_count: 0,
      },
    })),
    useForm: vi.fn(() => ({
      data: { file: null, preview_only: false },
      setData: vi.fn(),
      post: vi.fn(),
      reset: vi.fn(),
      clearErrors: vi.fn(),
      errors: {},
      processing: false,
    })),
  };
});

import { router } from '@inertiajs/react';

import TestimonialsIndex from './Index';

const makeTestimonial = (overrides = {}) => ({
  id: 1,
  name: 'Bob Smith',
  role: 'CEO',
  company: 'Acme Corp',
  avatar_url: null,
  content: 'Great product!',
  quote: 'Great product!',
  source: 'direct' as const,
  featured: false,
  rating: 5,
  status: 'pending' as const,
  approved_at: null,
  created_at: '2026-06-01T00:00:00Z',
  updated_at: '2026-06-01T00:00:00Z',
  ...overrides,
});

const makeProps = (overrides = {}) => ({
  testimonials: {
    data: [makeTestimonial()],
    current_page: 1,
    last_page: 1,
    per_page: 15,
    total: 1,
    from: 1,
    to: 1,
    links: [],
    path: '/admin/testimonials',
    first_page_url: '',
    last_page_url: '',
    next_page_url: null,
    prev_page_url: null,
  },
  filters: {},
  stats: { total: 1, pending: 1, approved: 0, rejected: 0, featured: 0, avg_rating: 4.5, average_rating: 5 },
  sources: [],
  ...overrides,
});

describe('Testimonials/Index Reject confirmation', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('does not fire router.post immediately on Reject click — opens dialog first', async () => {
    const user = userEvent.setup();
    render(<TestimonialsIndex {...makeProps()} />);
    await user.click(screen.getByRole('button', { name: /reject testimonial from bob smith/i }));
    expect(router.post).not.toHaveBeenCalled();
    expect(screen.getByRole('alertdialog')).toBeInTheDocument();
    expect(screen.getByText(/reject the testimonial from bob smith/i)).toBeInTheDocument();
  });

  it('fires router.post when confirm is clicked', async () => {
    const user = userEvent.setup();
    render(<TestimonialsIndex {...makeProps()} />);
    await user.click(screen.getByRole('button', { name: /reject testimonial from bob smith/i }));
    await user.click(screen.getByRole('button', { name: /^reject$/i }));
    expect(router.post).toHaveBeenCalledTimes(1);
    expect(router.post).toHaveBeenCalledWith(
      expect.stringContaining('/admin/testimonials'),
      {},
      expect.any(Object),
    );
  });

  it('does not fire router.post when cancel is clicked', async () => {
    const user = userEvent.setup();
    render(<TestimonialsIndex {...makeProps()} />);
    await user.click(screen.getByRole('button', { name: /reject testimonial from bob smith/i }));
    await user.click(screen.getByRole('button', { name: /cancel/i }));
    expect(router.post).not.toHaveBeenCalled();
  });
});
