"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OnboardingCronService = void 0;
const common_1 = require("@nestjs/common");
const schedule_1 = require("@nestjs/schedule");
const config_1 = require("../../config");
const entities_1 = require("../../config/database/entities");
const notifications_service_1 = require("../notifications/notifications.service");
let OnboardingCronService = class OnboardingCronService {
    constructor(db, notificationsService) {
        this.db = db;
        this.notificationsService = notificationsService;
    }
    async handleDailyOnboardingAlerts() {
        config_1.logger.info('Running daily Onboarding & Probation automated check...');
        try {
            const now = new Date();
            const inSevenDays = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000);
            const probationsDue = await this.db.probations.findMany({
                where: {
                    status: entities_1.ProbationStatusEnum.pending,
                    review_due_date: { lte: inSevenDays },
                },
                include: { member: true, manager: true },
            });
            for (const prob of probationsDue) {
                if (prob.manager_id) {
                    await this.notificationsService.sendNotification({
                        owner_id: prob.owner_id,
                        title: 'Probation Review Due Soon',
                        message: `Probation review for ${prob.member?.first_name} ${prob.member?.last_name} is due by ${prob.current_end_date ? new Date(prob.current_end_date).toLocaleDateString() : 'soon'}.`,
                        type: 'RECRUITMENT',
                        member_id: prob.manager_id,
                        reference_id: prob.id,
                    });
                }
            }
            const overdueTasks = await this.db.onboarding_tasks.findMany({
                where: {
                    status: entities_1.TaskStatusEnum.pending,
                    due_date: { lt: now },
                },
                include: { onboarding_case: true },
            });
            for (const task of overdueTasks) {
                if (task.assigned_member_id) {
                    await this.notificationsService.sendNotification({
                        owner_id: task.onboarding_case.owner_id,
                        title: 'Overdue Onboarding Task',
                        message: `Onboarding task "${task.title}" is overdue. Please complete it as soon as possible.`,
                        type: 'RECRUITMENT',
                        member_id: task.assigned_member_id,
                        reference_id: task.id,
                    });
                }
            }
            config_1.logger.info(`Automated check completed. Processed ${probationsDue.length} probation reminders and ${overdueTasks.length} overdue tasks.`);
        }
        catch (error) {
            config_1.logger.error('Error running daily onboarding alerts cron', error);
        }
    }
};
exports.OnboardingCronService = OnboardingCronService;
__decorate([
    (0, schedule_1.Cron)(schedule_1.CronExpression.EVERY_DAY_AT_8AM),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], OnboardingCronService.prototype, "handleDailyOnboardingAlerts", null);
exports.OnboardingCronService = OnboardingCronService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [config_1.DatabaseService,
        notifications_service_1.NotificationsService])
], OnboardingCronService);
//# sourceMappingURL=onboarding-cron.service.js.map