/**
 * Storage Service Tests
 */

import { describe, it, expect, beforeEach, vi } from 'vitest'
import * as storageService from '../storage-service'
import type { Context } from '../../types/context'

describe('Storage Service', () => {
  let mockContext: Context
  let mockDb: any

  beforeEach(() => {
    mockDb = {
      query: vi.fn(),
    }

    mockContext = {
      db: mockDb as any,
      user: {
        id: 'user-1',
        email: 'test@example.com',
        name: 'Test User',
        role: 'ADMIN',
      },
    } as Context
  })

  describe('getStorageAccounts', () => {
    it('should return storage accounts', async () => {
      const mockRows = [
        {
          id: 'account-1',
          name: 'minio-account',
          provider: 'MINIO',
          endpoint: 'http://localhost:9000',
          metadata: JSON.stringify({}),
          created_at: new Date(),
          updated_at: new Date(),
        },
      ]

      mockDb.query.mockResolvedValue({ rows: mockRows })

      const accounts = await storageService.getStorageAccounts(mockContext)

      expect(accounts).toHaveLength(1)
      expect(accounts[0].name).toBe('minio-account')
    })
  })

  describe('createStorageAccount', () => {
    it('should create a storage account', async () => {
      const input = {
        name: 'new-account',
        provider: 'MINIO',
        endpoint: 'http://localhost:9000',
      }

      mockDb.query.mockResolvedValue({
        rows: [
          {
            id: 'account-1',
            ...input,
            metadata: JSON.stringify({}),
            created_at: new Date(),
            updated_at: new Date(),
          },
        ],
      })

      const account = await storageService.createStorageAccount(mockContext, input)

      expect(account).toBeDefined()
      expect(account.name).toBe('new-account')
      expect(mockDb.query).toHaveBeenCalledWith(
        expect.stringContaining('INSERT INTO storage_accounts'),
        expect.arrayContaining([input.name, input.provider])
      )
    })
  })
})

