import { Injectable } from "@nestjs/common";
import { Result } from "src/common/errors/result-error.factory";
import { EmailToIdResolver } from "src/common/mappers/email-id.mapper";
import { PrismaPostgresService } from "prisma/postgres/prisma-postgres.service";

@Injectable()
export class HomePageService {
    constructor(
        private readonly prisma: PrismaPostgresService,
        private readonly emailresolver: EmailToIdResolver,
    ) { }

    buildHomePageHelpMessage = async (email: string): Promise<Result<string[], string>> => {
        const user_id = await this.emailresolver.resolveUserIdWithEmail(email);
        if (!user_id.success) return { success: false, error: 'INVALID_EMAIL' };

        const module_access = await this.prisma.userModuleAccess.findUnique({
            where: { user_id: user_id.data },
            select: {
                dashboard: true,
                employee_list: true,
                employee_management: true,
                personal_profile: true,
                timesheets: true,
                timesheets_approval: true,
            },
        });
        if (!module_access) return { success: false, error: 'MODULE_ACCESS_NOT_FOUND' };

        const help_message: string[] = [];

        if (module_access.dashboard) help_message.push('dashboard');
        if (module_access.personal_profile) help_message.push('personal_profile');
        if (module_access.timesheets) help_message.push('timesheets');
        if (module_access.employee_list) help_message.push('employee_list');
        if (module_access.employee_management) help_message.push('employee_management');
        if (module_access.timesheets_approval) help_message.push('timesheets_approval');

        return { success: true, data: help_message };
    }
}