import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js';
import { SlashCommand } from '../../utils/types';
import { context } from '../../config/context';
import { prisma } from '../../database/index';
import path from 'path';
import fs from 'fs';

const command: SlashCommand = {
  guildOnly: true,
  data: new SlashCommandBuilder().setName('transcript').setDescription('Exportiert das Transcript dieses Tickets.'),
  async execute(interaction: ChatInputCommandInteraction) {
    const channelId = interaction.channelId;
    const ticket = await prisma.ticket.findFirst({ where: { channelId } });
    if (!ticket) {
      await interaction.reply({ content: 'Kein Ticket in diesem Kanal.', ephemeral: true });
      return;
    }

    const filePath = await context.tickets.exportTranscript(interaction.channel!, ticket.id);
    const fileName = path.basename(filePath);
    await interaction.reply({ content: `Transcript exportiert: ${fileName}`, files: [fs.createReadStream(filePath)] });
  }
};

export default command;
