/* 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',
      email: 'test@example.com',
      role: 'BUYER',
      kycStatus: 'NONE',
      isActive: true,
      createdAt: '2024-01-01T00:00:00.000Z',
    },
    fetchProfile: vi.fn(),
  };
  return {
    useAuthStore: vi.fn((selector) => {
      if (typeof selector === 'function') return selector(store);
      return store;
    }),
  };
});

vi.mock('@/lib/profile-api', () => ({
  profileApi: {
    getAgentProfile: vi.fn().mockRejectedValue(new Error('Not an agent')),
    updateProfile: vi.fn().mockResolvedValue({}),
  },
}));

import ProfilePage from '../profile/page';

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

  it('renders profile heading', async () => {
    render(<ProfilePage />);
    expect(screen.getByText('Hồ sơ cá nhân')).toBeInTheDocument();
  });

  it('renders user information', async () => {
    render(<ProfilePage />);

    await waitFor(() => {
      expect(screen.getByText('Nguyen Van A')).toBeInTheDocument();
      expect(screen.getByText('0912345678')).toBeInTheDocument();
    });
  });

  it('renders KYC status badge', async () => {
    render(<ProfilePage />);

    await waitFor(() => {
      expect(screen.getByText('Chưa xác minh')).toBeInTheDocument();
    });
  });

  it('renders edit button', async () => {
    render(<ProfilePage />);

    await waitFor(() => {
      expect(screen.getByRole('button', { name: /chỉnh sửa/i })).toBeInTheDocument();
    });
  });

  it('enters edit mode when edit button is clicked', async () => {
    render(<ProfilePage />);

    await waitFor(() => {
      expect(screen.getByRole('button', { name: /chỉnh sửa/i })).toBeInTheDocument();
    });

    await userEvent.click(screen.getByRole('button', { name: /chỉnh sửa/i }));

    await waitFor(() => {
      expect(screen.getByRole('button', { name: /lưu thay đổi/i })).toBeInTheDocument();
      expect(screen.getByRole('button', { name: /hủy/i })).toBeInTheDocument();
    });
  });

  it('renders account status section', async () => {
    render(<ProfilePage />);

    await waitFor(() => {
      expect(screen.getByText('Trạng thái tài khoản')).toBeInTheDocument();
      expect(screen.getByText('Hoạt động')).toBeInTheDocument();
    });
  });
});
