import { Injectable } from "@nestjs/common";
import { Result } from "src/common/errors/result-error.factory";
import { EmailToIdResolver } from "src/common/mappers/email-id.mapper";
import { ModuleAccess } from "src/identity-and-account/user-module-access/module-acces.dto";
import { PrismaPostgresService } from "prisma/postgres/prisma-postgres.service";

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

    async updateModuleAccess(email: string, dto: ModuleAccess, employee_email?: string): Promise<Result<ModuleAccess, string>> {
        const account_email = employee_email ?? email;
        const user_id = await this.emailResolver.resolveUserIdWithEmail(account_email);
        if (!user_id.success) return { success: false, error: 'EMPLOYEE_NOT_FOUND' };

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

        const updated_access:ModuleAccess = await this.prisma.userModuleAccess.update({
            where: { id: orignal_access.id },
            data: {
                timesheets: dto.timesheets,
                timesheets_approval: dto.timesheets_approval,
                employee_list: dto.employee_list,
                employee_management: dto.employee_management,
                personal_profile: dto.personal_profile,
                dashboard: dto.dashboard,
            },
            select: {
                timesheets: true,
                timesheets_approval: true,
                employee_list: true,
                employee_management: true,
                personal_profile: true,
                dashboard: true,
            }
        })
        return { success: true, data: updated_access };
    }

    async revokeModuleAccess(email: string, employee_email?: string): Promise<Result<ModuleAccess, string>> {
        const account_email = employee_email ?? email;
        const user_id = await this.emailResolver.resolveUserIdWithEmail(account_email);
        if (!user_id.success) return { success: false, error: 'EMPLOYEE_NOT_FOUND' };

        const access = await this.prisma.userModuleAccess.findUnique({
            where: { user_id: user_id.data },
            select: { id: true },
        });
        if (!access) return { success: false, error: 'MODULE_ACCESS_NOT_FOUND' };

        const revoked_access: ModuleAccess = await this.prisma.userModuleAccess.update({
            where: { id: access.id },
            data: {
                timesheets: false,
                timesheets_approval: false,
                employee_list: false,
                employee_management: false,
                personal_profile: false,
                dashboard: false,
            },
            select: {
                timesheets: true,
                timesheets_approval: true,
                employee_list: true,
                employee_management: true,
                personal_profile: true,
                dashboard: true,
            }
        });

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