"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.QnaController = void 0;
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const prisma_service_1 = require("../prisma/prisma.service");
const envelope_1 = require("../../common/envelope");
const class_validator_1 = require("class-validator");
const roles_decorator_1 = require("../../common/decorators/roles.decorator");
const roles_1 = require("../../common/roles");
class CreateThreadDto {
}
__decorate([
    (0, class_validator_1.IsString)(),
    __metadata("design:type", String)
], CreateThreadDto.prototype, "moduleId", void 0);
__decorate([
    (0, class_validator_1.IsString)(),
    __metadata("design:type", String)
], CreateThreadDto.prototype, "question", void 0);
class CreateAnswerDto {
}
__decorate([
    (0, class_validator_1.IsString)(),
    __metadata("design:type", String)
], CreateAnswerDto.prototype, "answer", void 0);
class UpdateStatusDto {
}
__decorate([
    (0, class_validator_1.IsString)(),
    __metadata("design:type", String)
], UpdateStatusDto.prototype, "status", void 0);
let QnaController = class QnaController {
    constructor(prisma) {
        this.prisma = prisma;
    }
    async list(moduleId) {
        const where = {};
        if (moduleId)
            where.moduleId = moduleId;
        const threads = await this.prisma.qnaThread.findMany({
            where,
            orderBy: [{ pinned: 'desc' }, { createdAt: 'desc' }],
            include: { askedBy: { select: { id: true, fullName: true, role: true } }, answers: { include: { postedBy: { select: { id: true, fullName: true, role: true } } }, orderBy: { createdAt: 'asc' } } },
        });
        return (0, envelope_1.ok)(threads);
    }
    async createThread(req, dto) {
        const userId = req.user.userId;
        const thread = await this.prisma.qnaThread.create({ data: { moduleId: dto.moduleId, askedById: userId, question: dto.question } });
        return (0, envelope_1.ok)(thread);
    }
    async answer(req, threadId, dto) {
        const userId = req.user.userId;
        const a = await this.prisma.qnaAnswer.create({ data: { threadId, postedById: userId, answer: dto.answer } });
        await this.prisma.qnaThread.update({ where: { id: threadId }, data: { status: 'ANSWERED' } });
        return (0, envelope_1.ok)(a);
    }
    async setStatus(threadId, dto) {
        const t = await this.prisma.qnaThread.update({ where: { id: threadId }, data: { status: dto.status } });
        return (0, envelope_1.ok)(t);
    }
    async pin(threadId) {
        const t = await this.prisma.qnaThread.update({ where: { id: threadId }, data: { pinned: true } });
        return (0, envelope_1.ok)(t);
    }
};
exports.QnaController = QnaController;
__decorate([
    (0, common_1.Get)('qna/threads'),
    __param(0, (0, common_1.Query)('moduleId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], QnaController.prototype, "list", null);
__decorate([
    (0, common_1.Post)('qna/threads'),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, CreateThreadDto]),
    __metadata("design:returntype", Promise)
], QnaController.prototype, "createThread", null);
__decorate([
    (0, common_1.Post)('qna/threads/:threadId/answers'),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Param)('threadId')),
    __param(2, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String, CreateAnswerDto]),
    __metadata("design:returntype", Promise)
], QnaController.prototype, "answer", null);
__decorate([
    (0, roles_decorator_1.Roles)(roles_1.UserRole.STAFF, roles_1.UserRole.ADMIN),
    (0, common_1.Patch)('admin/qna/threads/:threadId/status'),
    __param(0, (0, common_1.Param)('threadId')),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, UpdateStatusDto]),
    __metadata("design:returntype", Promise)
], QnaController.prototype, "setStatus", null);
__decorate([
    (0, roles_decorator_1.Roles)(roles_1.UserRole.STAFF, roles_1.UserRole.ADMIN),
    (0, common_1.Patch)('admin/qna/threads/:threadId/pin'),
    __param(0, (0, common_1.Param)('threadId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], QnaController.prototype, "pin", null);
exports.QnaController = QnaController = __decorate([
    (0, swagger_1.ApiTags)('QnA'),
    (0, swagger_1.ApiBearerAuth)(),
    (0, common_1.Controller)(),
    __metadata("design:paramtypes", [prisma_service_1.PrismaService])
], QnaController);
//# sourceMappingURL=qna.controller.js.map