import { describe, it, expect, beforeEach } from 'vitest'
import { setActivePinia, createPinia } from 'pinia'

import { useUserStore } from './user'

describe('useUserStore', () => {
  beforeEach(() => {
    setActivePinia(createPinia())
  })

  it('starts with no profile', () => {
    /**
     * Test that the user store initializes without profile data.
     *
     * Before fetching the user profile, the store should have
     * null profile and sensible defaults for computed properties.
     */
    const store = useUserStore()

    expect(store.profile).toBeNull()
    expect(store.displayName).toBe('Unknown')
    expect(store.avatarUrl).toBeUndefined()
    expect(store.hasStarterDeck).toBe(false)
    expect(store.linkedAccounts).toEqual([])
  })

  it('tracks loading state', () => {
    /**
     * Test that the loading state ref is accessible.
     *
     * Components need to check loading state to show spinners
     * while profile data is being fetched.
     */
    const store = useUserStore()

    expect(store.isLoading).toBe(false)
  })

  it('tracks error state', () => {
    /**
     * Test that the error state ref is accessible.
     *
     * Components need to display error messages when
     * profile operations fail.
     */
    const store = useUserStore()

    expect(store.error).toBeNull()
  })

  it('clear resets all profile state', () => {
    /**
     * Test that clear removes all profile data.
     *
     * When user logs out, clear() should reset the store
     * to its initial state.
     */
    const store = useUserStore()

    // Manually set profile (simulating fetchProfile success)
    store.profile = {
      id: 'user-123',
      displayName: 'Test User',
      avatarUrl: 'https://example.com/avatar.png',
      hasStarterDeck: true,
      createdAt: '2026-01-01T00:00:00Z',
      linkedAccounts: [
        {
          provider: 'google',
          providerUserId: 'google-123',
          email: 'test@example.com',
          linkedAt: '2026-01-01T00:00:00Z',
        },
      ],
    }
    store.error = 'Some error'

    store.clear()

    expect(store.profile).toBeNull()
    expect(store.error).toBeNull()
  })

  it('computed properties reflect profile data', () => {
    /**
     * Test that computed properties derive from profile correctly.
     *
     * Components use these computed properties for display,
     * so they must accurately reflect the profile data.
     */
    const store = useUserStore()

    store.profile = {
      id: 'user-123',
      displayName: 'Test User',
      avatarUrl: 'https://example.com/avatar.png',
      hasStarterDeck: true,
      createdAt: '2026-01-01T00:00:00Z',
      linkedAccounts: [
        {
          provider: 'discord',
          providerUserId: 'discord-456',
          email: null,
          linkedAt: '2026-01-01T00:00:00Z',
        },
      ],
    }

    expect(store.displayName).toBe('Test User')
    expect(store.avatarUrl).toBe('https://example.com/avatar.png')
    expect(store.hasStarterDeck).toBe(true)
    expect(store.linkedAccounts).toHaveLength(1)
    expect(store.linkedAccounts[0].provider).toBe('discord')
  })
})
