import { create } from 'zustand';
import { persist } from 'zustand/middleware';

export const useAuthStore = create(
  persist(
    (set) => ({
      user: null,
      token: null,
      isAuthenticated: false,
      mustChangePassword: false,
      login: (user, token) => set({ 
        user, 
        token, 
        isAuthenticated: true,
        mustChangePassword: user.must_change_password || false
      }),
      logout: () => set({ user: null, token: null, isAuthenticated: false, mustChangePassword: false }),
      updateUser: (user) => set({ user, mustChangePassword: user.must_change_password || false }),
      clearPasswordFlag: () => set({ mustChangePassword: false })
    }),
    {
      name: 'auth-storage'
    }
  )
);
