// Shared by the /internal/announce webhook (site publishes a news post) and
// the manual /announce command (staff re-posts/boosts an existing one) — so
// both paths produce an identical embed.
const { EmbedBuilder } = require('discord.js')

const guildConfig = require('../model/guildConfig')
const brand = require('../brand')
const createLogger = require('../utils/logger')

const log = createLogger('news')

async function postAnnounce(client, guildId, { title, excerpt, url, imageUrl }) {
  const channelId = await guildConfig.getNewsChannelId(guildId)
  if (!channelId) throw new Error('No news channel configured — set one with /news first.')

  const channel = await client.channels.fetch(channelId)
  if (!channel || !channel.isTextBased()) throw new Error('Configured news channel is missing or not text-based.')

  const embed = new EmbedBuilder().setColor(brand.accentInt).setTitle(title).setURL(url)
  if (excerpt) embed.setDescription(excerpt)
  if (imageUrl) embed.setImage(imageUrl)

  await channel.send({ embeds: [embed] })
  log.info('news announced', { title, channelId })
}

module.exports = { postAnnounce }
