import { Injectable } from "@nestjs/common";
import { Prisma, PrismaClient } from "@prisma/client";
import { Result } from "src/common/errors/result-error.factory";
import { PrismaPostgresService } from "prisma/postgres/prisma-postgres.service";

type Tx = Prisma.TransactionClient | PrismaClient;

@Injectable()
export class FullNameResolver {
    constructor(private readonly prisma: PrismaPostgresService) { }

    readonly resolveFullName = async (employee_id: number, client?: Tx): Promise<Result<string, string>> => {
        const db = (client ?? this.prisma) as PrismaClient;
        const employee = await db.employees.findUnique({
            where: { id: employee_id },
            select: { user: { select: { first_name: true, last_name: true } } },
        });
        if (!employee) return { success: false, error: `INVALID_EMPLOYEE` }

        const full_name = (employee.user.first_name + " " + employee.user.last_name) || " ";
        return { success: true, data: full_name };
    }
}