'use client';

import { create } from 'zustand';

export interface AuthUser {
  employeeId: string;
  name: string;
  branchId: string;
  branchName: string;
  isAdmin: boolean;
  dbId: string;
}

export type ViewType = 'login' | 'voting' | 'results' | 'admin';

interface AuthState {
  user: AuthUser | null;
  isAuthenticated: boolean;
  isLoading: boolean;
  currentView: ViewType;
  setUser: (user: AuthUser | null) => void;
  setIsLoading: (loading: boolean) => void;
  setCurrentView: (view: ViewType) => void;
  logout: () => void;
}

export const useAuthStore = create<AuthState>((set) => ({
  user: null,
  isAuthenticated: false,
  isLoading: true,
  currentView: 'login',
  setUser: (user) =>
    set({
      user,
      isAuthenticated: !!user,
      isLoading: false,
      currentView: user ? (user.isAdmin ? 'admin' : 'voting') : 'login',
    }),
  setIsLoading: (isLoading) => set({ isLoading }),
  setCurrentView: (currentView) => set({ currentView }),
  logout: () =>
    set({
      user: null,
      isAuthenticated: false,
      isLoading: false,
      currentView: 'login',
    }),
}));
