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 BankCodesResolver {
    constructor(private readonly prisma: PrismaPostgresService) { }

    //find id and modifier by type
    readonly findIdAndModifierByType = async (
        type: string,
        client?: Tx
    ): Promise<Result<{ id: number; modifier: number }, string>> => {
        const db = (client ?? this.prisma) as PrismaClient;
        const bank = await db.bankCodes.findFirst({
            where: { type },
            select: { id: true, modifier: true },
        });
        if (!bank) return { success: false, error: `INVALID_TYPE` };

        return { success: true, data: { id: bank.id, modifier: bank.modifier } };
    };

    //finds only id by type
    readonly findBankCodeIDByType = async (type: string, client?: Tx
    ): Promise<Result<number, string>> => {
        const db = (client ?? this.prisma) as PrismaClient;
        const bank_code = await db.bankCodes.findFirst({
            where: { type },
            select: { id: true },
        });
        if (!bank_code) return { success: false, error: `INVALID_TYPE` };

        return { success: true, data: bank_code.id };
    }

    readonly findTypeByBankCodeId = async (bank_code_id: number, client?: Tx
    ): Promise<Result<string, string>> => {
        const db = (client ?? this.prisma) as PrismaClient;
        const bank_code = await db.bankCodes.findFirst({
            where: { id: bank_code_id },
            select: { type: true },
        });
        if (!bank_code) return { success: false, error: `INVALID_TYPE` }

        return { success: true, data: bank_code.type };
    }


}