import React from 'react';
import { TextDecoder, TextEncoder } from 'util';
import { configure } from '@testing-library/react';

Object.assign(globalThis, { TextDecoder, TextEncoder });

// Heavy MUI views (job table, workspace dialog, profile page) can exceed the
// 1s default async query timeout on slower machines; findBy*/waitFor assertions
// still resolve as soon as the element appears.
configure({ asyncUtilTimeout: 4000 });
jest.setTimeout(30000);

jest.mock('./api', () => ({
  api: {
    get: jest.fn(),
    post: jest.fn(() => Promise.resolve({ data: {} })),
    put: jest.fn(() => Promise.resolve({ data: {} })),
    patch: jest.fn(() => Promise.resolve({ data: {} })),
    delete: jest.fn(() => Promise.resolve({ data: {} })),
    interceptors: { request: { use: jest.fn() }, response: { use: jest.fn() } },
  },
  getApiErrorMessage: jest.fn((error: any, fallback?: string) => {
    const text = typeof error?.response?.data === 'string' && error.response.data.trim()
      ? error.response.data.trim()
      : typeof error?.message === 'string' && error.message.trim()
        ? error.message.trim()
        : '';
    if (!text) return fallback || 'Request failed.';
    if (/<\s*html\b|<\s*body\b|<\s*head\b|<\s*title\b|<\s*!doctype\b/i.test(text)) return fallback || 'Request failed.';
    return text.length > 300 ? `${text.slice(0, 297).trimEnd()}...` : text;
  }),
}));

jest.mock('./components/GoogleAuthCard', () => () => null);
jest.mock('./components/MicrosoftAuthCard', () => () => null);

beforeEach(() => {
  const { api } = require('./api');
  api.get.mockImplementation((url: string) => {
    if (url === '/auth/config') {
      return Promise.resolve({ data: { requireAuth: false, googleEnabled: false, microsoftEnabled: false, localEnabled: true, allowRegistration: false } });
    }
    if (url === '/auth/me') {
      return Promise.resolve({ data: { roles: [], email: 'demo@example.com', userName: 'demo' } });
    }
    if (url === '/jobapplications/reminders') {
      return Promise.resolve({ data: [] });
    }
    if (url === '/companies') {
      return Promise.resolve({ data: [] });
    }
    if (url === '/jobapplications') {
      return Promise.resolve({ data: { items: [], total: 0, page: 1, pageSize: 15 } });
    }
    if (url === '/jobapplications/stats') {
      return Promise.resolve({ data: { total: 0, active: 0, deleted: 0, byStatus: {}, appliedLast30Days: 0, averageDaysSinceApplied: 0 } });
    }
    if (url === '/jobapplications/analytics-overview') {
      return Promise.resolve({ data: { funnel: [], responseRateBySource: [], topCompanies: [], totalResponses: 0, totalActive: 0 } });
    }
    if (url === '/jobapplications/analytics' || url === '/jobapplications/tags') {
      return Promise.resolve({ data: [] });
    }
    if (url === '/jobapplications/tag-trends') {
      return Promise.resolve({ data: { months: [], series: [] } });
    }
    return Promise.resolve({ data: [] });
  });
});
