import { Context } from "grammy";
import { InlineKeyboard } from "grammy";
import { ServiceCategoryModel } from "../models/ServiceCategory";
import { ServiceModel, parseFeatures } from "../models/Service";
import { getServiceCategoriesMenu, getServicePackagesMenu } from "../menus/mainMenu";

/** Tampilkan menu 3 kategori: Web, APK, Bot */
export async function showServicesMenu(ctx: Context) {
  const categories = ServiceCategoryModel.getAll();
  if (ctx.callbackQuery) {
    try { await ctx.deleteMessage(); } catch (_) {}
  }
  await ctx.reply(
    `🛠 *Layanan JD Technolody*\n\nPilih kategori layanan:`,
    { parse_mode: "Markdown", reply_markup: getServiceCategoriesMenu(categories) }
  );
}

/** Tampilkan daftar paket dalam satu kategori */
export async function showServiceCategory(ctx: Context, categorySlug: string) {
  const category = ServiceCategoryModel.getBySlug(categorySlug);
  if (!category) return;

  const packages = ServiceModel.getByCategory(categorySlug);
  try { await ctx.deleteMessage(); } catch (_) {}

  if (packages.length === 0) {
    await ctx.reply(
      `${category.icon} *${category.name}*\n\nBelum ada paket tersedia.`,
      {
        parse_mode: "Markdown",
        reply_markup: new InlineKeyboard().text("🔙 Kembali", "services_menu"),
      }
    );
    return;
  }

  await ctx.reply(
    `${category.icon} *${category.name}*\n\n${category.description}\n\nPilih paket:`,
    { parse_mode: "Markdown", reply_markup: getServicePackagesMenu(packages, categorySlug) }
  );
}

/** Tampilkan detail satu paket */
export async function showServicePackage(ctx: Context, packageId: number) {
  const pkg = ServiceModel.getById(packageId);
  if (!pkg) return;

  const category = ServiceCategoryModel.getBySlug(pkg.category_slug);
  const features = parseFeatures(pkg.features);
  try { await ctx.deleteMessage(); } catch (_) {}

  let text = `${pkg.icon} *${pkg.name}*\n\n`;
  text += `${pkg.description}\n\n`;
  text += `💰 *Harga: ${pkg.price}*\n\n`;

  if (features.length > 0) {
    text += `✅ *Yang Anda dapatkan:*\n`;
    text += features.map((f) => `• ${f}`).join("\n");
    text += "\n\n";
  }

  text += `_Harga dapat berubah sesuai kompleksitas. Konsultasi gratis!_`;

  await ctx.reply(text, {
    parse_mode: "Markdown",
    reply_markup: new InlineKeyboard()
      .text("🤖 Konsultasi Sekarang", `consult_cat_${pkg.category_slug}`).row()
      .text("🔙 Paket Lainnya", `svc_back_${pkg.category_slug}`),
  });
}

/** Tampilkan harga ringkas semua kategori untuk menu 💰 Harga */
export function buildPriceText(): string {
  const categories = ServiceCategoryModel.getAll();
  let text = `💰 *Daftar Harga Layanan JD Technolody*\n\n`;

  for (const cat of categories) {
    const packages = ServiceModel.getByCategory(cat.slug);
    if (packages.length === 0) continue;
    text += `${cat.icon} *${cat.name}*\n`;
    text += packages.map((p) => `   • ${p.name}: *${p.price}*`).join("\n");
    text += "\n\n";
  }

  text += `_Harga dapat berubah sesuai kompleksitas fitur._`;
  return text;
}
