"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.LibraryController = 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");
const class_validator_1 = require("class-validator");
class CreateBookDto {
}
__decorate([
    (0, class_validator_1.IsString)(),
    __metadata("design:type", String)
], CreateBookDto.prototype, "title", void 0);
__decorate([
    (0, class_validator_1.IsString)(),
    __metadata("design:type", String)
], CreateBookDto.prototype, "author", void 0);
__decorate([
    (0, class_validator_1.IsString)(),
    __metadata("design:type", String)
], CreateBookDto.prototype, "type", void 0);
__decorate([
    (0, class_validator_1.IsOptional)(),
    (0, class_validator_1.IsInt)(),
    (0, class_validator_1.Min)(0),
    __metadata("design:type", Number)
], CreateBookDto.prototype, "price", void 0);
__decorate([
    (0, class_validator_1.IsOptional)(),
    (0, class_validator_1.IsString)(),
    __metadata("design:type", String)
], CreateBookDto.prototype, "currency", void 0);
__decorate([
    (0, class_validator_1.IsString)(),
    __metadata("design:type", String)
], CreateBookDto.prototype, "visibility", void 0);
__decorate([
    (0, class_validator_1.IsString)(),
    __metadata("design:type", String)
], CreateBookDto.prototype, "fileUrl", void 0);
__decorate([
    (0, class_validator_1.IsOptional)(),
    (0, class_validator_1.IsString)(),
    __metadata("design:type", String)
], CreateBookDto.prototype, "coverUrl", void 0);
__decorate([
    (0, class_validator_1.IsOptional)(),
    (0, class_validator_1.IsBoolean)(),
    __metadata("design:type", Boolean)
], CreateBookDto.prototype, "published", void 0);
let LibraryController = class LibraryController {
    constructor(prisma) {
        this.prisma = prisma;
    }
    async list(q) {
        const where = { published: true };
        if (q)
            where.OR = [{ title: { contains: q, mode: 'insensitive' } }, { author: { contains: q, mode: 'insensitive' } }];
        const items = await this.prisma.libraryBook.findMany({ where, orderBy: { createdAt: 'desc' } });
        return (0, envelope_1.ok)(items);
    }
    async get(bookId) {
        const book = await this.prisma.libraryBook.findUnique({ where: { id: bookId } });
        return (0, envelope_1.ok)(book);
    }
    async create(dto) {
        const b = await this.prisma.libraryBook.create({ data: { ...dto, currency: dto.currency ?? 'NGN', price: dto.type === 'paid' ? (dto.price ?? 0) : null, published: dto.published ?? true } });
        return (0, envelope_1.ok)(b);
    }
    async update(bookId, dto) {
        const b = await this.prisma.libraryBook.update({ where: { id: bookId }, data: dto });
        return (0, envelope_1.ok)(b);
    }
};
exports.LibraryController = LibraryController;
__decorate([
    (0, common_1.Get)('library/books'),
    __param(0, (0, common_1.Query)('q')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], LibraryController.prototype, "list", null);
__decorate([
    (0, common_1.Get)('library/books/:bookId'),
    __param(0, (0, common_1.Param)('bookId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], LibraryController.prototype, "get", null);
__decorate([
    (0, roles_decorator_1.Roles)(roles_1.UserRole.ADMIN),
    (0, common_1.Post)('admin/library/books'),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [CreateBookDto]),
    __metadata("design:returntype", Promise)
], LibraryController.prototype, "create", null);
__decorate([
    (0, roles_decorator_1.Roles)(roles_1.UserRole.ADMIN),
    (0, common_1.Patch)('admin/library/books/:bookId'),
    __param(0, (0, common_1.Param)('bookId')),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], LibraryController.prototype, "update", null);
exports.LibraryController = LibraryController = __decorate([
    (0, swagger_1.ApiTags)('Library'),
    (0, swagger_1.ApiBearerAuth)(),
    (0, common_1.Controller)(),
    __metadata("design:paramtypes", [prisma_service_1.PrismaService])
], LibraryController);
//# sourceMappingURL=library.controller.js.map