const prisma = require('../../prisma/PrismaClient.js');
const timeLocal = require('../../utils/TimeLocal.js');

const VendorRepository = {
    findAll: async ({ skip, take, where, orderBy }) => {
        const vendors = await prisma.vendor.findMany({
            where,
            skip,
            take,
            orderBy,
            select: {
                id: true,
                name: true,
                name_pt: true,
                strengths: true,
                weaknesses: true,
                website: true,
                user: {
                    select: {
                        id: true,
                        username: true,
                        email: true,
                        firstname: true,
                        lastname: true,
                    }
                },
                _count: {
                    select: {
                        vendor_histories: {
                            where: {
                                deletedAt: null
                            }
                        }
                    }
                },
                createdAt: true,
                updatedAt: true,
            },
        });

        const formattedVendors = vendors.map(v => {
            const { _count, ...rest } = v;
            return {
                ...rest,
                count_hospitals: _count.vendor_histories,
            };
        });

        return formattedVendors;
    },

    countAll: async (where) => {
        return prisma.vendor.count({ where });
    },

    findById: async (id) => {
        const vendor = await prisma.vendor.findFirst({
            where: {
                id,
                deletedAt: null
            },
            select: {
                id: true,
                name: true,
                name_pt: true,
                strengths: true,
                weaknesses: true,
                website: true,
                user: {
                    select: {
                        id: true,
                        username: true,
                        email: true,
                        firstname: true,
                        lastname: true,
                    }
                },
                _count: {
                    select: {
                        vendor_histories: {
                            where: {
                                deletedAt: null
                            }
                        }
                    }
                },
                createdAt: true,
                updatedAt: true,
            },
        });

        const { _count, ...rest } = vendor;
        return {
            ...rest,
            count_hospitals: _count.vendor_histories
        };
    },

    create: async (data) => {
        return prisma.vendor.create({ data });
    },

    update: async (id, data) => {
        return prisma.vendor.update({
            where: { id },
            data
        });
    },

    delete: async (id) => {
        // delete vendor
        const vendor = await prisma.vendor.update({
            where: { id },
            data: {
                deletedAt: timeLocal.now().toDate()
            }
        });

        // Unlink vendor_id di vendor_histories
        await prisma.vendorHistory.updateMany({
            where: {
                vendor_id: id,
                deletedAt: null
            },
            data: {
                vendor_id: null
            }
        });

        return vendor;
    },
};

module.exports = VendorRepository;