const CONSENT_KEY = 'decoda_consent';
const CONSENT_VERSION = '1';

export const ConsentManager = {
  getConsent() {
    const stored = localStorage.getItem(CONSENT_KEY);
    if (!stored) return null;

    try {
      const data = JSON.parse(stored);
      return data.version === CONSENT_VERSION ? data : null;
    } catch {
      return null;
    }
  },

  setConsent(accepted) {
    const consent = {
      version: CONSENT_VERSION,
      accepted,
      timestamp: new Date().toISOString(),
    };
    localStorage.setItem(CONSENT_KEY, JSON.stringify(consent));
  },

  hasConsent() {
    const consent = this.getConsent();
    return consent ? consent.accepted : false;
  },

  hasUserDecided() {
    return this.getConsent() !== null;
  },

  reset() {
    localStorage.removeItem(CONSENT_KEY);
  },
};
