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);
};
import BillingService from "../../../Services/BillingService";
import ProjectService from "../../../Services/ProjectService";
import TelemetryUsageBillingService from "../../../Services/TelemetryUsageBillingService";
import ServerMeteredPlan from "./ServerMeteredPlan";
import OneUptimeDate from "../../../../Types/Date";
import ProductType from "../../../../Types/MeteredPlan/ProductType";
import ObjectID from "../../../../Types/ObjectID";
import CaptureSpan from "../../../Utils/Telemetry/CaptureSpan";
export default class TelemetryMeteredPlan extends ServerMeteredPlan {
    get productType() {
        return this._productType;
    }
    set productType(v) {
        this._productType = v;
    }
    get unitCostInUSD() {
        return this._unitCostInUSD;
    }
    set unitCostInUSD(v) {
        this._unitCostInUSD = v;
    }
    constructor(data) {
        super();
        this._unitCostInUSD = 0;
        this.productType = data.productType;
        this.unitCostInUSD = data.unitCostInUSD;
    }
    getTotalCostInUSD(data) {
        return data.dataIngestedInGB * data.retentionInDays * this.unitCostInUSD;
    }
    getProductType() {
        return this.productType;
    }
    async reportQuantityToBillingProvider(projectId, options) {
        // get all unreported logs
        var _a;
        await TelemetryUsageBillingService.stageTelemetryUsageForProject({
            projectId: projectId,
            productType: this.productType,
        });
        const usageBillings = await TelemetryUsageBillingService.getUnreportedUsageBilling({
            projectId: projectId,
            productType: this.productType,
        });
        if (usageBillings.length === 0) {
            return;
        }
        // calculate all the total usage count and report it to billing provider.
        let totalCostInUSD = 0;
        for (const usageBilling of usageBillings) {
            if (((_a = usageBilling === null || usageBilling === void 0 ? void 0 : usageBilling.totalCostInUSD) === null || _a === void 0 ? void 0 : _a.value) &&
                (usageBilling === null || usageBilling === void 0 ? void 0 : usageBilling.totalCostInUSD.value) > 0) {
                totalCostInUSD += usageBilling.totalCostInUSD.value;
            }
        }
        if (totalCostInUSD < 1) {
            return; // too low to report.
        }
        // convert USD to cents.
        let totalCostInCents = totalCostInUSD * 100;
        // convert this to integer.
        totalCostInCents = Math.ceil(totalCostInCents);
        // update this count in project as well.
        const project = await ProjectService.findOneById({
            id: projectId,
            select: {
                paymentProviderMeteredSubscriptionId: true,
                paymentProviderPlanId: true,
            },
            props: {
                isRoot: true,
            },
        });
        if (project &&
            ((options === null || options === void 0 ? void 0 : options.meteredPlanSubscriptionId) ||
                project.paymentProviderMeteredSubscriptionId) &&
            project.paymentProviderPlanId) {
            await BillingService.addOrUpdateMeteredPricingOnSubscription((options === null || options === void 0 ? void 0 : options.meteredPlanSubscriptionId) ||
                project.paymentProviderMeteredSubscriptionId, this, totalCostInCents);
            for (const usageBilling of usageBillings) {
                if (usageBilling.id) {
                    // now mark it as reported.
                    await TelemetryUsageBillingService.updateOneById({
                        id: usageBilling.id,
                        data: {
                            isReportedToBillingProvider: true,
                            reportedToBillingProviderAt: OneUptimeDate.getCurrentDate(),
                        },
                        props: {
                            isRoot: true,
                        },
                    });
                }
            }
        }
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", String)
], TelemetryMeteredPlan.prototype, "getProductType", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [ObjectID, Object]),
    __metadata("design:returntype", Promise)
], TelemetryMeteredPlan.prototype, "reportQuantityToBillingProvider", null);
//# sourceMappingURL=TelemetryMeteredPlan.js.map