import React from 'react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, waitFor } from '@testing-library/react';
import PhotoPostWizardContainer from '@/features/photopost/containers/PhotoPostWizardContainer';

// Mocks for services
const generateSocialMediaPostsMock = vi.fn();
const publishSocialMediaPostMock = vi.fn();
const uploadEventFileMock = vi.fn();
const speechToTextMock = vi.fn();
const getEventMediaMock = vi.fn();

vi.mock('@/features/photopost/api/socialPostService', () => ({
  generateSocialMediaPosts: (...args: any[]) => generateSocialMediaPostsMock(...args),
  publishSocialMediaPost: (...args: any[]) => publishSocialMediaPostMock(...args),
}));
vi.mock('@/shared/api/fileService', () => ({
  uploadEventFile: (...args: any[]) => uploadEventFileMock(...args),
}));
vi.mock('@/shared/api/toolService', () => ({
  speechToText: (...args: any[]) => speechToTextMock(...args),
}));
vi.mock('@/features/event/api/getEventMedia', () => ({
  getEventMedia: (...args: any[]) => getEventMediaMock(...args),
}));

// Mock presenter to trigger container callbacks
vi.mock('@/features/photopost/components/PhotoPostWizard', () => ({
  default: (props: any) => (
    <div>
      <button
        onClick={() => {
          const p = props.onGenerateSocialPosts?.({ eventId: 'e1', images: [], description: 'd' });
          if (p && typeof p.catch === 'function') p.catch(() => {});
        }}
      >
        trigger-generate
      </button>
      <button
        onClick={() => {
          const p = props.onPublishSocialPost?.({ postId: 'p1', posts: {}, images: [], scheduledTime: new Date().toISOString() });
          if (p && typeof p.catch === 'function') p.catch(() => {});
        }}
      >
        trigger-publish
      </button>
    </div>
  ),
}));

// Hoist-safe sonner mock and import
vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } }));
import { toast } from 'sonner';

beforeEach(() => {
  vi.restoreAllMocks();
  generateSocialMediaPostsMock.mockReset();
  publishSocialMediaPostMock.mockReset();
  uploadEventFileMock.mockReset();
  speechToTextMock.mockReset();
  getEventMediaMock.mockReset();
  (toast.success as any).mockReset?.();
  (toast.error as any).mockReset?.();
});

describe('PhotoPostWizardContainer (integration)', () => {
  it('onPublish success shows success toast', async () => {
    publishSocialMediaPostMock.mockResolvedValueOnce({ status: 'sucess' });

    render(<PhotoPostWizardContainer eventId={'e1'} onComplete={() => {}} />);

    await screen.findByText('trigger-publish');
    screen.getByText('trigger-publish').click();

    expect(publishSocialMediaPostMock).toHaveBeenCalledTimes(1);
    await waitFor(() => expect((toast.success as any)).toHaveBeenCalledTimes(1));
  });

  it('onPublish error shows error toast', async () => {
    publishSocialMediaPostMock.mockRejectedValueOnce(new Error('fail'));

    render(<PhotoPostWizardContainer eventId={'e1'} onComplete={() => {}} />);

    await screen.findByText('trigger-publish');
    screen.getByText('trigger-publish').click();

    expect(publishSocialMediaPostMock).toHaveBeenCalledTimes(1);
    await waitFor(() => expect((toast.error as any)).toHaveBeenCalledTimes(1));
  });

  it('onGenerate error shows error toast', async () => {
    generateSocialMediaPostsMock.mockRejectedValueOnce(new Error('fail'));

    render(<PhotoPostWizardContainer eventId={'e1'} onComplete={() => {}} />);

    await screen.findByText('trigger-generate');
    screen.getByText('trigger-generate').click();

    expect(generateSocialMediaPostsMock).toHaveBeenCalledTimes(1);
    await waitFor(() => expect((toast.error as any)).toHaveBeenCalledTimes(1));
  });
});
