import { z } from 'zod';

export const GiveawayStatusSchema = z.enum(['ACTIVE', 'PAUSED', 'ENDED']);
export type GiveawayStatus = z.infer<typeof GiveawayStatusSchema>;

export const TicketModeSchema = z.enum(['CHANNEL', 'THREAD']);
export type TicketMode = z.infer<typeof TicketModeSchema>;

export const TicketStatusSchema = z.enum(['OPEN', 'CLAIMED', 'CLOSED']);
export type TicketStatus = z.infer<typeof TicketStatusSchema>;

export const TicketPrioritySchema = z.enum(['LOW', 'NORMAL', 'HIGH', 'URGENT']);
export type TicketPriority = z.infer<typeof TicketPrioritySchema>;

export const SelfRoleModeSchema = z.enum(['BUTTONS', 'DROPDOWN', 'REACTIONS']);
export type SelfRoleMode = z.infer<typeof SelfRoleModeSchema>;

export const SelfRoleBehaviorSchema = z.enum(['NORMAL', 'UNIQUE', 'VERIFY', 'TEMPORARY']);
export type SelfRoleBehavior = z.infer<typeof SelfRoleBehaviorSchema>;

export const SelfRoleEntrySchema = z.object({
  roleId: z.string(),
  label: z.string(),
  emoji: z.string().optional(),
  description: z.string().optional()
});
export type SelfRoleEntry = z.infer<typeof SelfRoleEntrySchema>;

export const TagResponseTypeSchema = z.enum(['TEXT', 'EMBED', 'COMPONENTS_V2']);
export type TagResponseType = z.infer<typeof TagResponseTypeSchema>;

export const SuggestionStatusSchema = z.enum([
  'OPEN',
  'APPROVED',
  'DENIED',
  'CONSIDERED',
  'IMPLEMENTED'
]);
export type SuggestionStatus = z.infer<typeof SuggestionStatusSchema>;

import { expandBracketChannelMentions } from './phase2.js';

export function applyTagPlaceholders(
  template: string,
  vars: Record<string, string>
): string {
  const withVars = Object.entries(vars).reduce(
    (acc, [key, value]) => acc.replaceAll(`{${key}}`, value),
    template
  );
  return expandBracketChannelMentions(withVars);
}

export function pickRandomWinners<T>(items: T[], count: number): T[] {
  const pool = [...items];
  const winners: T[] = [];
  while (winners.length < count && pool.length > 0) {
    const index = Math.floor(Math.random() * pool.length);
    winners.push(pool.splice(index, 1)[0]!);
  }
  return winners;
}

export function escapeHtml(input: string): string {
  return input
    .replaceAll('&', '&amp;')
    .replaceAll('<', '&lt;')
    .replaceAll('>', '&gt;')
    .replaceAll('"', '&quot;');
}

export function buildTicketTranscriptHtml(input: {
  guildName: string;
  ticketId: string;
  openerTag: string;
  messages: Array<{ author: string; content: string; timestamp: string }>;
}): string {
  const rows = input.messages
    .map(
      (m) =>
        `<div class="msg"><span class="meta">${escapeHtml(m.timestamp)} — ${escapeHtml(m.author)}</span><div>${escapeHtml(m.content)}</div></div>`
    )
    .join('\n');
  return `<!DOCTYPE html><html><head><meta charset="utf-8"/><title>Ticket ${escapeHtml(input.ticketId)}</title>
<style>body{font-family:sans-serif;background:#111;color:#eee;padding:1rem}.msg{margin:.75rem 0;padding:.5rem;border-left:3px solid #6366f1}.meta{color:#9ca3af;font-size:.85rem}</style>
</head><body><h1>Ticket ${escapeHtml(input.ticketId)}</h1><p>${escapeHtml(input.guildName)} — ${escapeHtml(input.openerTag)}</p>${rows}</body></html>`;
}
