import AsyncStorage from '@react-native-async-storage/async-storage';

const VIEWED_NOTIFICATIONS_KEY = '@viewed_notifications';

interface ViewedNotification {
    id: string;
    type: 'reservation' | 'promotion';
    viewedAt: string;
}

export const notificationStorage = {
    async markAsViewed(id: string, type: 'reservation' | 'promotion'): Promise<void> {
        try {
            const existing = await this.getViewedNotifications();
            const newViewed: ViewedNotification = {
                id,
                type,
                viewedAt: new Date().toISOString()
            };

            const updated = [...existing, newViewed];
            await AsyncStorage.setItem(VIEWED_NOTIFICATIONS_KEY, JSON.stringify(updated));
        } catch (error) {
            console.error('Error marking notification as viewed:', error);
        }
    },

    async getViewedNotifications(): Promise<ViewedNotification[]> {
        try {
            const viewed = await AsyncStorage.getItem(VIEWED_NOTIFICATIONS_KEY);
            return viewed ? JSON.parse(viewed) : [];
        } catch (error) {
            console.error('Error getting viewed notifications:', error);
            return [];
        }
    },

    async isNotificationViewed(id: string): Promise<boolean> {
        const viewed = await this.getViewedNotifications();
        return viewed.some((notification) => notification.id === id);
    },

    async seeAllCurrentStorage() {
        const viewed = await this.getViewedNotifications();
        return viewed;
    },
    async clearStorage() {
        await AsyncStorage.removeItem(VIEWED_NOTIFICATIONS_KEY);
    }
};
