"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.CertificatesController = 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 roles_decorator_1 = require("../../common/decorators/roles.decorator");
const roles_1 = require("../../common/roles");
let CertificatesController = class CertificatesController {
    constructor(prisma) {
        this.prisma = prisma;
    }
    async my(req) {
        const userId = req.user.userId;
        const items = await this.prisma.certificate.findMany({ where: { userId }, include: { course: true }, orderBy: { issuedAt: 'desc' } });
        return (0, envelope_1.ok)(items);
    }
    async verify(code) {
        const cert = await this.prisma.certificate.findUnique({ where: { code }, include: { user: { select: { fullName: true } }, course: { select: { title: true } } } });
        return (0, envelope_1.ok)(cert ? { valid: true, certificate: cert } : { valid: false });
    }
    async issue(enrollmentId) {
        const enrollment = await this.prisma.enrollment.findUnique({ where: { id: enrollmentId } });
        if (!enrollment)
            return (0, envelope_1.ok)({ error: 'Enrollment not found' });
        const code = `ALM-CERT-${Date.now()}-${Math.random().toString(16).slice(2).toUpperCase()}`;
        const cert = await this.prisma.certificate.upsert({
            where: { courseId_userId: { courseId: enrollment.courseId, userId: enrollment.userId } },
            create: { courseId: enrollment.courseId, userId: enrollment.userId, code, pdfUrl: null },
            update: {},
        });
        return (0, envelope_1.ok)(cert);
    }
};
exports.CertificatesController = CertificatesController;
__decorate([
    (0, common_1.Get)('certificates/my'),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], CertificatesController.prototype, "my", null);
__decorate([
    (0, common_1.Get)('certificates/verify/:code'),
    __param(0, (0, common_1.Param)('code')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], CertificatesController.prototype, "verify", null);
__decorate([
    (0, roles_decorator_1.Roles)(roles_1.UserRole.ADMIN, roles_1.UserRole.STAFF),
    (0, common_1.Post)('admin/certificates/issue/:enrollmentId'),
    __param(0, (0, common_1.Param)('enrollmentId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], CertificatesController.prototype, "issue", null);
exports.CertificatesController = CertificatesController = __decorate([
    (0, swagger_1.ApiTags)('Certificates'),
    (0, swagger_1.ApiBearerAuth)(),
    (0, common_1.Controller)(),
    __metadata("design:paramtypes", [prisma_service_1.PrismaService])
], CertificatesController);
//# sourceMappingURL=certificates.controller.js.map