import { Injectable, NotFoundException } from '@nestjs/common';
import { Modules, Users } from 'prisma/postgres/generated/prisma/client/postgres/client';
import { toKeysFromBoolean } from 'src/common/utils/boolean-utils';
import { PrismaPostgresService } from 'prisma/postgres/prisma-postgres.service';

@Injectable()
export abstract class AbstractUserService {
	constructor(protected readonly prisma: PrismaPostgresService) { }

	async findOneByEmail(email: string): Promise<Partial<Users>> {
		const user = await this.prisma.users.findUnique({
			where: { email },
			include: {
				user_module_access: {
					select: {
						dashboard: true,
						employee_list: true,
						employee_management: true,
						personal_profile: true,
						timesheets: true,
						timesheets_approval: true,
                        chatbot: true,
					},
				},
			},

		});
		if (!user) {
			throw new NotFoundException(`No user with email #${email} exists`);
		}

        let module_access: Modules[] = [];
        if (user.user_module_access !== null) module_access = toKeysFromBoolean(user.user_module_access);
		
        const clean_user = {
			first_name: user.first_name,
			last_name: user.last_name,
			email: user.email,
			role: user.role,
			user_module_access: module_access,
		}

		return clean_user;
	}
}
