import type { PrismaClient } from '@prisma/client';
import type { WelcomeEmbed, WelcomeMessageType } from '@nexumi/shared';
import { applyWelcomePlaceholders } from '@nexumi/shared';
import type { Guild, GuildMember } from 'discord.js';
import { ensureGuild } from '../../guild.js';

export async function getWelcomeConfig(prisma: PrismaClient, guildId: string) {
  await ensureGuild(prisma, guildId);
  let config = await prisma.welcomeConfig.findUnique({ where: { guildId } });
  if (!config) {
    config = await prisma.welcomeConfig.create({ data: { guildId } });
  }
  return config;
}

export function buildPlaceholderVars(member: GuildMember, guild: Guild) {
  return {
    user: `<@${member.id}>`,
    'user.name': member.displayName,
    'user.tag': member.user.tag,
    'user.id': member.id,
    'user.avatar': member.user.displayAvatarURL({ size: 256 }),
    server: guild.name,
    memberCount: guild.memberCount,
    'server.icon': guild.iconURL({ size: 128 }) ?? ''
  };
}

export function renderWelcomeText(template: string, member: GuildMember, guild: Guild): string {
  return applyWelcomePlaceholders(template, buildPlaceholderVars(member, guild));
}

export function parseWelcomeEmbed(raw: unknown): WelcomeEmbed | null {
  if (!raw || typeof raw !== 'object') {
    return null;
  }
  return raw as WelcomeEmbed;
}

export type WelcomePayload = {
  type: WelcomeMessageType;
  content?: string | null;
  embed?: WelcomeEmbed | null;
  imageTitle?: string | null;
  imageSubtitle?: string | null;
};

export function resolveWelcomePayload(
  config: Awaited<ReturnType<typeof getWelcomeConfig>>
): WelcomePayload {
  return {
    type: config.welcomeType as WelcomeMessageType,
    content: config.welcomeContent,
    embed: parseWelcomeEmbed(config.welcomeEmbed),
    imageTitle: config.imageTitle,
    imageSubtitle: config.imageSubtitle
  };
}
