import { create } from "zustand" import { persist } from "zustand/middleware" interface WalletState { walletConnected: boolean walletAddress: string username: string | null isVerified: boolean country: string | null citizenshipVerified: boolean eligibilityVerified: boolean verificationStatus: string // "Unverified", "Verified", or "Fully Eligible" perspectivesSubmittedToday: number lastSubmissionDate: string | null trustScore: number dailyLimit: number connectWallet: (address?: string, username?: string, isVerified?: boolean) => void disconnectWallet: () => void incrementPerspectiveCount: () => void resetPerspectiveCount: () => void updateTrustScore: (newScore: number) => void updateDailyLimit: (newLimit: number) => void toggleVerification: () => void setVerificationDetails: (country: string, citizenshipVerified: boolean, eligibilityVerified: boolean, status: string) => void setCountry: (country: string) => void } export const useWalletStore = create()( persist( (set, get) => ({ walletConnected: false, walletAddress: "", username: null, isVerified: false, country: null, citizenshipVerified: false, eligibilityVerified: false, verificationStatus: "Unverified", perspectivesSubmittedToday: 0, lastSubmissionDate: null, trustScore: 20, // Default trust score for new users dailyLimit: 5, // Default daily limit for unverified users connectWallet: (address = "0x1a2...3b4c", username = "VoxPop User", isVerified = false) => set({ walletConnected: true, walletAddress: address, username, isVerified, // Set daily limit to unlimited (99) if verified dailyLimit: isVerified ? 99 : get().dailyLimit }), disconnectWallet: () => set({ walletConnected: false, walletAddress: "", username: null, isVerified: false, country: null, citizenshipVerified: false, eligibilityVerified: false, verificationStatus: "Unverified", perspectivesSubmittedToday: 0, lastSubmissionDate: null, trustScore: 20, dailyLimit: 5 }), incrementPerspectiveCount: () => { const currentDate = new Date().toDateString(); const { lastSubmissionDate, perspectivesSubmittedToday } = get(); // Reset count if it's a new day if (lastSubmissionDate !== currentDate) { set({ perspectivesSubmittedToday: 1, lastSubmissionDate: currentDate }); } else { set({ perspectivesSubmittedToday: perspectivesSubmittedToday + 1, lastSubmissionDate: currentDate }); } }, resetPerspectiveCount: () => { set({ perspectivesSubmittedToday: 0, lastSubmissionDate: null }); }, updateTrustScore: (newScore: number) => { const currentScore = get().trustScore; const updatedScore = Math.min(Math.max(newScore, 0), 100); // Clamp between 0-100 set({ trustScore: updatedScore }); // Update daily limit based on trust score if user is unverified if (!get().isVerified) { const newLimit = Math.floor(5 + (updatedScore / 20)); // 5 base + up to 5 more based on trust set({ dailyLimit: newLimit }); } }, updateDailyLimit: (newLimit: number) => { set({ dailyLimit: newLimit }); }, toggleVerification: () => { const currentVerificationStatus = get().isVerified; set({ isVerified: !currentVerificationStatus, // Update daily limit when toggling verification dailyLimit: !currentVerificationStatus ? 99 : Math.floor(5 + (get().trustScore / 20)) }); }, setVerificationDetails: (country: string, citizenshipVerified: boolean, eligibilityVerified: boolean, status: string) => { set({ country, citizenshipVerified, eligibilityVerified, verificationStatus: status, // Update isVerified based on citizenshipVerified (at minimum) isVerified: citizenshipVerified, // Update daily limit for verified users dailyLimit: citizenshipVerified ? 99 : get().dailyLimit }); }, setCountry: (country: string) => { set({ country }); } }), { name: "wallet-storage", }, ), )