import { create } from 'zustand'
import { UserRole, WorkspaceType } from '../domain/enums'

export interface MockUser {
  id: string
  email: string
  name: string
  role: UserRole
  organizationId: string
  organizationName: string
  allowedWorkspaces: WorkspaceType[]
}

export const SessionStatus = {
  UNAUTHENTICATED: 'unauthenticated',
  AUTHENTICATED: 'authenticated',
  EXPIRED: 'expired',
  RESTRICTED: 'restricted',
  ONBOARDING: 'onboarding',
} as const
export type SessionStatus = typeof SessionStatus[keyof typeof SessionStatus]

interface SessionState {
  currentUser: MockUser | null
  activeOrganizationId: string | null
  isAuthenticated: boolean
  sessionStatus: SessionStatus
  login: (user: MockUser) => void
  logout: () => void
  switchOrganization: (organizationId: string) => void
  setSessionStatus: (status: SessionStatus) => void
}

const mockUser: MockUser = {
  id: 'user-001',
  email: 'admin@ideal-sharing.ch',
  name: 'Admin User',
  role: UserRole.ORGANIZATION_ADMIN,
  organizationId: 'org-wincasa',
  organizationName: 'Wincasa AG',
  allowedWorkspaces: [WorkspaceType.SUPPLY, WorkspaceType.DEMAND],
}

export const useSessionStore = create<SessionState>((set) => ({
  currentUser: mockUser,
  activeOrganizationId: mockUser.organizationId,
  isAuthenticated: true,
  sessionStatus: SessionStatus.AUTHENTICATED,
  login: (user) => set({
    currentUser: user,
    activeOrganizationId: user.organizationId,
    isAuthenticated: true,
    sessionStatus: SessionStatus.AUTHENTICATED,
  }),
  logout: () => set({
    currentUser: null,
    activeOrganizationId: null,
    isAuthenticated: false,
    sessionStatus: SessionStatus.UNAUTHENTICATED,
  }),
  switchOrganization: (organizationId) => set({ activeOrganizationId: organizationId }),
  setSessionStatus: (status) => set({ sessionStatus: status }),
}))
