import { renderHook, act } from '@testing-library/react';
import { vi, describe, it, expect, beforeEach } from 'vitest';

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

vi.mock('../../lib/supabase', () => ({
  supabase: {
    from: vi.fn(() => ({
      select: vi.fn(() => ({ in: mockIn })),
    })),
  },
}));

import { usePricing } from '../usePricing';

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

  it('starts with default prices {200: 2000, 300: 3000} and loading=true', () => {
    mockIn.mockReturnValue(new Promise(() => {}));
    const { result } = renderHook(() => usePricing());
    expect(result.current.loading).toBe(true);
    expect(result.current.prices).toEqual({ 200: 2000, 300: 3000 });
  });

  it('updates prices from DB and sets loading=false', async () => {
    mockIn.mockResolvedValue({
      data: [
        { key: 'price_200', value: '1500' },
        { key: 'price_300', value: '2500' },
      ],
      error: null,
    });
    const { result } = renderHook(() => usePricing());
    await act(async () => {});
    expect(result.current.loading).toBe(false);
    expect(result.current.prices).toEqual({ 200: 1500, 300: 2500 });
  });

  it('keeps default prices when DB returns empty data', async () => {
    mockIn.mockResolvedValue({ data: null, error: null });
    const { result } = renderHook(() => usePricing());
    await act(async () => {});
    expect(result.current.prices).toEqual({ 200: 2000, 300: 3000 });
  });
});
