"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isAdmin = isAdmin;
exports.handleAdminDashboard = handleAdminDashboard;
exports.handleBroadcast = handleBroadcast;
exports.handleStats = handleStats;
exports.handleLeads = handleLeads;
const grammy_1 = require("grammy");
const User_1 = require("../models/User");
const Lead_1 = require("../models/Lead");
const config_1 = require("../config");
function isAdmin(ctx) {
    const adminIds = config_1.config.ADMIN_CHAT_ID.split(",").map((id) => id.trim());
    return adminIds.includes(String(ctx.from?.id));
}
async function handleAdminDashboard(ctx) {
    if (!isAdmin(ctx)) {
        await ctx.reply("⛔ Anda tidak memiliki akses admin.");
        return;
    }
    await ctx.reply(`🛡 *Admin Dashboard — JD Technolody*\n\nPilih aksi:`, {
        parse_mode: "Markdown",
        reply_markup: new grammy_1.InlineKeyboard()
            .text("📊 Statistik", "admin_stats")
            .text("📋 Leads Baru", "admin_leads").row()
            .text("📤 Broadcast", "admin_broadcast_prompt").row(),
    });
}
async function handleBroadcast(ctx) {
    if (!isAdmin(ctx)) {
        await ctx.reply("⛔ Anda tidak memiliki akses admin.");
        return;
    }
    const text = ctx.message?.text?.replace("/broadcast", "").trim();
    if (!text) {
        await ctx.reply("Format: `/broadcast <pesan>`\nContoh: `/broadcast 🎉 Promo diskon 30% bulan ini!`", { parse_mode: "Markdown" });
        return;
    }
    const users = User_1.UserModel.getAllSubscribed();
    await ctx.reply(`📤 Broadcast ke ${users.length} user dimulai...`);
    let success = 0;
    let failed = 0;
    for (const user of users) {
        try {
            await ctx.api.sendMessage(user.telegram_id, `📢 *Update dari JD Technolody*\n\n${text}`, { parse_mode: "Markdown" });
            success++;
        }
        catch (_) {
            failed++;
        }
        await new Promise((r) => setTimeout(r, 50));
    }
    await ctx.reply(`✅ Broadcast selesai!\n✅ Berhasil: *${success}*\n❌ Gagal: *${failed}*`, { parse_mode: "Markdown" });
}
async function handleStats(ctx) {
    if (!isAdmin(ctx)) {
        await ctx.reply("⛔ Anda tidak memiliki akses admin.");
        return;
    }
    const totalUsers = User_1.UserModel.countAll();
    const subscribedUsers = User_1.UserModel.countSubscribed();
    const todayUsers = User_1.UserModel.countToday();
    const totalLeads = Lead_1.LeadModel.countAll();
    const newLeads = Lead_1.LeadModel.countByStatus("new");
    const hotLeads = Lead_1.LeadModel.countHot();
    const todayLeads = Lead_1.LeadModel.countToday();
    await ctx.reply(`📊 *Statistik Bot JD Technolody*\n\n` +
        `👥 Total User: *${totalUsers}*\n` +
        `🔔 User Subscribe: *${subscribedUsers}*\n` +
        `🆕 User Baru Hari ini: *${todayUsers}*\n\n` +
        `📋 Total Leads: *${totalLeads}*\n` +
        `🆕 Leads Baru: *${newLeads}*\n` +
        `🔥 Hot Leads: *${hotLeads}*\n` +
        `📅 Leads Hari ini: *${todayLeads}*`, { parse_mode: "Markdown" });
}
async function handleLeads(ctx) {
    if (!isAdmin(ctx)) {
        await ctx.reply("⛔ Anda tidak memiliki akses admin.");
        return;
    }
    const leads = Lead_1.LeadModel.getNewLeads(10);
    if (leads.length === 0) {
        await ctx.reply("✅ Tidak ada leads baru.");
        return;
    }
    for (const lead of leads) {
        const hotBadge = lead.is_hot ? "🔥 *HOT LEAD*\n" : "";
        const msg = `${hotBadge}` +
            `👤 *${lead.name}*\n` +
            `📱 @${lead.username || "-"} (ID: ${lead.telegram_id})\n` +
            `📞 WA: ${lead.phone_number || "-"}\n` +
            `🛠 ${lead.service_type} | 💰 ${lead.budget}\n` +
            `📌 Project: ${lead.project_name}\n` +
            `⏰ ${lead.created_at}`;
        await ctx.reply(msg, {
            parse_mode: "Markdown",
            reply_markup: new grammy_1.InlineKeyboard()
                .url("💬 Chat", `tg://user?id=${lead.telegram_id}`)
                .url("📞 WA", `https://wa.me/${lead.phone_number?.replace(/[^0-9]/g, "") || "0"}`),
        });
    }
}
