import React from 'react';
import { describe, it, expect, vi } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { SettingsPluginsPage } from '@/pages/SettingsPlugins';

const mockPlugins = [
  { name: 'mail-plugin', display_name: 'Mail Plugin', description: 'Mail integration', version: '1.0.0', status: 'active', installed: true, active: true },
  { name: 'export-plugin', display_name: 'Export Plugin', description: 'Export functionality', version: '2.1.0', status: 'inactive', installed: true, active: false },
  { name: 'beta-plugin', display_name: 'Beta Plugin', description: 'Beta feature', version: '0.1.0', status: 'discovered', installed: false, active: false },
];

vi.mock('@/api/hooks', () => ({
  usePlugins: () => ({ data: { plugins: mockPlugins }, isLoading: false, isError: false, error: null, refetch: vi.fn() }),
  useInstallPlugin: () => ({ mutateAsync: vi.fn(), isPending: false }),
  useActivatePlugin: () => ({ mutateAsync: vi.fn(), isPending: false }),
  useDeactivatePlugin: () => ({ mutateAsync: vi.fn(), isPending: false }),
  useUninstallPlugin: () => ({ mutateAsync: vi.fn(), isPending: false }),
  useUploadPlugin: () => ({ mutateAsync: vi.fn(), isPending: false }),
  useInstallPluginFromUrl: () => ({ mutateAsync: vi.fn(), isPending: false }),
}));

vi.mock('@/components/ui/Toast', () => ({
  useToast: () => ({ success: vi.fn(), error: vi.fn(), info: vi.fn() }),
}));

function renderPage() {
  return render(<MemoryRouter><SettingsPluginsPage /></MemoryRouter>);
}

describe('SettingsPluginsPage', () => {
  it('renders plugins page', () => {
    renderPage();
    expect(screen.getByTestId('settings-plugins-page')).toBeInTheDocument();
  });

  it('renders refresh button', () => {
    renderPage();
    expect(screen.getByTestId('refresh-plugins-btn')).toBeInTheDocument();
  });

  it('renders plugin names', () => {
    renderPage();
    expect(screen.getByText('Mail Plugin')).toBeInTheDocument();
    expect(screen.getByText('Export Plugin')).toBeInTheDocument();
    expect(screen.getByText('Beta Plugin')).toBeInTheDocument();
  });

  it('renders install button for discovered plugins', () => {
    renderPage();
    expect(screen.getByTestId('install-plugin-beta-plugin')).toBeInTheDocument();
  });

  it('renders activate button for inactive plugins', () => {
    renderPage();
    expect(screen.getByTestId('activate-plugin-export-plugin')).toBeInTheDocument();
  });

  it('renders deactivate button for active plugins', () => {
    renderPage();
    expect(screen.getByTestId('deactivate-plugin-mail-plugin')).toBeInTheDocument();
  });

  it('renders uninstall button for installed plugins', () => {
    renderPage();
    expect(screen.getByTestId('uninstall-plugin-mail-plugin')).toBeInTheDocument();
    expect(screen.getByTestId('uninstall-plugin-export-plugin')).toBeInTheDocument();
  });

  it('renders upload section', () => {
    renderPage();
    expect(screen.getByTestId('plugin-zip-upload-input')).toBeInTheDocument();
    expect(screen.getByTestId('plugin-upload-btn')).toBeInTheDocument();
  });

  it('renders URL install section', () => {
    renderPage();
    expect(screen.getByTestId('plugin-url-input')).toBeInTheDocument();
    expect(screen.getByTestId('plugin-install-url-btn')).toBeInTheDocument();
  });
});
