import { Request, Response } from 'express';
import { supabase } from '../config/supabase.js';

export const notificationController = {
    getUserNotifications: async (req: Request, res: Response) => {
        try {
            const organizationId = req.headers['x-organization-id'] as string || req.appUser?.organizationId;

            if (!organizationId) {
                return res.json([]); // Return empty instead of error
            }

            const { data: notifications, error } = await supabase
                .from('notifications')
                .select('*')
                .eq('organization_id', organizationId)
                .order('created_at', { ascending: false });

            if (error && error.code !== '42P01') throw error;
            res.json(notifications || []);
        } catch (error) {
            console.error(error);
            res.json([]);
        }
    },

    markAsRead: async (req: Request, res: Response) => {
        try {
            const { id } = req.params;
            const { error } = await supabase
                .from('notifications')
                .update({ is_read: true })
                .eq('id', id);

            if (error && error.code !== '42P01') throw error;
            res.json({ success: true });
        } catch (error) {
            console.error(error);
            res.json({ success: true });
        }
    },

    markAllAsRead: async (req: Request, res: Response) => {
        try {
            const organizationId = req.headers['x-organization-id'] as string || req.appUser?.organizationId;

            if (!organizationId) {
                return res.json({ success: true });
            }

            const { error } = await supabase
                .from('notifications')
                .update({ is_read: true })
                .eq('organization_id', organizationId);

            if (error && error.code !== '42P01') throw error;
            res.json({ success: true });
        } catch (error) {
            console.error(error);
            res.json({ success: true });
        }
    },

    clearAll: async (req: Request, res: Response) => {
        try {
            const organizationId = req.headers['x-organization-id'] as string || req.appUser?.organizationId;

            if (!organizationId) {
                return res.json({ success: true });
            }

            const { error } = await supabase
                .from('notifications')
                .delete()
                .eq('organization_id', organizationId);

            if (error && error.code !== '42P01') throw error;
            res.json({ success: true });
        } catch (error) {
            console.error(error);
            res.json({ success: true });
        }
    },

    archive: async (req: Request, res: Response) => {
        try {
            const { id } = req.params;
            const { error } = await supabase
                .from('notifications')
                .update({ is_archived: true })
                .eq('id', id);

            if (error && error.code !== '42P01') throw error;
            res.json({ success: true });
        } catch (error) {
            console.error(error);
            res.json({ success: true });
        }
    },

    delete: async (req: Request, res: Response) => {
        try {
            const { id } = req.params;
            const { error } = await supabase
                .from('notifications')
                .delete()
                .eq('id', id);

            if (error && error.code !== '42P01') throw error;
            res.json({ success: true });
        } catch (error) {
            console.error(error);
            res.json({ success: true });
        }
    }
};