/** reportsService — Previously called the Python backend for report generation.
 *  Reports are now generated directly from Supabase queries in the AdminReports view. */
import { supabase } from '@/supabase';

export interface Report {
    id: string;
    user_id: string;
    user_name?: string;
    message: string;
    status: 'pending' | 'resolved' | 'archived';
    created_at: string;
}

export const reportsService = {
    async getReports(): Promise<Report[]> {
        const { data, error } = await supabase.from('reports').select('*').order('created_at', { ascending: false })
        if (error) {
            console.warn('Reports table might not exist or error fetching', error)
            return []
        }
        return data as Report[]
    },

    async updateReportStatus(id: string, status: string): Promise<void> {
        const { error } = await supabase.from('reports').update({ status }).eq('id', id)
        if (error) throw new Error(error.message)
    },

    async sendReport(message: string): Promise<void> {
        const { error } = await supabase.from('reports').insert([
            { message, status: 'pending' }
        ]);
        if (error) throw new Error(error.message);
    },

    async getRoutesReport() {
        const { data, error } = await supabase.from('routes').select('*')
        if (error) throw new Error(error.message)
        return data
    },

    async getUsersReport() {
        const { data, error } = await supabase.from('users').select('id, email, role, created_at, is_active')
        if (error) throw new Error(error.message)
        return data
    }
}
