import { describe, it, expect, beforeEach } from 'vitest'
import { useAssistantStore } from '../assistantStore'
import type { AssistantContext, AssistantMessage } from '../../domain/assistant'

const ctx: AssistantContext = {
  currentRoute: '/demand/results',
  workspace: 'DEMAND',
  userRole: 'TENANT',
  organizationId: 'org-1',
}

const msg: AssistantMessage = {
  id: 'msg-1',
  role: 'user',
  content: 'Welche Flächen sind verfügbar?',
  createdAt: new Date().toISOString(),
}

describe('assistantStore', () => {
  beforeEach(() => {
    useAssistantStore.setState({ isOpen: false, context: null, messages: [], isLoading: false, error: null })
  })

  describe('open / close', () => {
    it('opens the assistant', () => {
      useAssistantStore.getState().open()
      expect(useAssistantStore.getState().isOpen).toBe(true)
    })

    it('closes the assistant', () => {
      useAssistantStore.getState().open()
      useAssistantStore.getState().close()
      expect(useAssistantStore.getState().isOpen).toBe(false)
    })
  })

  describe('setContext', () => {
    it('stores the context', () => {
      useAssistantStore.getState().setContext(ctx)
      expect(useAssistantStore.getState().context).toEqual(ctx)
    })
  })

  describe('updateContext', () => {
    it('merges partial context when context exists', () => {
      useAssistantStore.getState().setContext(ctx)
      useAssistantStore.getState().updateContext({ currentRoute: '/demand/compare' })
      expect(useAssistantStore.getState().context?.currentRoute).toBe('/demand/compare')
      expect(useAssistantStore.getState().context?.workspace).toBe('DEMAND')
    })

    it('is a no-op when context is null', () => {
      useAssistantStore.getState().updateContext({ currentRoute: '/demand/compare' })
      expect(useAssistantStore.getState().context).toBeNull()
    })
  })

  describe('addMessage', () => {
    it('appends messages in order', () => {
      const msg2: AssistantMessage = { ...msg, id: 'msg-2', role: 'assistant', content: 'Hier sind die Ergebnisse.' }
      useAssistantStore.getState().addMessage(msg)
      useAssistantStore.getState().addMessage(msg2)
      const { messages } = useAssistantStore.getState()
      expect(messages).toHaveLength(2)
      expect(messages[0].id).toBe('msg-1')
      expect(messages[1].id).toBe('msg-2')
    })
  })

  describe('setLoading', () => {
    it('toggles loading state', () => {
      useAssistantStore.getState().setLoading(true)
      expect(useAssistantStore.getState().isLoading).toBe(true)
      useAssistantStore.getState().setLoading(false)
      expect(useAssistantStore.getState().isLoading).toBe(false)
    })
  })

  describe('setError', () => {
    it('stores and clears error', () => {
      useAssistantStore.getState().setError('Verbindungsfehler')
      expect(useAssistantStore.getState().error).toBe('Verbindungsfehler')
      useAssistantStore.getState().setError(null)
      expect(useAssistantStore.getState().error).toBeNull()
    })
  })

  describe('clearConversation', () => {
    it('resets messages and error, keeps context', () => {
      useAssistantStore.getState().setContext(ctx)
      useAssistantStore.getState().addMessage(msg)
      useAssistantStore.getState().setError('some error')
      useAssistantStore.getState().clearConversation()
      expect(useAssistantStore.getState().messages).toHaveLength(0)
      expect(useAssistantStore.getState().error).toBeNull()
      expect(useAssistantStore.getState().context).toEqual(ctx)
    })
  })
})
