"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.OrdersController = 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");
let OrdersController = class OrdersController {
    constructor(prisma) {
        this.prisma = prisma;
    }
    async createOrder(req) {
        const userId = req.user.userId;
        const cart = await this.prisma.cart.findUnique({ where: { userId }, include: { items: { include: { product: true } } } });
        if (!cart || cart.items.length === 0)
            return (0, envelope_1.ok)({ error: 'Cart is empty' });
        const total = cart.items.reduce((sum, i) => sum + i.product.price * i.quantity, 0);
        const currency = cart.items[0]?.product?.currency ?? 'NGN';
        const order = await this.prisma.order.create({
            data: {
                userId,
                currency,
                total,
                items: { create: cart.items.map(i => ({ productId: i.productId, title: i.product.title, unitPrice: i.product.price, quantity: i.quantity })) },
            },
            include: { items: true },
        });
        await this.prisma.cartItem.deleteMany({ where: { cartId: cart.id } });
        return (0, envelope_1.ok)(order);
    }
    async myOrders(req) {
        const userId = req.user.userId;
        const items = await this.prisma.order.findMany({ where: { userId }, include: { items: true, payments: true }, orderBy: { createdAt: 'desc' } });
        return (0, envelope_1.ok)(items);
    }
    async get(req, orderId) {
        const userId = req.user.userId;
        const order = await this.prisma.order.findFirst({ where: { id: orderId, userId }, include: { items: true, payments: true } });
        return (0, envelope_1.ok)(order);
    }
};
exports.OrdersController = OrdersController;
__decorate([
    (0, common_1.Post)('orders'),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], OrdersController.prototype, "createOrder", null);
__decorate([
    (0, common_1.Get)('orders/my'),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], OrdersController.prototype, "myOrders", null);
__decorate([
    (0, common_1.Get)('orders/:orderId'),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Param)('orderId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Promise)
], OrdersController.prototype, "get", null);
exports.OrdersController = OrdersController = __decorate([
    (0, swagger_1.ApiTags)('Orders'),
    (0, swagger_1.ApiBearerAuth)(),
    (0, common_1.Controller)(),
    __metadata("design:paramtypes", [prisma_service_1.PrismaService])
], OrdersController);
//# sourceMappingURL=orders.controller.js.map