// /share command - Share the current session as a public URL.
import { ChannelType, MessageFlags, } from 'discord.js';
import { getThreadSession } from '../database.js';
import { initializeOpencodeForDirectory } from '../opencode.js';
import { resolveWorkingDirectory, SILENT_MESSAGE_FLAGS, } from '../discord-utils.js';
import { createLogger, LogPrefix } from '../logger.js';
const logger = createLogger(LogPrefix.SHARE);
export async function handleShareCommand({ command, }) {
    const channel = command.channel;
    if (!channel) {
        await command.reply({
            content: 'This command can only be used in a channel',
            flags: MessageFlags.Ephemeral | SILENT_MESSAGE_FLAGS,
        });
        return;
    }
    const isThread = [
        ChannelType.PublicThread,
        ChannelType.PrivateThread,
        ChannelType.AnnouncementThread,
    ].includes(channel.type);
    if (!isThread) {
        await command.reply({
            content: 'This command can only be used in a thread with an active session',
            flags: MessageFlags.Ephemeral | SILENT_MESSAGE_FLAGS,
        });
        return;
    }
    const resolved = await resolveWorkingDirectory({
        channel: channel,
    });
    if (!resolved) {
        await command.reply({
            content: 'Could not determine project directory for this channel',
            flags: MessageFlags.Ephemeral | SILENT_MESSAGE_FLAGS,
        });
        return;
    }
    const { projectDirectory } = resolved;
    const sessionId = await getThreadSession(channel.id);
    if (!sessionId) {
        await command.reply({
            content: 'No active session in this thread',
            flags: MessageFlags.Ephemeral | SILENT_MESSAGE_FLAGS,
        });
        return;
    }
    const getClient = await initializeOpencodeForDirectory(projectDirectory);
    if (getClient instanceof Error) {
        await command.reply({
            content: `Failed to share session: ${getClient.message}`,
            flags: MessageFlags.Ephemeral | SILENT_MESSAGE_FLAGS,
        });
        return;
    }
    try {
        const response = await getClient().session.share({
            sessionID: sessionId,
        });
        if (!response.data?.share?.url) {
            await command.reply({
                content: 'Failed to generate share URL',
                flags: MessageFlags.Ephemeral | SILENT_MESSAGE_FLAGS,
            });
            return;
        }
        await command.reply({
            content: `🔗 **Session shared:** ${response.data.share.url}`,
            flags: SILENT_MESSAGE_FLAGS,
        });
        logger.log(`Session ${sessionId} shared: ${response.data.share.url}`);
    }
    catch (error) {
        logger.error('[SHARE] Error:', error);
        await command.reply({
            content: `Failed to share session: ${error instanceof Error ? error.message : 'Unknown error'}`,
            flags: MessageFlags.Ephemeral | SILENT_MESSAGE_FLAGS,
        });
    }
}
