import { requestJson } from './apiClient';

export const ReportService = {
    async getOverview({ tenant_id, start_at, end_at, granularity } = {}) {
        const iso = (d) => {
            if (!d) return undefined;
            const date = d instanceof Date ? d : new Date(d);
            if (Number.isNaN(date.getTime())) return undefined;
            return date.toISOString();
        };

        const query = {
            tenant_id,
            start_at: iso(start_at),
            end_at: iso(end_at),
            granularity
        };
        return requestJson('/super/v1/reports/overview', { query });
    },
    async exportReport({ tenant_id, start_at, end_at, granularity, format } = {}) {
        const iso = (d) => {
            if (!d) return undefined;
            const date = d instanceof Date ? d : new Date(d);
            if (Number.isNaN(date.getTime())) return undefined;
            return date.toISOString();
        };

        return requestJson('/super/v1/reports/export', {
            method: 'POST',
            body: {
                tenant_id,
                start_at: iso(start_at),
                end_at: iso(end_at),
                granularity,
                format: format || 'csv'
            }
        });
    }
};
