import React from 'react';
import '@testing-library/jest-dom';
import { fireEvent, render, screen } from '@testing-library/react';
import { ConfirmProvider } from './confirm';
import { PromptProvider } from './prompt';
import { ToastProvider } from './toast';
import { I18nProvider } from './i18n/I18nProvider';
import JobDetailsDialog from './components/JobDetailsDialog';
import { api } from './api';

jest.setTimeout(15000);

jest.mock('./api', () => ({
  api: {
    get: jest.fn(),
    post: jest.fn(),
    put: jest.fn(),
    patch: jest.fn(),
    delete: jest.fn(),
    interceptors: { request: { use: jest.fn() }, response: { use: jest.fn() } },
  },
}));

const mockedApi = api as jest.Mocked<typeof api>;

function renderDialog() {
  return render(
    <ToastProvider>
      <I18nProvider>
        <ConfirmProvider>
          <PromptProvider>
            <JobDetailsDialog open jobId={42} onClose={() => {}} />
          </PromptProvider>
        </ConfirmProvider>
      </I18nProvider>
    </ToastProvider>,
  );
}

beforeEach(() => {
  Object.assign(navigator, {
    clipboard: {
      writeText: jest.fn().mockResolvedValue(undefined),
    },
  });

  mockedApi.get.mockImplementation((url: string) => {
    if (url === '/jobapplications/42') {
      return Promise.resolve({ data: {
        id: 42,
        jobTitle: 'Backend Developer',
        status: 'Waiting',
        dateApplied: new Date().toISOString(),
        daysSince: 10,
        company: { name: 'Acme', recruiterEmail: 'recruiter@acme.test' },
        tailoredCvText: 'Saved CV',
        coverLetterText: 'Saved cover letter',
        recruiterMessageDraft: 'Saved recruiter message',
        notes: 'Original notes\n\n<<<APPLICATION_ANSWER_DRAFT>>>\nSaved application answer\n<<<END_APPLICATION_ANSWER_DRAFT>>>',
        shortSummary: 'summary'
      } } as any);
    }
    if (url === '/auth/me') {
      return Promise.resolve({ data: { roles: [], profileCvText: 'Master CV text' } } as any);
    }
    if (url === '/jobapplications/42/history') {
      return Promise.resolve({ data: [] } as any);
    }
    if (url === '/attachments/42') {
      return Promise.resolve({ data: [{ id: 9, fileName: 'resume.pdf', uploadDate: new Date().toISOString(), fileType: 'application/pdf', fileSize: 1234, purpose: 'resume', useForAi: true }] } as any);
    }
    if (url === '/jobapplications/42/followup-draft') {
      return Promise.resolve({ data: {
        subject: 'Re: Backend Developer application update',
        body: 'Hi Maria,\n\nI wanted to follow up on the Backend Developer thread and reiterate my fit for owning the API layer.\n\nThanks,\nCasey',
        reason: 'Scheduled follow-up is due.',
        suggestedSendOn: new Date().toISOString(),
        contextSummary: 'Scheduled follow-up is due. Latest thread activity was on March 10, 2026. Saved application package material is available for reuse.',
        contextSignals: ['Saved cover letter available', 'Saved tailored CV available', 'Thread participants: Maria Recruiter <maria@acme.test>, user@example.test'],
        threadSubject: 'Backend Developer application update',
        lastCorrespondenceFrom: 'Maria Recruiter <maria@acme.test>',
        lastCorrespondenceAt: new Date().toISOString(),
      } } as any);
    }
    return Promise.resolve({ data: [] } as any);
  });
  mockedApi.post.mockResolvedValue({ data: {} } as any);
  mockedApi.put.mockResolvedValue({ data: {} } as any);
});

afterEach(() => {
  jest.clearAllMocks();
});

test('follow-up workspace keeps the draft editable and routes delivery to Job email', async () => {
  renderDialog();

  fireEvent.click(await screen.findByRole('tab', { name: /follow up/i }));

  expect(await screen.findByText(/follow-up context/i)).toBeInTheDocument();
  expect(await screen.findByText(/saved application package material is available for reuse/i)).toBeInTheDocument();
  expect(await screen.findByText(/saved cover letter available/i)).toBeInTheDocument();
  expect(await screen.findByText(/manual send boundary/i)).toBeInTheDocument();
  expect(await screen.findByText(/open job email.*connected provider.*final confirmation/i)).toBeInTheDocument();
  expect(await screen.findByDisplayValue(/i wanted to follow up on the backend developer thread/i)).toBeInTheDocument();

  const draft = screen.getByDisplayValue(/i wanted to follow up on the backend developer thread/i);
  fireEvent.change(draft, { target: { value: 'Hi Maria,\n\nEdited follow-up.\n\nThanks,\nCasey' } });

  expect(draft).toHaveValue('Hi Maria,\n\nEdited follow-up.\n\nThanks,\nCasey');
  expect(screen.getByRole('link', { name: /open job email/i })).toHaveAttribute('href', '/correspondence');
  expect(mockedApi.post).not.toHaveBeenCalledWith('/jobapplications/42/send-followup', expect.anything());
});
