import { HttpStatus } from "@nestjs/common";

export class ResponseUtils {
    static successfully(data: any = null, message = 'Success', statusCode = HttpStatus.OK) {
        return {
            statusCode,
            success: true,
            message,
            data,
        };
    }

    static created(data: any = null, message = 'Created') {
        return {
            statusCode: HttpStatus.CREATED,
            success: true,
            message,
            data,
        };
    }

    static paginate<T>(
        payload: { items: T[]; total: number; page: number; limit: number },
        message = 'Success',
        statusCode = HttpStatus.OK,
    ) {
        const { items, total, page, limit } = payload;
        const totalPages = Math.max(1, Math.ceil(total / Math.max(1, limit)));
        const hasNext = page < totalPages;
        const hasPrev = page > 1;

        return {
            statusCode,
            success: true,
            message,
            meta: {
                total,
                page,
                limit,
                totalPages,
                hasNext,
                hasPrev,
                nextPage: hasNext ? page + 1 : null,
                prevPage: hasPrev ? page - 1 : null,
            },
            data: items,
        };
    }

    // static prismaErrors(e: any) {
    //     if (e instanceof Prisma.PrismaClientKnownRequestError) {
    //         switch (e.code) {
    //             case 'P1001':
    //                 this.internalError(
    //                     'Failed to connect to the database. Please check your internet connection or contact support if the issue persists.'
    //                 );
    //
    //             case 'P2024':
    //                 this.internalError(
    //                     'Database connection timed out while waiting for a connection from the pool. Please check your internet connection and try again.'
    //                 );
    //
    //             case 'P2002':
    //                 this.badRequest(
    //                     'Duplicate record detected. The value you’re trying to create already exists.'
    //                 );
    //
    //             default:
    //                 this.internalError(
    //                     'An unexpected database error occurred. Please try again later or contact support.'
    //                 );
    //         }
    //     } else {
    //         this.internalError(
    //             'An unexpected database error occurred. Please try again later or contact support : ' + e
    //         );
    //     }
    // }
}
