// src/routes/stats.routes.test.ts
import { describe, it, expect, vi, beforeEach } from 'vitest';
import supertest from 'supertest';
import { createTestApp } from '../tests/utils/createTestApp';

// 1. Mock the Service Layer directly.
vi.mock('../services/db/index.db', () => ({
  adminRepo: {
    getMostFrequentSaleItems: vi.fn(),
  },
}));

// Import the router and mocked DB AFTER all mocks are defined.
import statsRouter from './stats.routes';
import * as db from '../services/db/index.db';
import { mockLogger } from '../tests/utils/mockLogger';

// Mock the logger to keep test output clean
vi.mock('../services/logger.server', async () => ({
  // Use async import to avoid hoisting issues with mockLogger
  logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));

const expectLogger = expect.objectContaining({
  info: expect.any(Function),
  error: expect.any(Function),
});

describe('Stats Routes (/api/stats)', () => {
  const app = createTestApp({ router: statsRouter, basePath: '/api/stats' });

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

  describe('GET /most-frequent-sales', () => {
    it('should return most frequent sale items with default parameters', async () => {
      vi.mocked(db.adminRepo.getMostFrequentSaleItems).mockResolvedValue([]);
      const response = await supertest(app).get('/api/stats/most-frequent-sales');
      expect(response.status).toBe(200);
      expect(db.adminRepo.getMostFrequentSaleItems).toHaveBeenCalledWith(30, 10, expectLogger);
    });

    it('should use provided query parameters', async () => {
      vi.mocked(db.adminRepo.getMostFrequentSaleItems).mockResolvedValue([]);
      await supertest(app).get('/api/stats/most-frequent-sales?days=90&limit=5');
      expect(db.adminRepo.getMostFrequentSaleItems).toHaveBeenCalledWith(90, 5, expectLogger);
    });

    it('should return 500 if the database call fails', async () => {
      const dbError = new Error('DB Error');
      vi.mocked(db.adminRepo.getMostFrequentSaleItems).mockRejectedValue(dbError);
      const response = await supertest(app).get('/api/stats/most-frequent-sales');
      expect(response.status).toBe(500);
      expect(response.body.message).toBe('DB Error');
      expect(mockLogger.error).toHaveBeenCalledWith(
        { error: dbError },
        'Error fetching most frequent sale items in /api/stats/most-frequent-sales:',
      );
    });

    it('should return 400 for invalid query parameters', async () => {
      const response = await supertest(app).get('/api/stats/most-frequent-sales?days=0&limit=abc');
      expect(response.status).toBe(400);
      expect(response.body.errors).toBeDefined();
      expect(response.body.errors.length).toBe(2);
    });
  });
});
