import { describe, it, expect, beforeEach, vi } from 'vitest'
import { setActivePinia, createPinia } from 'pinia'
import { useAuthStore } from './auth'
import { OrganizationalLevel } from '@/types'

// Mock the auth API
vi.mock('@/api/auth', () => ({
  authApi: {
    login: vi.fn(),
    logout: vi.fn(),
    getProfile: vi.fn()
  }
}))

describe('Auth Store', () => {
  beforeEach(() => {
    setActivePinia(createPinia())
    localStorage.clear()
  })

  it('should initialize with no user and no token', () => {
    const store = useAuthStore()
    expect(store.user).toBeNull()
    expect(store.isAuthenticated).toBe(false)
  })

  it('should return correct organizational level when user is set', () => {
    const store = useAuthStore()
    store.user = {
      id: 1,
      username: 'test',
      displayName: 'Test User',
      organizationalUnitId: 1,
      organizationalUnitName: 'Division 1',
      organizationalLevel: OrganizationalLevel.Division
    }
    expect(store.organizationalLevel).toBe(OrganizationalLevel.Division)
  })

  it('should return level 4 when no user is set', () => {
    const store = useAuthStore()
    expect(store.organizationalLevel).toBe(4)
  })

  it('should correctly determine canManageSubordinates for Division level', () => {
    const store = useAuthStore()
    store.user = {
      id: 1,
      username: 'test',
      displayName: 'Test User',
      organizationalUnitId: 1,
      organizationalUnitName: 'Division 1',
      organizationalLevel: OrganizationalLevel.Division
    }
    expect(store.canManageSubordinates).toBe(true)
  })

  it('should correctly determine canManageSubordinates for Company level', () => {
    const store = useAuthStore()
    store.user = {
      id: 1,
      username: 'test',
      displayName: 'Test User',
      organizationalUnitId: 1,
      organizationalUnitName: 'Company 1',
      organizationalLevel: OrganizationalLevel.Company
    }
    expect(store.canManageSubordinates).toBe(false)
  })

  it('should correctly determine canCreateAllocations for Division level', () => {
    const store = useAuthStore()
    store.user = {
      id: 1,
      username: 'test',
      displayName: 'Test User',
      organizationalUnitId: 1,
      organizationalUnitName: 'Division 1',
      organizationalLevel: OrganizationalLevel.Division
    }
    expect(store.canCreateAllocations).toBe(true)
  })

  it('should correctly determine canCreateAllocations for non-Division level', () => {
    const store = useAuthStore()
    store.user = {
      id: 1,
      username: 'test',
      displayName: 'Test User',
      organizationalUnitId: 1,
      organizationalUnitName: 'Regiment 1',
      organizationalLevel: OrganizationalLevel.Regiment
    }
    expect(store.canCreateAllocations).toBe(false)
  })

  it('should correctly check hasPermission', () => {
    const store = useAuthStore()
    store.user = {
      id: 1,
      username: 'test',
      displayName: 'Test User',
      organizationalUnitId: 1,
      organizationalUnitName: 'Regiment 1',
      organizationalLevel: OrganizationalLevel.Regiment
    }
    
    // Regiment (level 2) should have permission for level 2 and above
    expect(store.hasPermission(1)).toBe(false) // Division
    expect(store.hasPermission(2)).toBe(true)  // Regiment
    expect(store.hasPermission(3)).toBe(true)  // Battalion
    expect(store.hasPermission(4)).toBe(true)  // Company
  })

  it('should clear user data on logout', async () => {
    const store = useAuthStore()
    store.user = {
      id: 1,
      username: 'test',
      displayName: 'Test User',
      organizationalUnitId: 1,
      organizationalUnitName: 'Division 1',
      organizationalLevel: OrganizationalLevel.Division
    }
    store.token = 'test-token'
    localStorage.setItem('token', 'test-token')
    
    await store.logout()
    
    expect(store.user).toBeNull()
    expect(store.token).toBeNull()
    expect(localStorage.getItem('token')).toBeNull()
  })
})
