export interface CookiePreferences {
  essential: boolean;
  analytics: boolean;
  marketing: boolean;
  preferences: boolean;
  timestamp: string;
  version: string;
}

const COOKIE_CONSENT_KEY = 'bakery_cookie_consent';
const COOKIE_CONSENT_VERSION = '1.0';

export const getCookieConsent = (): CookiePreferences | null => {
  try {
    const stored = localStorage.getItem(COOKIE_CONSENT_KEY);
    if (!stored) return null;

    const consent = JSON.parse(stored) as CookiePreferences;

    if (consent.version !== COOKIE_CONSENT_VERSION) {
      return null;
    }

    return consent;
  } catch (error) {
    console.error('Error reading cookie consent:', error);
    return null;
  }
};

export const saveCookieConsent = (preferences: CookiePreferences): void => {
  try {
    localStorage.setItem(COOKIE_CONSENT_KEY, JSON.stringify(preferences));

    initializeAnalytics(preferences);
    initializeMarketing(preferences);
  } catch (error) {
    console.error('Error saving cookie consent:', error);
  }
};

export const clearCookieConsent = (): void => {
  try {
    localStorage.removeItem(COOKIE_CONSENT_KEY);
  } catch (error) {
    console.error('Error clearing cookie consent:', error);
  }
};

export const hasConsent = (category: keyof Omit<CookiePreferences, 'timestamp' | 'version'>): boolean => {
  const consent = getCookieConsent();
  if (!consent) return false;
  return consent[category];
};

const initializeAnalytics = (preferences: CookiePreferences): void => {
  if (preferences.analytics) {
    console.log('Analytics cookies enabled');
  } else {
    console.log('Analytics cookies disabled');
  }
};

const initializeMarketing = (preferences: CookiePreferences): void => {
  if (preferences.marketing) {
    console.log('Marketing cookies enabled');
  } else {
    console.log('Marketing cookies disabled');
  }
};

export const getCookieCategories = () => [
  {
    id: 'essential',
    name: 'Essential Cookies',
    description: 'Required for the website to function. These cookies enable core functionality such as security, authentication, and session management. They cannot be disabled.',
    required: true,
    examples: ['Session tokens', 'Authentication cookies', 'Security tokens']
  },
  {
    id: 'preferences',
    name: 'Preference Cookies',
    description: 'Allow the website to remember information that changes the way the website behaves or looks, such as your preferred language or region.',
    required: false,
    examples: ['Language preference', 'Theme preference', 'Region settings']
  },
  {
    id: 'analytics',
    name: 'Analytics Cookies',
    description: 'Help us understand how visitors interact with our website by collecting and reporting information anonymously.',
    required: false,
    examples: ['Google Analytics', 'Page views', 'User behavior tracking']
  },
  {
    id: 'marketing',
    name: 'Marketing Cookies',
    description: 'Used to track visitors across websites to display relevant advertisements and marketing campaigns.',
    required: false,
    examples: ['Advertising cookies', 'Retargeting pixels', 'Social media cookies']
  }
];
