"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FridayAppMessageDao = void 0;
const FridayApp_1 = require("../models/FridayApp");
const FridayAppView_1 = require("../models/FridayAppView");
const typeorm_1 = require("typeorm");
const dayjs_1 = __importDefault(require("dayjs"));
class FridayAppMessageDao {
    static finishReply(replyId) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const reply = yield FridayApp_1.FridayAppReplyTable.findOne({
                    where: { id: replyId },
                });
                if (!reply) {
                    throw new Error(`Reply with id ${replyId} not found`);
                }
                reply.finished = true;
                reply.endTimeStamp = (0, dayjs_1.default)().format('YYYY-MM-DD HH:mm:ss.SSS');
                yield reply.save();
                // Return the updated reply data
                return (yield FridayAppView_1.FridayAppReplyView.findOne({
                    where: { id: replyId },
                }));
            }
            catch (error) {
                console.error(error);
                throw error;
            }
        });
    }
    static saveReplyMessage(replyId, msg, finished) {
        return __awaiter(this, void 0, void 0, function* () {
            yield FridayApp_1.FridayAppReplyTable.createQueryBuilder()
                .insert()
                .into(FridayApp_1.FridayAppReplyTable)
                .values({
                id: replyId,
                startTimeStamp: msg.timestamp,
                finished: finished,
            })
                .orIgnore()
                .execute();
            yield FridayApp_1.FridayAppMessageTable.createQueryBuilder()
                .insert()
                .into(FridayApp_1.FridayAppMessageTable)
                .values({
                id: msg.id,
                replyId: replyId,
                name: msg.name,
                role: msg.role,
                content: msg.content,
                timestamp: msg.timestamp,
            })
                .orUpdate(['name', 'role', 'content', 'timestamp'], ['id'])
                .execute();
            return (yield FridayAppView_1.FridayAppReplyView.findOne({
                where: { id: replyId },
            }));
        });
    }
    static getRepliesBefore(timestamp_1) {
        return __awaiter(this, arguments, void 0, function* (timestamp, limit = 100) {
            const conditions = {
                order: { startTimeStamp: 'ASC' },
                limit: limit,
            };
            if (timestamp) {
                conditions.where = { startTimeStamp: (0, typeorm_1.LessThan)(timestamp) };
            }
            const replies = yield FridayAppView_1.FridayAppReplyView.find(conditions);
            return {
                replies,
                hasMore: true,
            };
        });
    }
    static cleanHistoryMessages() {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                yield FridayApp_1.FridayAppMessageTable.clear();
                yield FridayApp_1.FridayAppReplyTable.clear();
            }
            catch (error) {
                console.error('Error cleaning history messages:', error);
                throw new Error('Failed to clean history messages.');
            }
        });
    }
}
exports.FridayAppMessageDao = FridayAppMessageDao;
