/**
 * Activity Service Tests
 *
 * Tests the activity log API service including:
 * - Fetching activity logs
 * - Query parameter filtering
 * - Error handling
 */
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { getActivityLogs, activityService } from './activity';

// Mock the apiRequest function
vi.mock('./api', () => ({
  apiRequest: vi.fn(),
}));

import { apiRequest } from './api';

describe('activity service', () => {
  const mockLogs = [
    {
      id: 1,
      workflow_id: 1,
      execution_id: 'exec-1',
      level: 'info',
      message: 'Workflow started',
      context: {},
      created_at: '2024-01-01T00:00:00Z',
    },
    {
      id: 2,
      workflow_id: 1,
      execution_id: 'exec-1',
      level: 'success',
      message: 'Workflow completed',
      context: {},
      created_at: '2024-01-01T00:00:01Z',
    },
  ];

  beforeEach(() => {
    vi.clearAllMocks();
  });

  // ==========================================================================
  // getActivityLogs Tests
  // ==========================================================================

  describe('getActivityLogs', () => {
    it('fetches activity logs without filters', async () => {
      vi.mocked(apiRequest).mockResolvedValue(mockLogs);

      const result = await getActivityLogs();

      expect(apiRequest).toHaveBeenCalledWith('/activity', { method: 'GET' });
      expect(result).toEqual(mockLogs);
    });

    it('includes workflow_id filter in query params', async () => {
      vi.mocked(apiRequest).mockResolvedValue(mockLogs);

      await getActivityLogs({ workflow_id: 123 });

      expect(apiRequest).toHaveBeenCalledWith('/activity?workflow_id=123', {
        method: 'GET',
      });
    });

    it('includes level filter in query params', async () => {
      vi.mocked(apiRequest).mockResolvedValue(mockLogs);

      await getActivityLogs({ level: 'error' });

      expect(apiRequest).toHaveBeenCalledWith('/activity?level=error', {
        method: 'GET',
      });
    });

    it('includes pagination filters', async () => {
      vi.mocked(apiRequest).mockResolvedValue(mockLogs);

      await getActivityLogs({ limit: 50, offset: 10 });

      expect(apiRequest).toHaveBeenCalledWith('/activity?limit=50&offset=10', {
        method: 'GET',
      });
    });

    it('includes date range filters', async () => {
      vi.mocked(apiRequest).mockResolvedValue(mockLogs);

      await getActivityLogs({
        start_date: '2024-01-01',
        end_date: '2024-01-31',
      });

      expect(apiRequest).toHaveBeenCalledWith(
        '/activity?start_date=2024-01-01&end_date=2024-01-31',
        { method: 'GET' },
      );
    });

    it('combines multiple filters', async () => {
      vi.mocked(apiRequest).mockResolvedValue(mockLogs);

      await getActivityLogs({
        workflow_id: 5,
        level: 'warning',
        limit: 25,
      });

      expect(apiRequest).toHaveBeenCalledWith('/activity?workflow_id=5&level=warning&limit=25', {
        method: 'GET',
      });
    });

    it('propagates API errors', async () => {
      const error = new Error('API Error');
      vi.mocked(apiRequest).mockRejectedValue(error);

      await expect(getActivityLogs()).rejects.toThrow('API Error');
    });
  });

  // ==========================================================================
  // activityService Object Tests
  // ==========================================================================

  describe('activityService', () => {
    it('exports getActivityLogs method', () => {
      expect(activityService.getActivityLogs).toBe(getActivityLogs);
    });
  });
});
