"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.AdminProductRequestService = void 0;
const common_1 = require("@nestjs/common");
const typeorm_1 = require("typeorm");
const typeorm_2 = require("@nestjs/typeorm");
const productrequest_entity_1 = require("../../common/productrequest/productrequest.entity");
let AdminProductRequestService = class AdminProductRequestService {
    constructor(productrequestRepository) {
        this.productrequestRepository = productrequestRepository;
    }
    async findAll(vendorId, categoryId) {
        let baseQuery = `
    SELECT 
      pr.*, 
      s.name AS shopName
    FROM product_requests pr
    LEFT JOIN categories s ON pr.shopId = s.id
    WHERE 1=1
  `;
        const params = [];
        if (categoryId) {
            baseQuery += ` AND s.id = ?`;
            params.push(categoryId);
        }
        if (vendorId) {
            baseQuery += ` AND FIND_IN_SET(s.id, (
      SELECT categories 
      FROM vendor 
      WHERE userId = ?
    ))`;
            params.push(vendorId);
        }
        baseQuery += ` ORDER BY pr.createdAt DESC`;
        return await this.productrequestRepository.query(baseQuery, params);
    }
    async findOne(id) {
        const productrequest = await this.productrequestRepository.findOneBy({ id });
        if (!productrequest) {
            throw new common_1.NotFoundException(`ProductRequest with ID ${id} not found`);
        }
        return productrequest;
    }
    async remove(id) {
        const result = await this.productrequestRepository.delete(id);
        if (result.affected === 0) {
            throw new common_1.NotFoundException(`ProductRequest with ID ${id} not found`);
        }
    }
    async updateStatus(id, UpdateStatusCloseOpenDto) {
        const request = await this.productrequestRepository.findOne({ where: { id } });
        if (!request) {
            throw new common_1.NotFoundException(`Product Request with ID ${id} not found`);
        }
        request.status = UpdateStatusCloseOpenDto.status;
        await this.productrequestRepository.save(request);
        return {
            status: true,
            message: `Status updated to ${UpdateStatusCloseOpenDto.status}`,
            data: request,
        };
    }
};
exports.AdminProductRequestService = AdminProductRequestService;
exports.AdminProductRequestService = AdminProductRequestService = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, typeorm_2.InjectRepository)(productrequest_entity_1.ProductRequest)),
    __metadata("design:paramtypes", [typeorm_1.Repository])
], AdminProductRequestService);
//# sourceMappingURL=productrequest.service.js.map