"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
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 __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
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.AdminService = void 0;
const common_1 = require("@nestjs/common");
const mongoose_1 = require("@nestjs/mongoose");
const admin_schema_1 = require("./schemas/admin.schema");
const mongoose_2 = require("mongoose");
const bcrypt = __importStar(require("bcryptjs"));
const BCRYPT_ROUNDS = Number(process.env.BCRYPT_ROUNDS || 10);
let AdminService = class AdminService {
    adminModel;
    constructor(adminModel) {
        this.adminModel = adminModel;
    }
    async findByEmail(email) {
        return this.adminModel.findOne({ email: (email || '').trim().toLowerCase() }).lean();
    }
    async findByIdForAuth(id) {
        return this.adminModel.findById(id).lean();
    }
    async findById(id) {
        return this.adminModel.findById(id, { password: 0 }).lean();
    }
    async findByPhone(phone) {
        return this.adminModel.findOne({ phone: (phone || '').trim() }).lean();
    }
    async create(dto) {
        const email = (dto.email || '').trim().toLowerCase();
        const exists = await this.adminModel.findOne({ email }).lean();
        if (exists)
            throw new common_1.BadRequestException('Email already exists');
        const hashedPassword = await bcrypt.hash(dto.password, BCRYPT_ROUNDS);
        const { generateAdminId } = await Promise.resolve().then(() => __importStar(require('../common/utils/id-generator')));
        const uniqueId = await generateAdminId(this.adminModel);
        const admin = await this.adminModel.create({
            uniqueId,
            email,
            password: hashedPassword,
            phone: dto.phone ? dto.phone.trim() : undefined,
        });
        return admin.toJSON();
    }
    async updatePassword(id, newPasswordHash) {
        const updated = await this.adminModel.findByIdAndUpdate(id, { password: newPasswordHash }, { new: true });
        if (!updated)
            throw new common_1.NotFoundException('Admin not found');
        return { message: 'Password updated successfully' };
    }
    async updateProfile(id, data) {
        const update = {};
        if (data.name !== undefined)
            update.name = (data.name || '').trim() || undefined;
        if (data.phone !== undefined)
            update.phone = data.phone ? data.phone.trim() : undefined;
        if (data.photoUrl !== undefined)
            update.photoUrl = data.photoUrl ? data.photoUrl.trim() : '';
        const updated = await this.adminModel.findByIdAndUpdate(id, update, { new: true, projection: { password: 0 } }).lean();
        if (!updated)
            throw new common_1.NotFoundException('Admin not found');
        return updated;
    }
    async updateEmail(id, newEmail) {
        const email = (newEmail || '').trim().toLowerCase();
        if (!email)
            throw new common_1.BadRequestException('Email is required');
        const exists = await this.adminModel.findOne({ email, _id: { $ne: id } }).lean();
        if (exists)
            throw new common_1.BadRequestException('Email already in use');
        const updated = await this.adminModel.findByIdAndUpdate(id, { email }, { new: true, projection: { password: 0 } }).lean();
        if (!updated)
            throw new common_1.NotFoundException('Admin not found');
        return updated;
    }
    async updateNotificationPrefs(id, prefs) {
        const update = {};
        if (prefs.email !== undefined)
            update['notificationPrefs.email'] = prefs.email;
        if (prefs.whatsapp !== undefined)
            update['notificationPrefs.whatsapp'] = prefs.whatsapp;
        const updated = await this.adminModel.findByIdAndUpdate(id, { $set: update }, { new: true, projection: { password: 0 } }).lean();
        if (!updated)
            throw new common_1.NotFoundException('Admin not found');
        return updated;
    }
};
exports.AdminService = AdminService;
exports.AdminService = AdminService = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, mongoose_1.InjectModel)(admin_schema_1.Admin.name)),
    __metadata("design:paramtypes", [mongoose_2.Model])
], AdminService);
//# sourceMappingURL=admin.service.js.map