// static/admin/js/dashboard.js

const dashLog = window.LogConfig.createLogger('prospecting-dashboard');

function prospectingDashboard() {
    return {
        ...data(),

        currentPage: 'prospecting-dashboard',

        stats: {
            total_prospects: 0,
            digital_count: 0,
            offline_count: 0,
            top_priority: 0,
            avg_score: null,
            leads_by_tier: {},
            common_issues: [],
        },
        loading: true,
        error: null,
        showImportModal: false,

        async init() {
            await I18n.loadModule('prospecting');

            if (window._prospectingDashboardInit) return;
            window._prospectingDashboardInit = true;

            dashLog.info('Dashboard initializing');
            await this.loadStats();
        },

        async loadStats() {
            this.loading = true;
            this.error = null;
            try {
                const response = await apiClient.get('/admin/prospecting/stats');
                Object.assign(this.stats, response);
                dashLog.info('Stats loaded', this.stats);
            } catch (err) {
                this.error = err.message;
                dashLog.error('Failed to load stats', err);
            } finally {
                this.loading = false;
            }
        },

        async runBatchScan() {
            try {
                await apiClient.post('/admin/prospecting/enrichment/http-check/batch');
                Utils.showToast('Batch scan started', 'success');
            } catch (err) {
                Utils.showToast('Failed to start batch scan: ' + err.message, 'error');
            }
        },

        tierBadgeClass(tier) {
            const classes = {
                top_priority: 'text-red-700 bg-red-100 dark:text-red-100 dark:bg-red-700',
                quick_win: 'text-orange-700 bg-orange-100 dark:text-orange-100 dark:bg-orange-700',
                strategic: 'text-blue-700 bg-blue-100 dark:text-blue-100 dark:bg-blue-700',
                low_priority: 'text-gray-700 bg-gray-100 dark:text-gray-100 dark:bg-gray-700',
            };
            return classes[tier] || classes.low_priority;
        },

        issueLabel(flag) {
            const labels = {
                no_ssl: 'No SSL/HTTPS',
                very_slow: 'Very Slow',
                slow: 'Slow',
                not_mobile_friendly: 'Not Mobile Friendly',
                outdated_cms: 'Outdated CMS',
                unknown_cms: 'Unknown CMS',
                legacy_js: 'Legacy JavaScript',
                no_analytics: 'No Analytics',
                no_website: 'No Website',
                uses_gmail: 'Uses Gmail',
            };
            return labels[flag] || flag.replace(/_/g, ' ');
        },
    };
}
