/**
 * Tests for Attio getRecord fetch implementation
 */

import { describe as _describe, it, expect, vi, beforeEach } from 'vitest'

// Attio integration not in use; suite skipped per project decision (2026-04-29).
const describe = _describe.skip
import { getRecord } from './index.js'
import { ToolingError } from '../../../../../../types.js'

// Mock fetch globally
const mockFetch = vi.fn()
global.fetch = mockFetch as typeof fetch

describe('getRecord', () => {
  beforeEach(() => {
    vi.clearAllMocks()
  })

  it('should fetch a record successfully', async () => {
    const mockResponse = {
      data: {
        id: {
          workspace_id: 'workspace-123',
          object_id: 'object-456',
          record_id: 'record-789'
        },
        created_at: '2024-01-01T12:00:00Z',
        web_url: 'https://app.attio.com/workspace/record-789',
        values: {
          name: [{ first_name: 'John', last_name: 'Doe' }],
          email: [{ email_address: 'john@example.com' }]
        }
      }
    }

    mockFetch.mockResolvedValue({
      ok: true,
      json: async () => mockResponse
    })

    const result = await getRecord({ apiKey: 'test-key' }, { object: 'people', recordId: 'record-789' })

    expect(result).toEqual({
      id: 'record-789',
      values: mockResponse.data.values,
      createdAt: '2024-01-01T12:00:00Z',
      webUrl: 'https://app.attio.com/workspace/record-789'
    })

    expect(mockFetch).toHaveBeenCalledWith(
      'https://api.attio.com/v2/objects/people/records/record-789',
      expect.objectContaining({
        method: 'GET',
        headers: {
          Authorization: 'Bearer test-key',
          Accept: 'application/json'
        }
      })
    )
  })

  it('should throw credentials_invalid when API key is missing', async () => {
    await expect(getRecord({ apiKey: '' }, { object: 'people', recordId: 'record-123' })).rejects.toThrow(ToolingError)

    await expect(getRecord({ apiKey: '' }, { object: 'people', recordId: 'record-123' })).rejects.toThrow(
      'Missing Attio API key'
    )
  })

  it('should throw validation_error when parameters are missing', async () => {
    await expect(getRecord({ apiKey: 'test-key' }, { object: '', recordId: 'record-123' })).rejects.toThrow(
      ToolingError
    )

    await expect(getRecord({ apiKey: 'test-key' }, { object: 'people', recordId: '' })).rejects.toThrow(
      'Missing required parameters'
    )
  })

  it('should handle 404 not found errors', async () => {
    mockFetch.mockResolvedValue({
      ok: false,
      status: 404,
      statusText: 'Not Found',
      text: async () =>
        JSON.stringify({
          status_code: 404,
          type: 'invalid_request_error',
          code: 'not_found',
          message: 'Record not found'
        })
    })

    await expect(getRecord({ apiKey: 'test-key' }, { object: 'people', recordId: 'nonexistent' })).rejects.toThrow(
      ToolingError
    )

    try {
      await getRecord({ apiKey: 'test-key' }, { object: 'people', recordId: 'nonexistent' })
    } catch (error) {
      expect((error as ToolingError).errorType).toBe('validation_error')
      expect((error as ToolingError).message).toContain('Record not found')
    }
  })

  it('should handle 401 authentication errors', async () => {
    mockFetch.mockResolvedValue({
      ok: false,
      status: 401,
      statusText: 'Unauthorized',
      text: async () =>
        JSON.stringify({
          status_code: 401,
          type: 'authentication_error',
          code: 'unauthorized',
          message: 'Invalid API key'
        })
    })

    try {
      await getRecord({ apiKey: 'invalid-key' }, { object: 'people', recordId: 'record-123' })
    } catch (error) {
      expect((error as ToolingError).errorType).toBe('credentials_invalid')
      expect((error as ToolingError).message).toContain('Invalid API key')
    }
  })

  it('should work with different object types', async () => {
    const mockResponse = {
      data: {
        id: {
          workspace_id: 'workspace-123',
          object_id: 'object-456',
          record_id: 'company-789'
        },
        created_at: '2024-01-01T12:00:00Z',
        web_url: 'https://app.attio.com/workspace/company-789',
        values: {
          name: 'Acme Corp',
          domains: [{ domain: 'acme.com' }]
        }
      }
    }

    mockFetch.mockResolvedValue({
      ok: true,
      json: async () => mockResponse
    })

    const result = await getRecord({ apiKey: 'test-key' }, { object: 'companies', recordId: 'company-789' })

    expect(result.id).toBe('company-789')
    expect(result.values.name).toBe('Acme Corp')

    expect(mockFetch).toHaveBeenCalledWith(
      'https://api.attio.com/v2/objects/companies/records/company-789',
      expect.anything()
    )
  })
})
