import { describe, it, expect } from 'vitest'
import { CREDENTIAL_SCHEMAS, getCredentialSchema, listCredentialSchemas, getCredentialTypes } from '../schemas'

describe('Credential Schema Registry', () => {
  describe('CREDENTIAL_SCHEMAS', () => {
    it('should have OAuth schemas', () => {
      expect(CREDENTIAL_SCHEMAS['google-sheets']).toBeDefined()
      expect(CREDENTIAL_SCHEMAS['google-sheets'].type).toBe('oauth')
      expect(CREDENTIAL_SCHEMAS['google-sheets'].oauthProvider).toBe('google-sheets')

      expect(CREDENTIAL_SCHEMAS.dropbox).toBeDefined()
      expect(CREDENTIAL_SCHEMAS.dropbox.type).toBe('oauth')
      expect(CREDENTIAL_SCHEMAS.dropbox.oauthProvider).toBe('dropbox')
    })

    it('should have api-key single-field schema', () => {
      expect(CREDENTIAL_SCHEMAS['api-key']).toBeDefined()
      expect(CREDENTIAL_SCHEMAS['api-key'].type).toBe('single_field')
      expect(CREDENTIAL_SCHEMAS['api-key'].fields).toHaveLength(1)
      expect(CREDENTIAL_SCHEMAS['api-key'].fields?.[0].key).toBe('apiKey')
    })
  })

  describe('getCredentialSchema', () => {
    it('should return schema for valid type', () => {
      const schema = getCredentialSchema('google-sheets')
      expect(schema).toStrictEqual(CREDENTIAL_SCHEMAS['google-sheets'])
    })

    it('should return api-key schema for unknown type', () => {
      const schema = getCredentialSchema('unknown')
      expect(schema).toStrictEqual(CREDENTIAL_SCHEMAS['api-key'])
    })

    it('should validate schema with Zod', () => {
      // Should not throw - schemas are valid
      expect(() => getCredentialSchema('google-sheets')).not.toThrow()
      expect(() => getCredentialSchema('api-key')).not.toThrow()
    })

    it('should return OAuth schemas correctly', () => {
      const googleSheetsSchema = getCredentialSchema('google-sheets')
      expect(googleSheetsSchema.type).toBe('oauth')
      expect(googleSheetsSchema.oauthProvider).toBe('google-sheets')
      expect(googleSheetsSchema.fields).toBeUndefined()
    })
  })

  describe('listCredentialSchemas', () => {
    it('should return all schemas as array', () => {
      const schemas = listCredentialSchemas()
      expect(schemas.length).toBeGreaterThan(0)
      expect(schemas).toContainEqual(CREDENTIAL_SCHEMAS['google-sheets'])
      expect(schemas).toContainEqual(CREDENTIAL_SCHEMAS['api-key'])
    })

    it('should return array with correct schema structure', () => {
      const schemas = listCredentialSchemas()
      schemas.forEach((schema) => {
        expect(schema).toHaveProperty('type')
        expect(schema).toHaveProperty('label')
        expect(schema).toHaveProperty('description')
        expect(schema).toHaveProperty('nameSuggestions')
      })
    })
  })

  describe('getCredentialTypes', () => {
    it('should return all type keys', () => {
      const types = getCredentialTypes()
      expect(types).toContain('dropbox')
      expect(types).toContain('google-sheets')
      expect(types).toContain('api-key')
    })

    it('should return unique types', () => {
      const types = getCredentialTypes()
      const uniqueTypes = [...new Set(types)]
      expect(types).toEqual(uniqueTypes)
    })
  })
})
