"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.EnrollmentsController = 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 EnrollDto {
}
__decorate([
    (0, class_validator_1.IsString)(),
    __metadata("design:type", String)
], EnrollDto.prototype, "courseId", void 0);
let EnrollmentsController = class EnrollmentsController {
    constructor(prisma) {
        this.prisma = prisma;
    }
    async enroll(req, dto) {
        const userId = req.user.userId;
        const course = await this.prisma.course.findUnique({ where: { id: dto.courseId } });
        if (!course)
            return (0, envelope_1.ok)({ error: 'Course not found' });
        const enrollment = await this.prisma.enrollment.upsert({
            where: { userId_courseId: { userId, courseId: dto.courseId } },
            create: { userId, courseId: dto.courseId, status: course.isFree || course.price === 0 ? 'ACTIVE' : 'PENDING_PAYMENT' },
            update: {},
        });
        return (0, envelope_1.ok)(enrollment);
    }
    async myEnrollments(req) {
        const userId = req.user.userId;
        const items = await this.prisma.enrollment.findMany({ where: { userId }, include: { course: true }, orderBy: { createdAt: 'desc' } });
        return (0, envelope_1.ok)(items);
    }
    async enrollment(req, enrollmentId) {
        const userId = req.user.userId;
        const item = await this.prisma.enrollment.findFirst({ where: { id: enrollmentId, userId }, include: { course: true } });
        return (0, envelope_1.ok)(item);
    }
    async activate(enrollmentId) {
        const updated = await this.prisma.enrollment.update({ where: { id: enrollmentId }, data: { status: 'ACTIVE' } });
        return (0, envelope_1.ok)(updated);
    }
};
exports.EnrollmentsController = EnrollmentsController;
__decorate([
    (0, common_1.Post)('enrollments'),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, EnrollDto]),
    __metadata("design:returntype", Promise)
], EnrollmentsController.prototype, "enroll", null);
__decorate([
    (0, common_1.Get)('enrollments/my'),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], EnrollmentsController.prototype, "myEnrollments", null);
__decorate([
    (0, common_1.Get)('enrollments/:enrollmentId'),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Param)('enrollmentId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Promise)
], EnrollmentsController.prototype, "enrollment", null);
__decorate([
    (0, roles_decorator_1.Roles)(roles_1.UserRole.ADMIN, roles_1.UserRole.STAFF),
    (0, common_1.Post)('admin/enrollments/:enrollmentId/activate'),
    __param(0, (0, common_1.Param)('enrollmentId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], EnrollmentsController.prototype, "activate", null);
exports.EnrollmentsController = EnrollmentsController = __decorate([
    (0, swagger_1.ApiTags)('Enrollments'),
    (0, swagger_1.ApiBearerAuth)(),
    (0, common_1.Controller)(),
    __metadata("design:paramtypes", [prisma_service_1.PrismaService])
], EnrollmentsController);
//# sourceMappingURL=enrollments.controller.js.map