"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAudioList = getAudioList;
exports.getAudioById = getAudioById;
exports.deleteAudio = deleteAudio;
exports.toggleFavorite = toggleFavorite;
exports.updateAudio = updateAudio;
exports.getUserStats = getUserStats;
const models_1 = require("../../models");
// 获取用户音频列表
async function getAudioList(userId, options) {
    const { page = 1, pageSize = 10, isFavorite, keyword, category } = options;
    // 构建查询条件
    const where = {};
    // 只有提供了有效的 userId 才添加用户过滤条件
    if (userId) {
        where.userId = parseInt(userId);
    }
    if (isFavorite !== undefined) {
        where.isFavorite = isFavorite;
    }
    if (keyword) {
        where.OR = [
            { title: { contains: keyword } },
            { text: { contains: keyword } },
        ];
    }
    if (category) {
        where.category = category;
    }
    const total = await models_1.prisma.audio.count({ where });
    const totalPages = Math.ceil(total / pageSize);
    const list = await models_1.prisma.audio.findMany({
        where,
        orderBy: { createdAt: 'desc' },
        skip: (page - 1) * pageSize,
        take: pageSize,
    });
    return {
        list,
        total,
        page,
        pageSize,
        totalPages,
    };
}
// 获取单个音频
async function getAudioById(audioId, userId) {
    const id = parseInt(audioId);
    if (isNaN(id)) {
        return null;
    }
    // 如果有有效的 userId，添加用户过滤
    if (userId) {
        return models_1.prisma.audio.findFirst({
            where: { id, userId: parseInt(userId) },
        });
    }
    // 没有 userId，返回该音频（允许匿名访问）
    return models_1.prisma.audio.findUnique({ where: { id } });
}
// 删除音频
async function deleteAudio(audioId, userId) {
    // 验证 userId 是否为有效
    if (!userId) {
        return false;
    }
    const id = parseInt(audioId);
    if (isNaN(id)) {
        return false;
    }
    try {
        await models_1.prisma.audio.delete({
            where: { id, userId: parseInt(userId) },
        });
        return true;
    }
    catch {
        return false;
    }
}
// 切换收藏状态
async function toggleFavorite(audioId, userId) {
    // 验证 userId 是否为有效
    if (!userId) {
        return null;
    }
    const id = parseInt(audioId);
    if (isNaN(id)) {
        return null;
    }
    const audio = await models_1.prisma.audio.findFirst({
        where: { id, userId: parseInt(userId) },
    });
    if (!audio)
        return null;
    const updated = await models_1.prisma.audio.update({
        where: { id: audio.id },
        data: { isFavorite: !audio.isFavorite },
    });
    return updated;
}
// 更新音频信息
async function updateAudio(audioId, userId, data) {
    // 验证 userId 是否为有效
    if (!userId) {
        return null;
    }
    const id = parseInt(audioId);
    if (isNaN(id)) {
        return null;
    }
    const audio = await models_1.prisma.audio.findFirst({
        where: { id, userId: parseInt(userId) },
    });
    if (!audio)
        return null;
    const updated = await models_1.prisma.audio.update({
        where: { id: audio.id },
        data: {
            ...(data.title && { title: data.title }),
            ...(data.tags && { tags: JSON.stringify(data.tags) }),
        },
    });
    return updated;
}
// 获取用户使用统计
async function getUserStats(userId) {
    // 验证 userId 是否为有效
    if (!userId) {
        return { totalAudios: 0, totalDuration: 0, totalWords: 0, favoriteCount: 0 };
    }
    const uid = parseInt(userId);
    if (isNaN(uid)) {
        return { totalAudios: 0, totalDuration: 0, totalWords: 0, favoriteCount: 0 };
    }
    const totalAudios = await models_1.prisma.audio.count({ where: { userId: uid } });
    const allAudios = await models_1.prisma.audio.findMany({
        where: { userId: uid },
        select: {
            audioDuration: true,
            wordCount: true,
            isFavorite: true,
        },
    });
    const totalDuration = allAudios.reduce((sum, a) => sum + a.audioDuration, 0);
    const totalWords = allAudios.reduce((sum, a) => sum + a.wordCount, 0);
    const favoriteCount = allAudios.filter(a => a.isFavorite).length;
    return {
        totalAudios,
        totalDuration,
        totalWords,
        favoriteCount,
    };
}
