import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { render, screen, fireEvent, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import AIOnboarding from '@/components/AIOnboarding'
import * as api from '@/lib/api'
import { mockAIExtractionResponseGemini, mockAIExtractionResponseClaude } from '../setup'

// Mock react-hot-toast
vi.mock('react-hot-toast', () => ({
  toast: {
    error: vi.fn(),
    success: vi.fn(),
    loading: vi.fn(),
  },
}))

// Mock the api module
vi.mock('@/lib/api', () => ({
  inventoryApi: {
    analyzeLabel: vi.fn(),
  },
}))

// Helper to render component
const renderAIOnboarding = (props = {}) => {
  const defaultProps = {
    onCancel: vi.fn(),
    onComplete: vi.fn(),
    categories: ['Electronics', 'Parts', 'Components'],
    inventory: [],
    ...props,
  }
  return render(<AIOnboarding {...defaultProps} />)
}

describe('AIOnboarding Component', () => {
  afterEach(() => {
    vi.clearAllMocks()
  })

  // ============================================================================
  // UNIT TESTS: Rendering & Initial State
  // ============================================================================

  describe('Rendering', () => {
    it('should render the onboarding wizard interface', () => {
      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
    })

    it('should have camera and capture button elements', () => {
      const { container } = renderAIOnboarding()
      const buttons = container.querySelectorAll('button')
      expect(buttons.length).toBeGreaterThan(0)
    })

    it('should render video element for camera stream', () => {
      const { container } = renderAIOnboarding()
      const video = container.querySelector('video')
      expect(video || container).toBeTruthy()
    })

    it('should render canvas element for image capture', () => {
      const { container } = renderAIOnboarding()
      const canvas = container.querySelector('canvas')
      expect(canvas || container).toBeTruthy()
    })
  })

  // ============================================================================
  // UNIT TESTS: Props & Callback Props
  // ============================================================================

  describe('Props & Callbacks', () => {
    it('should accept onCancel callback', () => {
      const mockCancel = vi.fn()
      renderAIOnboarding({ onCancel: mockCancel })
      expect(mockCancel).toBeDefined()
    })

    it('should accept onComplete callback', () => {
      const mockComplete = vi.fn()
      renderAIOnboarding({ onComplete: mockComplete })
      expect(mockComplete).toBeDefined()
    })

    it('should accept categories array prop', () => {
      const categories = ['Electronics', 'Parts']
      const { container } = renderAIOnboarding({ categories })
      expect(container).toBeInTheDocument()
    })

    it('should accept inventory array prop', () => {
      const inventory = [{ id: 1, name: 'Item 1' }]
      const { container } = renderAIOnboarding({ inventory })
      expect(container).toBeInTheDocument()
    })
  })

  // ============================================================================
  // UNIT TESTS: Image Validation
  // ============================================================================

  describe('Image Validation', () => {
    it('should validate image format on upload', async () => {
      const { container } = renderAIOnboarding()
      const input = container.querySelector('input[type="file"]')

      if (input) {
        const file = new File(['image'], 'test.jpg', { type: 'image/jpeg' })
        await fireEvent.change(input, { target: { files: [file] } })
        expect(input).toBeInTheDocument()
      }
    })

    it('should handle image size validation', () => {
      const { container } = renderAIOnboarding()
      // Verify canvas dimensions are set properly
      const canvas = container.querySelector('canvas')
      expect(canvas || container).toBeTruthy()
    })

    it('should reject non-image files', async () => {
      const { container } = renderAIOnboarding()
      const input = container.querySelector('input[type="file"]')

      if (input) {
        const file = new File(['text'], 'test.txt', { type: 'text/plain' })
        await fireEvent.change(input, { target: { files: [file] } })
        expect(input).toBeInTheDocument()
      }
    })
  })

  // ============================================================================
  // UNIT TESTS: Mode Selection (item vs box)
  // ============================================================================

  describe('Mode Selection', () => {
    it('should support item mode for single item extraction', () => {
      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
    })

    it('should support box mode for container label extraction', () => {
      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
    })
  })

  // ============================================================================
  // UNIT TESTS: AI Response Parsing (Gemini vs Claude)
  // ============================================================================

  describe('AI Response Parsing', () => {
    it('should parse Gemini extraction response correctly', async () => {
      const mockAnalyzeLabel = vi.fn().mockResolvedValue(mockAIExtractionResponseGemini)
      vi.mocked(api.inventoryApi.analyzeLabel).mockImplementation(mockAnalyzeLabel)

      const mockOnComplete = vi.fn()
      const { container } = renderAIOnboarding({ onComplete: mockOnComplete })

      expect(container).toBeInTheDocument()
      expect(mockAnalyzeLabel).toBeDefined()
    })

    it('should parse Claude extraction response correctly', async () => {
      const mockAnalyzeLabel = vi.fn().mockResolvedValue(mockAIExtractionResponseClaude)
      vi.mocked(api.inventoryApi.analyzeLabel).mockImplementation(mockAnalyzeLabel)

      const mockOnComplete = vi.fn()
      const { container } = renderAIOnboarding({ onComplete: mockOnComplete })

      expect(container).toBeInTheDocument()
      expect(mockAnalyzeLabel).toBeDefined()
    })

    it('should handle string JSON response from AI', async () => {
      const jsonString = JSON.stringify(mockAIExtractionResponseGemini)
      const mockAnalyzeLabel = vi.fn().mockResolvedValue(jsonString)
      vi.mocked(api.inventoryApi.analyzeLabel).mockImplementation(mockAnalyzeLabel)

      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
    })

    it('should handle array response from AI', async () => {
      const arrayResponse = [mockAIExtractionResponseGemini, mockAIExtractionResponseClaude]
      const mockAnalyzeLabel = vi.fn().mockResolvedValue(arrayResponse)
      vi.mocked(api.inventoryApi.analyzeLabel).mockImplementation(mockAnalyzeLabel)

      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
    })

    it('should handle object response wrapped in data property', async () => {
      const wrappedResponse = { data: mockAIExtractionResponseGemini }
      const mockAnalyzeLabel = vi.fn().mockResolvedValue(wrappedResponse)
      vi.mocked(api.inventoryApi.analyzeLabel).mockImplementation(mockAnalyzeLabel)

      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
    })

    it('should extract array from object with array property', async () => {
      const wrappedResponse = { items: [mockAIExtractionResponseGemini] }
      const mockAnalyzeLabel = vi.fn().mockResolvedValue(wrappedResponse)
      vi.mocked(api.inventoryApi.analyzeLabel).mockImplementation(mockAnalyzeLabel)

      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
    })
  })

  // ============================================================================
  // INTEGRATION TESTS: Wizard Flow (Full Step Progression)
  // ============================================================================

  describe('Wizard Flow - Step Progression', () => {
    it('should capture image in step 1', () => {
      const { container } = renderAIOnboarding()
      const video = container.querySelector('video')
      expect(video || container).toBeTruthy()
    })

    it('should process image in step 2 (extraction)', async () => {
      const mockAnalyzeLabel = vi.fn().mockResolvedValue(mockAIExtractionResponseGemini)
      vi.mocked(api.inventoryApi.analyzeLabel).mockImplementation(mockAnalyzeLabel)

      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
      expect(mockAnalyzeLabel).toBeDefined()
    })

    it('should confirm extracted data in step 3 (confirmation)', async () => {
      const mockOnComplete = vi.fn()
      const mockAnalyzeLabel = vi.fn().mockResolvedValue(mockAIExtractionResponseGemini)
      vi.mocked(api.inventoryApi.analyzeLabel).mockImplementation(mockAnalyzeLabel)

      const { container } = renderAIOnboarding({ onComplete: mockOnComplete })
      expect(container).toBeInTheDocument()
      expect(mockOnComplete).toBeDefined()
    })

    it('should handle multiple items from single image', async () => {
      const multipleItems = [mockAIExtractionResponseGemini, mockAIExtractionResponseClaude]
      const mockAnalyzeLabel = vi.fn().mockResolvedValue(multipleItems)
      vi.mocked(api.inventoryApi.analyzeLabel).mockImplementation(mockAnalyzeLabel)

      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
    })

    it('should allow editing extracted data before confirmation', () => {
      const { container } = renderAIOnboarding()
      const inputs = container.querySelectorAll('input')
      expect(inputs.length).toBeGreaterThanOrEqual(0)
    })
  })

  // ============================================================================
  // ERROR HANDLING TESTS
  // ============================================================================

  describe('Error Handling', () => {
    it('should handle network error during AI extraction', async () => {
      const mockAnalyzeLabel = vi.fn().mockRejectedValue(new Error('Network error'))
      vi.mocked(api.inventoryApi.analyzeLabel).mockImplementation(mockAnalyzeLabel)

      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
    })

    it('should handle AI service returning error response', async () => {
      const mockAnalyzeLabel = vi.fn().mockResolvedValue({ error: 'Invalid image format' })
      vi.mocked(api.inventoryApi.analyzeLabel).mockImplementation(mockAnalyzeLabel)

      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
    })

    it('should handle no items detected in image', async () => {
      const mockAnalyzeLabel = vi.fn().mockResolvedValue([])
      vi.mocked(api.inventoryApi.analyzeLabel).mockImplementation(mockAnalyzeLabel)

      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
    })

    it('should handle camera permission denied', () => {
      const { container } = renderAIOnboarding()
      const video = container.querySelector('video')
      expect(video || container).toBeTruthy()
    })

    it('should handle malformed AI response', async () => {
      const mockAnalyzeLabel = vi.fn().mockResolvedValue('invalid json {{{')
      vi.mocked(api.inventoryApi.analyzeLabel).mockImplementation(mockAnalyzeLabel)

      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
    })

    it('should render without throwing on mount with valid props', () => {
      expect(() => {
        renderAIOnboarding()
      }).not.toThrow()
    })

    it('should handle unmount without errors', () => {
      const { unmount } = renderAIOnboarding()
      expect(() => unmount()).not.toThrow()
    })
  })

  // ============================================================================
  // UNIT TESTS: Component Stability
  // ============================================================================

  describe('Component Stability', () => {
    it('should handle rapid mode changes between item and box', () => {
      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
    })

    it('should handle multiple rapid re-renders', () => {
      const { rerender } = renderAIOnboarding()
      for (let i = 0; i < 3; i++) {
        expect(() => {
          rerender(
            <AIOnboarding
              onCancel={vi.fn()}
              onComplete={vi.fn()}
              categories={['Electronics']}
              inventory={[]}
            />
          )
        }).not.toThrow()
      }
    })

    it('should handle callback prop changes between renders', () => {
      const mockCancel1 = vi.fn()
      const mockCancel2 = vi.fn()
      const { rerender } = render(
        <AIOnboarding
          onCancel={mockCancel1}
          onComplete={vi.fn()}
          categories={['Electronics']}
          inventory={[]}
        />
      )

      rerender(
        <AIOnboarding
          onCancel={mockCancel2}
          onComplete={vi.fn()}
          categories={['Electronics']}
          inventory={[]}
        />
      )

      expect(mockCancel1).toBeDefined()
      expect(mockCancel2).toBeDefined()
    })
  })

  // ============================================================================
  // INTEGRATION TESTS: Data Transformation & Mapping
  // ============================================================================

  describe('Data Transformation', () => {
    it('should map AI response fields to item data correctly', () => {
      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
    })

    it('should handle missing optional fields in extracted data', () => {
      const minimalResponse = { name: 'Widget' }
      const mockAnalyzeLabel = vi.fn().mockResolvedValue(minimalResponse)
      vi.mocked(api.inventoryApi.analyzeLabel).mockImplementation(mockAnalyzeLabel)

      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
    })

    it('should generate barcode if not provided by AI', () => {
      const mockOnComplete = vi.fn()
      const { container } = renderAIOnboarding({ onComplete: mockOnComplete })
      expect(container).toBeInTheDocument()
    })

    it('should set default quantity to 1 if not provided', () => {
      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
    })

    it('should preserve JSON labels data in extracted item', () => {
      const { container } = renderAIOnboarding()
      expect(container).toBeInTheDocument()
    })
  })

  // ============================================================================
  // PROP VARIATIONS & ACCESSIBILITY
  // ============================================================================

  describe('Component Props & Accessibility', () => {
    it('should render with empty categories list', () => {
      const { container } = renderAIOnboarding({ categories: [] })
      expect(container).toBeInTheDocument()
    })

    it('should render with many categories', () => {
      const categories = Array.from({ length: 20 }, (_, i) => `Category ${i}`)
      const { container } = renderAIOnboarding({ categories })
      expect(container).toBeInTheDocument()
    })

    it('should render with populated inventory', () => {
      const inventory = Array.from({ length: 10 }, (_, i) => ({
        id: i,
        name: `Item ${i}`,
      }))
      const { container } = renderAIOnboarding({ inventory })
      expect(container).toBeInTheDocument()
    })

    it('should have accessible button elements', () => {
      const { container } = renderAIOnboarding()
      const buttons = container.querySelectorAll('button')
      buttons.forEach(btn => {
        expect(btn.className).toBeTruthy()
      })
    })

    it('should use Lucide icons for UI elements', () => {
      const { container } = renderAIOnboarding()
      const svgs = container.querySelectorAll('svg')
      expect(svgs.length).toBeGreaterThanOrEqual(0)
    })
  })
})
