import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render } from 'ink-testing-library';
import React from 'react';
import { SupplierListScreen } from '../../../components/masterdata/suppliers/SupplierListScreen.js';
import { useSuppliers } from '../../../hooks/useSuppliers.js';
import type { SupplierDTO } from '@effigenix/api-client';

vi.mock('../../../state/navigation-context.js', () => ({
  useNavigation: () => ({
    replace: vi.fn(),
    back: vi.fn(),
    navigate: vi.fn(),
    current: 'supplier-list',
    params: {},
    canGoBack: true,
  }),
}));

vi.mock('../../../hooks/useSuppliers.js', () => ({
  useSuppliers: vi.fn(),
}));

const mockSuppliers: SupplierDTO[] = [
  {
    id: '1',
    name: 'Frisch AG',
    status: 'ACTIVE',
    rating: null,
    certificates: [],
  } as unknown as SupplierDTO,
  {
    id: '2',
    name: 'Alt GmbH',
    status: 'INACTIVE',
    rating: null,
    certificates: [],
  } as unknown as SupplierDTO,
];

const defaultState = {
  suppliers: mockSuppliers,
  loading: false,
  error: null,
  fetchSuppliers: vi.fn(),
  createSupplier: vi.fn(),
  updateSupplier: vi.fn(),
  activateSupplier: vi.fn(),
  deactivateSupplier: vi.fn(),
  rateSupplier: vi.fn(),
  addCertificate: vi.fn(),
  removeCertificate: vi.fn(),
  clearError: vi.fn(),
};

describe('TC-SUP-06: Lieferantenliste – Filter', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(useSuppliers).mockReturnValue(defaultState);
  });

  it('Zeigt initial alle Lieferanten (Filter: Alle)', () => {
    const { lastFrame } = render(React.createElement(SupplierListScreen));
    expect(lastFrame()).toContain('Lieferanten');
    expect(lastFrame()).toContain('Alle');
    expect(lastFrame()).toContain('Frisch AG');
    expect(lastFrame()).toContain('Alt GmbH');
  });

  it('[A] filtert auf aktive Lieferanten', async () => {
    const { stdin, lastFrame } = render(React.createElement(SupplierListScreen));
    await new Promise((r) => setTimeout(r, 20));

    stdin.write('A');
    await new Promise((r) => setTimeout(r, 30));

    expect(lastFrame()).toContain('Aktiv');
    expect(lastFrame()).toContain('Frisch AG');
    expect(lastFrame()).not.toContain('Alt GmbH');
  });

  it('[I] filtert auf inaktive Lieferanten', async () => {
    const { stdin, lastFrame } = render(React.createElement(SupplierListScreen));
    await new Promise((r) => setTimeout(r, 20));

    stdin.write('I');
    await new Promise((r) => setTimeout(r, 30));

    expect(lastFrame()).toContain('Inaktiv');
    expect(lastFrame()).toContain('Alt GmbH');
    expect(lastFrame()).not.toContain('Frisch AG');
  });

  it('[a] setzt Filter zurück auf Alle', async () => {
    const { stdin, lastFrame } = render(React.createElement(SupplierListScreen));
    await new Promise((r) => setTimeout(r, 20));

    // Erst auf Aktiv filtern, dann zurücksetzen
    stdin.write('A');
    await new Promise((r) => setTimeout(r, 30));
    stdin.write('a');
    await new Promise((r) => setTimeout(r, 30));

    expect(lastFrame()).toContain('Alle');
    expect(lastFrame()).toContain('Frisch AG');
    expect(lastFrame()).toContain('Alt GmbH');
  });

  it('Zeigt Lade-Spinner wenn loading=true', () => {
    vi.mocked(useSuppliers).mockReturnValue({ ...defaultState, loading: true, suppliers: [] });
    const { lastFrame } = render(React.createElement(SupplierListScreen));
    expect(lastFrame()).toContain('Lade Lieferanten');
  });
});
