import { describe, it, expect, beforeEach, vi } from 'vitest'
import * as aiOptimizationService from '../../services/ai-optimization'
import { Context } from '../../types/context'

describe('AI Optimization Service', () => {
  const mockContext: Context = {
    user: {
      id: 'user-1',
      email: 'test@example.com',
      name: 'Test User',
      role: 'USER',
    },
  } as Context

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

  describe('getOptimizationRecommendations', () => {
    it('should return optimization recommendations', async () => {
      const recommendations = await aiOptimizationService.getOptimizationRecommendations(
        mockContext,
        'tenant-1'
      )
      expect(Array.isArray(recommendations)).toBe(true)
    })

    it('should throw error if user is not authenticated', async () => {
      const unauthenticatedContext = {} as Context

      await expect(
        aiOptimizationService.getOptimizationRecommendations(unauthenticatedContext)
      ).rejects.toThrow('Authentication required')
    })
  })

  describe('getCostOptimizationRecommendations', () => {
    it('should return only cost optimization recommendations', async () => {
      const recommendations = await aiOptimizationService.getCostOptimizationRecommendations(
        mockContext,
        'tenant-1'
      )
      expect(Array.isArray(recommendations)).toBe(true)
      recommendations.forEach(rec => {
        expect(rec.type).toBe('COST')
      })
    })
  })
})

