import { render, screen, waitFor } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { resetStore } from '../test/storeTestUtils';
import { useStore } from '../store/useStore';
import { ModelStatusBadge } from './ModelStatusBadge';

const apiMock = vi.hoisted(() => ({
  getAiModelStatus: vi.fn(),
}));

vi.mock('../lib/api', () => ({
  getAiModelStatus: apiMock.getAiModelStatus,
}));

describe('ModelStatusBadge', () => {
  beforeEach(() => {
    resetStore();
    vi.clearAllMocks();
    apiMock.getAiModelStatus.mockResolvedValue({
      selected_model: 'sam2.1_hiera_tiny',
      gpu: { available: true, device: 'cuda', name: 'RTX 4090', torch_available: true },
      models: [
        { id: 'sam2.1_hiera_tiny', label: 'SAM 2.1 Tiny', available: true, loaded: false, device: 'cuda', supports: ['point', 'box'], message: 'SAM 2.1 Tiny ready', package_available: true, checkpoint_exists: true, python_ok: true, torch_ok: true, cuda_required: false },
      ],
    });
  });

  it('loads real model status for the selected model', async () => {
    render(<ModelStatusBadge />);

    expect(await screen.findByText('SAM 2.1 Tiny 可用')).toBeInTheDocument();
    expect(apiMock.getAiModelStatus).toHaveBeenCalledWith('sam2.1_hiera_tiny');
  });

  it('does not expose disabled SAM3 status in the badge', async () => {
    render(<ModelStatusBadge />);

    await waitFor(() => expect(apiMock.getAiModelStatus).toHaveBeenCalledWith('sam2.1_hiera_tiny'));
    expect(screen.queryByText(/SAM 3/)).not.toBeInTheDocument();
  });
});
