"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPlayProgress = getPlayProgress;
exports.savePlayProgress = savePlayProgress;
exports.deletePlayRecord = deletePlayRecord;
exports.updatePlayProgress = updatePlayProgress;
exports.getSingleProgress = getSingleProgress;
const client_1 = require("@prisma/client");
const prisma = new client_1.PrismaClient();
/**
 * 获取用户播放记录
 */
async function getPlayProgress(userId, chapterId) {
    const userIdNum = parseInt(userId);
    const where = { userId: userIdNum };
    if (chapterId) {
        where.chapterId = chapterId;
    }
    const records = await prisma.playRecord.findMany({
        where,
        include: {
            chapter: {
                select: {
                    id: true,
                    number: true,
                    title: true,
                    audioUrl: true,
                    audioDuration: true,
                },
            },
        },
        orderBy: { updatedAt: 'desc' },
    });
    return records;
}
/**
 * 保存播放进度
 */
async function savePlayProgress(userId, chapterId, progress, duration) {
    const userIdNum = parseInt(userId);
    // 使用 upsert 语义：如果不存在则创建，存在则更新
    const existing = await prisma.playRecord.findUnique({
        where: {
            userId_chapterId: {
                userId: userIdNum,
                chapterId,
            },
        },
    });
    if (existing) {
        // 更新
        return await prisma.playRecord.update({
            where: {
                userId_chapterId: {
                    userId: userIdNum,
                    chapterId,
                },
            },
            data: {
                progress,
                duration,
            },
        });
    }
    else {
        // 创建
        return await prisma.playRecord.create({
            data: {
                userId: userIdNum,
                chapterId,
                progress,
                duration,
            },
        });
    }
}
/**
 * 删除播放记录
 */
async function deletePlayRecord(userId, chapterId) {
    const userIdNum = parseInt(userId);
    return await prisma.playRecord.delete({
        where: {
            userId_chapterId: {
                userId: userIdNum,
                chapterId,
            },
        },
    });
}
/**
 * 更新播放进度
 */
async function updatePlayProgress(userId, chapterId, progress, duration) {
    const userIdNum = parseInt(userId);
    const data = { progress };
    if (duration !== undefined) {
        data.duration = duration;
    }
    return await prisma.playRecord.update({
        where: {
            userId_chapterId: {
                userId: userIdNum,
                chapterId,
            },
        },
        data,
    });
}
/**
 * 获取单个章节的播放进度
 */
async function getSingleProgress(userId, chapterId) {
    const userIdNum = parseInt(userId);
    const record = await prisma.playRecord.findUnique({
        where: {
            userId_chapterId: {
                userId: userIdNum,
                chapterId,
            },
        },
    });
    return record;
}
