"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.PlansService = void 0;
const common_1 = require("@nestjs/common");
const typeorm_1 = require("@nestjs/typeorm");
const typeorm_2 = require("typeorm");
const plan_entity_1 = require("../entities/plan.entity");
let PlansService = class PlansService {
    constructor(planRepo) {
        this.planRepo = planRepo;
    }
    async findAll() {
        const plans = await this.planRepo.find({
            where: {
                is_active: true,
                is_visible: true,
            },
            order: {
                sort_order: 'ASC',
            },
        });
        return plans.map((plan) => this.toResponseDto(plan));
    }
    async findOne(id) {
        const plan = await this.planRepo.findOne({
            where: { id },
        });
        if (!plan) {
            throw new common_1.NotFoundException(`Plan with ID "${id}" not found`);
        }
        return this.toDetailResponseDto(plan);
    }
    async findBySlug(slug) {
        const plan = await this.planRepo.findOne({
            where: { slug },
        });
        if (!plan) {
            throw new common_1.NotFoundException(`Plan with slug "${slug}" not found`);
        }
        return this.toDetailResponseDto(plan);
    }
    toResponseDto(plan) {
        const featureDescriptions = this.extractFeatureDescriptions(plan);
        return {
            id: plan.id,
            name: plan.name,
            slug: plan.slug,
            display_name: plan.name,
            description: plan.description || '',
            tagline: plan.tagline || undefined,
            price_monthly: plan.price_monthly ? Number(plan.price_monthly) : 0,
            price_yearly: plan.price_yearly ? Number(plan.price_yearly) : 0,
            currency: plan.currency,
            features: featureDescriptions,
            limits: plan.limits || undefined,
            is_popular: plan.is_popular || undefined,
            trial_days: plan.trial_days || undefined,
        };
    }
    toDetailResponseDto(plan) {
        const baseDto = this.toResponseDto(plan);
        return {
            ...baseDto,
            is_enterprise: plan.is_enterprise || undefined,
            detailed_features: plan.features || undefined,
            metadata: plan.metadata || undefined,
        };
    }
    extractFeatureDescriptions(plan) {
        const descriptions = [];
        if (plan.features && Array.isArray(plan.features)) {
            for (const feature of plan.features) {
                if (typeof feature === 'object' && feature.name) {
                    if (typeof feature.value === 'boolean') {
                        if (feature.value) {
                            descriptions.push(feature.name);
                        }
                    }
                    else if (typeof feature.value === 'string') {
                        descriptions.push(`${feature.name}: ${feature.value}`);
                    }
                    else {
                        descriptions.push(feature.name);
                    }
                }
            }
        }
        if (plan.included_features && Array.isArray(plan.included_features)) {
            descriptions.push(...plan.included_features);
        }
        return descriptions;
    }
};
exports.PlansService = PlansService;
exports.PlansService = PlansService = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, typeorm_1.InjectRepository)(plan_entity_1.Plan)),
    __metadata("design:paramtypes", [typeorm_2.Repository])
], PlansService);
//# sourceMappingURL=plans.service.js.map