// Determine user handle (username or first name)
var hh = user.username ? "@" + user.username : user.first_name

// Set user property
User.setProperty("UserDone", true, "boolean")

// Initialize or update global status
var globalStatus = Libs.ResourcesLib.anotherChatRes("status", "global")

// Get or initialize the list of full bot users
var FullBotUser = Bot.getProperty("FullBotUser") || []
if (!FullBotUser.includes(hh)) {
  FullBotUser.push(hh)
  Bot.setProperty("FullBotUser", FullBotUser, "json")
}

// Format user name with link
var name =
  "<a href='tg://user?id=" +
  user.telegramid +
  "'><b>" +
  user.first_name +
  "</b></a>"

// Get or initialize user records
let totalUsers = Bot.getProperty("userRecords") || []
if (!totalUsers.some(u => u.id === user.telegramid)) {
  totalUsers.push({ id: user.telegramid })
  Bot.setProperty("userRecords", totalUsers, "json")
}
const totalUsersCount = totalUsers.length

// Get or initialize broadcast list
var broadcast = Bot.getProperty("Broadcast") || []
if (!broadcast.includes(user.telegramid)) {
  broadcast.push(user.telegramid)
  Bot.setProperty("Broadcast", broadcast, "json")
}

// Handle new user notification
if (!User.getProperty("UserDone")) {
  User.setProperty("UserDone", true, "boolean")
  Libs.ResourcesLib.anotherChatRes("status", "global").add(1)

  var isNewUser = !User.getProperty("seen")
  if (isNewUser) {
    var userCount = Libs.ResourcesLib.anotherChatRes("to", "global")
    userCount.add(1)

    var logsChannelId = "-1002078281897"
    var currentDate = new Date().toLocaleDateString("en-US", {
      timeZone: "Asia/Kolkata"
    })
    var currentTime = new Date().toLocaleTimeString("en-US", {
      timeZone: "Asia/Kolkata"
    })
    var botName = bot.name

    Api.sendMessage({
      chat_id: logsChannelId,
      text: `🥳 *New User Notification* 🔔\n\n*➲ User:* [${
        user.first_name
      }](tg://user?id=${user.telegramid})\n*➲ Username:* @${
        user.username
      }\n*➲ ID:* ${
        user.telegramid
      }\n\n*➲ Date:* ${currentDate}\n*➲ Time:* ${currentTime}\n*➲ Bot Name:* @${botName}\n*➲ Total Users:* ${userCount.value()}`,
      parse_mode: "Markdown"
    })

    User.setProperty("seen", true, "boolean")
  }
}

// Advertisement messages
var ads = [
  "@MaximXEmojis - Dive into a collection of expressive emojis for every mood! Join now and add flair to your conversations.",
  "@MaximXSticker - Discover vibrant and diverse sticker packs to enhance your messaging experience. Join us for a visual delight!",
  "@MaximXBots - Engage with cutting-edge bots designed for fun, utility, and more. Join the bot revolution and elevate your Telegram experience!",
  "@MaximXWallpaper - Immerse yourself in a gallery of stunning wallpapers to revamp your device's look. Join for a daily dose of aesthetic inspiration."
]
var randomAd = ads[Math.floor(Math.random() * ads.length)]

// Photo URLs
var photos = [
  "https://te.legra.ph/file/0325da0c1422acc1ee7a3.jpg",
  "https://te.legra.ph/file/a9f0df8afe53e8b8154a5.jpg",
  "https://te.legra.ph/file/7cf3fcf1778acc3184997.jpg",
  "https://te.legra.ph/file/b7322de19b8c94d3fc6f3.jpg",
  "https://te.legra.ph/file/95e7dc37f427a0a2e5e64.jpg",
  "https://te.legra.ph/file/c1a5426d0e0fc8e893c99.jpg",
  "https://te.legra.ph/file/24465e3cbd9767aab29ea.jpg",
  "https://te.legra.ph/file/d9e3166c983d690d378c5.jpg",
  "https://te.legra.ph/file/67864aa1f1a1a79714a0f.jpg",
  "https://te.legra.ph/file/17dd53b6a5922431bf01e.jpg",
  "https://te.legra.ph/file/cb101461c47f25f088ed3.jpg",
  "https://te.legra.ph/file/2e0a00b2545ba2e8ff0cd.jpg",
  "https://te.legra.ph/file/86037156a878c0273df07.jpg",
  "https://te.legra.ph/file/1bcbec5bd64dd502afff2.jpg",
  "https://te.legra.ph/file/ed3d3b5fc1cdb6626c4a5.jpg",
  "https://te.legra.ph/file/19a6b6842a5e0e8a510e1.jpg",
  "https://te.legra.ph/file/af3f9a03186608f0d56cf.jpg",
  "https://te.legra.ph/file/3fd1351cbd218e1047773.jpg",
  "https://te.legra.ph/file/d3827183fdecbe0bf313f.jpg",
  "https://te.legra.ph/file/77e4388f1d68a6557755f.jpg",
  "https://te.legra.ph/file/f83b2a809d9424ffc6485.jpg",
  "https://te.legra.ph/file/230ee12a06cd0f42bbe63.jpg",
  "https://te.legra.ph/file/230ee12a06cd0f42bbe63.jpg"
]
var randomPhoto = photos[Math.floor(Math.random() * photos.length)]

// Caption with ad and bot link
var cap = `𝐊ᴏɴ'ɪᴄʜɪᴡᴀ ${name},

By Using This Bot, You Acknowledge You Are At Least 18 Years Old.
  
𝐀ᴅs: <a href='t.me/QuinxAds'>𝐐ᴜɪɴx 𝐀ᴅs</a><blockquote>\n${randomAd}</blockquote>`

// Inline keyboard buttons
var inlineKeyboard = [
  [
    { text: "Episode 01", url: "t.me/example.com" },
    { text: "Episode 02", url: "t.me/example.com" }
  ]
]

// Send or edit message with photo and caption
if (request.message && request.message.message_id) {
  Api.editMessageMedia({
    message_id: request.message.message_id,
    media: {
      type: "photo",
      media: randomPhoto,
      caption: cap,
      parse_mode: "HTML",
      protect_content: true
    },
    disable_web_page_preview: true,
    reply_markup: { inline_keyboard: inlineKeyboard }
  })
} else {
  Api.sendPhoto({
    chat_id: request.chat.id,
    photo: randomPhoto,
    caption: cap,
    parse_mode: "HTML",
    disable_web_page_preview: true,
    reply_markup: {
      inline_keyboard: inlineKeyboard
    }
  })
}