"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 SmsService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SmsService = void 0;
const common_1 = require("@nestjs/common");
const config_1 = require("@nestjs/config");
let SmsService = SmsService_1 = class SmsService {
    config;
    logger = new common_1.Logger(SmsService_1.name);
    twilioClient = null;
    twilioFrom;
    isProd;
    constructor(config) {
        this.config = config;
        this.isProd = (process.env.NODE_ENV || '').toLowerCase() === 'production';
        const sid = this.config.get('TWILIO_SID');
        const token = this.config.get('TWILIO_TOKEN');
        this.twilioFrom = this.config.get('TWILIO_FROM');
        if (sid && token) {
            try {
                const { Twilio } = require('twilio');
                this.twilioClient = new Twilio(sid, token);
            }
            catch {
                this.logger.error('Twilio SDK not available. Install "twilio" to enable SMS.');
            }
        }
        else if (this.isProd) {
            this.logger.warn('Missing TWILIO_SID/TOKEN in production.');
        }
    }
    async sendSms(to, body) {
        if (!to || !/^\+\d{7,15}$/.test(to.trim())) {
            this.logger.warn(`Skipping SMS to invalid number: ${to}`);
            return { ok: false, error: 'Invalid phone number format' };
        }
        if (!this.twilioClient || !this.twilioFrom) {
            if (!this.isProd) {
                this.logger.log(`[DEV] SMS to=${to} body=${body.slice(0, 80)}...`);
                return { ok: true };
            }
            return { ok: false, error: 'SMS provider not configured' };
        }
        try {
            const sanitizedBody = body.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, '');
            await this.twilioClient.messages.create({
                body: sanitizedBody,
                from: this.twilioFrom,
                to,
            });
            return { ok: true };
        }
        catch (err) {
            this.logger.error(`SMS send failed to=${to}: ${err?.message}`);
            return { ok: false, error: 'Failed to send SMS' };
        }
    }
};
exports.SmsService = SmsService;
exports.SmsService = SmsService = SmsService_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [config_1.ConfigService])
], SmsService);
//# sourceMappingURL=sms.service.js.map