"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UsersProfileService = void 0;
const common_1 = require("@nestjs/common");
const admin_service_1 = require("../admin/admin.service");
const teachers_service_1 = require("../teachers/teachers.service");
const students_service_1 = require("../students/students.service");
const role_enum_1 = require("../common/roles/role.enum");
let UsersProfileService = class UsersProfileService {
    adminService;
    teachersService;
    studentsService;
    constructor(adminService, teachersService, studentsService) {
        this.adminService = adminService;
        this.teachersService = teachersService;
        this.studentsService = studentsService;
    }
    async getProfile(userId, role) {
        switch (role) {
            case role_enum_1.Role.ADMIN:
                return this.adminService.findById(userId);
            case role_enum_1.Role.TEACHER:
                return this.teachersService.findOne(userId);
            case role_enum_1.Role.STUDENT:
                return this.studentsService.findOne(userId);
            default:
                throw new common_1.BadRequestException('Invalid role');
        }
    }
    async updateProfile(userId, role, dto) {
        switch (role) {
            case role_enum_1.Role.ADMIN:
                return this.adminService.updateProfile(userId, {
                    name: dto.name,
                    phone: dto.phone,
                    photoUrl: dto.photoUrl,
                });
            case role_enum_1.Role.TEACHER:
                return this.teachersService.updateSelfProfile(userId, {
                    name: dto.name,
                    phone: dto.phone,
                    photoUrl: dto.photoUrl,
                });
            case role_enum_1.Role.STUDENT:
                return this.studentsService.updateSelfProfile(userId, {
                    name: dto.name,
                    phone: dto.phone,
                    profileImageUrl: dto.photoUrl,
                });
            default:
                throw new common_1.BadRequestException('Invalid role');
        }
    }
    async updateEmail(userId, role, newEmail) {
        const email = (newEmail || '').trim().toLowerCase();
        if (!email)
            throw new common_1.BadRequestException('Email is required');
        const [adminMatch, teacherMatch, studentMatch] = await Promise.all([
            this.adminService.findByEmail(email),
            this.teachersService.findByEmail(email),
            this.studentsService.findByEmail(email),
        ]);
        const otherUser = [adminMatch, teacherMatch, studentMatch].find((u) => u && String(u._id) !== userId);
        if (otherUser)
            throw new common_1.BadRequestException('Email already in use');
        switch (role) {
            case role_enum_1.Role.ADMIN:
                return this.adminService.updateEmail(userId, email);
            case role_enum_1.Role.TEACHER:
                return this.teachersService.updateEmail(userId, email);
            case role_enum_1.Role.STUDENT:
                return this.studentsService.updateEmail(userId, email);
            default:
                throw new common_1.BadRequestException('Invalid role');
        }
    }
    async updateNotificationPrefs(userId, role, prefs) {
        switch (role) {
            case role_enum_1.Role.ADMIN:
                return this.adminService.updateNotificationPrefs(userId, prefs);
            case role_enum_1.Role.TEACHER:
                return this.teachersService.updateNotificationPrefs(userId, prefs);
            case role_enum_1.Role.STUDENT:
                return this.studentsService.updateNotificationPrefs(userId, prefs);
            default:
                throw new common_1.BadRequestException('Invalid role');
        }
    }
};
exports.UsersProfileService = UsersProfileService;
exports.UsersProfileService = UsersProfileService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [admin_service_1.AdminService,
        teachers_service_1.TeachersService,
        students_service_1.StudentsService])
], UsersProfileService);
//# sourceMappingURL=users-profile.service.js.map