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

describe('useLoadingState', () => {
  beforeEach(() => {
    vi.useFakeTimers();
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  it('should initialize with IDLE status', () => {
    const { result } = renderHook(() => useLoadingState());
    expect(result.current.state.status).toBe('IDLE');
    expect(result.current.isLoading).toBe(false);
  });

  it('should transition to LOADING status', () => {
    const { result } = renderHook(() => useLoadingState());
    
    act(() => {
      result.current.setLoading('LOADING', 'Yükleniyor...');
    });

    expect(result.current.state.status).toBe('LOADING');
    expect(result.current.state.message).toBe('Yükleniyor...');
    expect(result.current.isLoading).toBe(true);
  });

  it('should set progress', () => {
    const { result } = renderHook(() => useLoadingState());
    
    act(() => {
      result.current.setProgress(50);
    });

    expect(result.current.state.progress).toBe(50);
  });

  it('should set error status', () => {
    const { result } = renderHook(() => useLoadingState());
    
    act(() => {
      result.current.setError('Hata oluştu');
    });

    expect(result.current.state.status).toBe('ERROR');
    expect(result.current.state.message).toBe('Hata oluştu');
    expect(result.current.isError).toBe(true);
  });

  it('should auto-clear after duration', () => {
    const { result } = renderHook(() => useLoadingState());
    
    act(() => {
      result.current.setLoading('SUCCESS', 'Tamamlandı', 1000);
    });

    expect(result.current.state.status).toBe('SUCCESS');

    act(() => {
      vi.advanceTimersByTime(1000);
    });

    expect(result.current.state.status).toBe('IDLE');
  });

  it('should clear state manually', () => {
    const { result } = renderHook(() => useLoadingState());
    
    act(() => {
      result.current.setLoading('LOADING', 'Yükleniyor');
      result.current.setProgress(50);
    });

    act(() => {
      result.current.clear();
    });

    expect(result.current.state.status).toBe('IDLE');
    expect(result.current.state.progress).toBe(0);
    expect(result.current.state.message).toBe('');
  });

  it('should return state via getState', () => {
    const { result } = renderHook(() => useLoadingState());
    
    act(() => {
      result.current.setLoading('LOADING', 'Test');
    });

    const state = result.current.getState();
    expect(state.status).toBe('LOADING');
    expect(state.message).toBe('Test');
  });
});
