import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, waitFor } from '@testing-library/react';

vi.mock('@umijs/max');
vi.mock('@/services/api');

import { smokeTest } from '../../../../test/smoke';
import Page from './detail';
import { getPurchaseOrder } from '@/services/api';

describe('Purchase / Orders / detail page', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  smokeTest(Page);

  it('renders order detail sections when order data is available', async () => {
    vi.mocked(getPurchaseOrder).mockResolvedValueOnce({
      code: 0,
      msg: 'ok',
      success: true,
      data: {
        orderId: 'order_001',
        orderNo: 'PO-2024-001',
        supplierName: '测试供应商',
        orderDate: '2024-01-15',
        purchaseBy: '张三',
        contractAmount: '10000',
        receivedQty: '0',
        paidAmount: '0',
        receiptStatus: 0,
        paymentStatus: 0,
        status: 0,
        remark: '',
        details: [],
      } as any,
    });

    render(<Page />);

    await waitFor(() => {
      // Text is embedded inside "采购订单详情 — PO-2024-001", use regex match
      expect(screen.getByText(/PO-2024-001/)).toBeInTheDocument();
    });
    expect(screen.getByText('测试供应商')).toBeInTheDocument();
  });

  it('shows 确认订单 button for draft status orders', async () => {
    vi.mocked(getPurchaseOrder).mockResolvedValueOnce({
      code: 0,
      msg: 'ok',
      success: true,
      data: {
        orderId: 'order_001',
        orderNo: 'PO-2024-001',
        supplierName: '供应商A',
        orderDate: '2024-01-15',
        purchaseBy: '张三',
        contractAmount: '5000',
        receivedQty: '0',
        paidAmount: '0',
        receiptStatus: 0,
        paymentStatus: 0,
        status: 0,
        remark: '',
        details: [],
      } as any,
    });

    render(<Page />);

    await waitFor(() => {
      expect(screen.getByRole('button', { name: /确认订单/ })).toBeInTheDocument();
    });
  });
});
