"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.SupportService = void 0;
const common_1 = require("@nestjs/common");
const typeorm_1 = require("@nestjs/typeorm");
const typeorm_2 = require("typeorm");
const ticket_entity_1 = require("./ticket.entity");
const comment_entity_1 = require("./comment.entity");
let SupportService = class SupportService {
    constructor(ticketRepo, commentRepo) {
        this.ticketRepo = ticketRepo;
        this.commentRepo = commentRepo;
    }
    async createTicket(dto) {
        const ticket = this.ticketRepo.create({
            email: dto.email,
            subject: dto.subject,
            ticketshowId: dto.ticketshowId,
            description: dto.description,
            vendorName: dto.vendorName,
            priority: dto.priority,
            status: dto.status,
            user: dto.userId ? { id: dto.userId } : null,
            userId: dto.userId ?? null,
        });
        const savedTicket = await this.ticketRepo.save(ticket);
        const comment = this.commentRepo.create({
            ticket: savedTicket,
            author: dto.author,
            message: dto.message,
            imagePaths: dto.imagePaths ?? [],
        });
        const savedComment = await this.commentRepo.save(comment);
        savedTicket.comments = [savedComment];
        return savedTicket;
    }
    async findAll(opts) {
        const { page, limit, status, priority, q, vendorId, order = 'DESC' } = opts;
        const qb = this.ticketRepo.createQueryBuilder('ticket');
        if (status) {
            qb.andWhere('ticket.status = :status', { status });
        }
        if (priority) {
            qb.andWhere('ticket.priority = :priority', { priority });
        }
        if (q) {
            qb.andWhere('(ticket.subject ILIKE :q OR ticket.description ILIKE :q OR ticket.vendor_name ILIKE :q)', { q: `%${q}%` });
        }
        if (vendorId) {
            qb.andWhere('ticket.userId = :userId', { userId: vendorId });
        }
        qb.orderBy('ticket.created_at', order);
        if (page && limit) {
            qb.skip((page - 1) * limit).take(limit);
            const [items, total] = await qb.getManyAndCount();
            return [items, total];
        }
        const items = await qb.getMany();
        return [items, items.length];
    }
    async findOne(id) {
        const ticket = await this.ticketRepo
            .createQueryBuilder('ticket')
            .leftJoinAndSelect('ticket.comments', 'comment')
            .where('ticket.id = :id', { id })
            .orderBy('comment.created_at', 'ASC')
            .getOne();
        if (!ticket)
            throw new common_1.NotFoundException('Ticket not found');
        return ticket;
    }
    async updateTicket(id, dto) {
        const ticket = await this.ticketRepo.preload({ id, ...dto });
        if (!ticket)
            throw new common_1.NotFoundException('Ticket not found');
        return this.ticketRepo.save(ticket);
    }
    async updateStatus(id, status) {
        const ticket = await this.findOne(id);
        ticket.status = status;
        return this.ticketRepo.save(ticket);
    }
    async remove(id) {
        const result = await this.ticketRepo.delete(id);
        if (result.affected === 0) {
            return {
                status: false,
                message: 'Ticket not found',
            };
        }
        return {
            status: true,
            message: 'Ticket deleted successfully',
        };
    }
    async addComment(ticketId, dto) {
        const ticket = await this.ticketRepo.findOneBy({ id: ticketId });
        if (!ticket)
            throw new common_1.NotFoundException('Ticket not found');
        const comment = this.commentRepo.create({
            ...dto,
            ticket,
            imagePaths: dto.imagePaths || [],
        });
        return this.commentRepo.save(comment);
    }
};
exports.SupportService = SupportService;
exports.SupportService = SupportService = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, typeorm_1.InjectRepository)(ticket_entity_1.Ticket)),
    __param(1, (0, typeorm_1.InjectRepository)(comment_entity_1.Comment)),
    __metadata("design:paramtypes", [typeorm_2.Repository,
        typeorm_2.Repository])
], SupportService);
//# sourceMappingURL=support.service.js.map