"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.AdminUserService = void 0;
const common_1 = require("@nestjs/common");
const typeorm_1 = require("@nestjs/typeorm");
const typeorm_2 = require("typeorm");
const admin_user_entity_1 = require("../entities/admin-user.entity");
const debug_logger_service_1 = require("../../core/services/debug-logger.service");
let AdminUserService = class AdminUserService {
    adminRepo;
    debugLogger;
    constructor(adminRepo, debugLogger) {
        this.adminRepo = adminRepo;
        this.debugLogger = debugLogger;
    }
    async createAdmin(data) {
        const email = data.email.toLowerCase();
        const existing = await this.adminRepo.findOne({ where: { email } });
        if (existing) {
            throw new common_1.ConflictException({
                message: `Admin user with email ${email} already exists.`,
                code: 'ADMIN_USER_EXISTS',
            });
        }
        const admin = this.adminRepo.create({
            email,
            name: data.name,
            metadata: data.metadata ?? {},
        });
        await admin.setPassword(data.password);
        this.debugLogger.debug('Creating admin user', 'AdminUserService', { email });
        await this.adminRepo.save(admin);
        this.debugLogger.info('Admin user created', 'AdminUserService', { id: admin.id, email });
        return admin;
    }
    async findByEmail(email) {
        if (!email) {
            return null;
        }
        return this.adminRepo.findOne({ where: { email: email.toLowerCase() } });
    }
    async findById(id) {
        if (!id) {
            return null;
        }
        return this.adminRepo.findOne({ where: { id } });
    }
    async listAdmins() {
        return this.adminRepo.find({ order: { createdAt: 'DESC' } });
    }
    async updateAdmin(id, data) {
        const admin = await this.findById(id);
        if (!admin) {
            throw new common_1.NotFoundException({
                message: `Admin user with ID ${id} not found`,
                code: 'ADMIN_USER_NOT_FOUND',
            });
        }
        if (data.name !== undefined) {
            admin.name = data.name;
        }
        if (data.metadata !== undefined) {
            admin.metadata = data.metadata;
        }
        if (data.password) {
            await admin.setPassword(data.password);
        }
        await this.adminRepo.save(admin);
        return admin;
    }
    async deleteAdmin(id) {
        const admin = await this.findById(id);
        if (!admin) {
            throw new common_1.NotFoundException({
                message: `Admin user with ID ${id} not found`,
                code: 'ADMIN_USER_NOT_FOUND',
            });
        }
        await this.adminRepo.delete(id);
    }
};
exports.AdminUserService = AdminUserService;
exports.AdminUserService = AdminUserService = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, typeorm_1.InjectRepository)(admin_user_entity_1.NestAuthAdminUser)),
    __metadata("design:paramtypes", [typeorm_2.Repository,
        debug_logger_service_1.DebugLoggerService])
], AdminUserService);
//# sourceMappingURL=admin-user.service.js.map