/* eslint-disable import-x/order */
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { beforeEach, describe, expect, it, vi } from 'vitest';
vi.mock('@/lib/auth-store', () => {
  const store = {
    user: {
      id: 'user-1',
      fullName: 'Nguyen Van A',
      phone: '0912345678',
      kycStatus: 'NONE',
    },
    fetchProfile: vi.fn(),
  };
  return {
    useAuthStore: vi.fn((selector) => {
      if (typeof selector === 'function') return selector(store);
      return store;
    }),
  };
});

vi.mock('@/lib/api-client', () => ({
  apiClient: {
    patch: vi.fn().mockResolvedValue({}),
  },
}));

import KycPage from '../kyc/page';

describe('KycPage', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('renders KYC page heading', () => {
    render(<KycPage />);
    expect(screen.getByText('Xác minh danh tính (KYC)')).toBeInTheDocument();
  });

  it('renders KYC status badge as not verified', () => {
    render(<KycPage />);
    expect(screen.getByText('Chưa xác minh')).toBeInTheDocument();
  });

  it('renders step indicator', () => {
    render(<KycPage />);
    expect(screen.getByText('1')).toBeInTheDocument();
    expect(screen.getByText('2')).toBeInTheDocument();
    expect(screen.getByText('3')).toBeInTheDocument();
  });

  it('renders document type selector on step 1', () => {
    render(<KycPage />);
    expect(screen.getByLabelText(/loại giấy tờ/i)).toBeInTheDocument();
    expect(screen.getByLabelText(/số giấy tờ/i)).toBeInTheDocument();
  });

  it('shows error when continuing without document number', async () => {
    render(<KycPage />);

    await userEvent.click(screen.getByRole('button', { name: /tiếp tục/i }));

    await waitFor(() => {
      expect(screen.getByText(/vui lòng nhập số giấy tờ/i)).toBeInTheDocument();
    });
  });

  it('advances to step 2 after filling document number', async () => {
    render(<KycPage />);

    await userEvent.type(screen.getByLabelText(/số giấy tờ/i), '012345678901');
    await userEvent.click(screen.getByRole('button', { name: /tiếp tục/i }));

    await waitFor(() => {
      expect(screen.getByLabelText(/ảnh mặt trước/i)).toBeInTheDocument();
    });
  });
});
