const prisma = require('../../prisma/PrismaClient.js');
const { formatDateOnly } = require('../../utils/FormatDate.js');

const VendorRepository = {
    findAll: async ({ skip, take, where, orderBy }) => {
        const rawVendors = 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,
                    }
                },
                createdAt: true,
                updatedAt: true,
                vendor_histories: {
                    where: {
                        deletedAt: null,
                        status: 'active',
                        hospital: {
                            deletedAt: null
                        }
                    },
                    select: {
                        vendor_impression: true,
                        contract_date: true,
                        contract_expired_date: true,
                        hospital: {
                            select: {
                                id: true,
                                name: true,
                                hospital_code: true,
                                address: true,
                                city: {
                                    select: { name: true }
                                },
                                province: {
                                    select: { name: true }
                                }
                            }
                        }
                    }
                }
            },
        });

        // Format tanggal di dalam vendor_histories
        const vendors = rawVendors.map(vendor => ({
            ...vendor,
            vendor_histories: vendor.vendor_histories.map(vh => ({
                ...vh,
                contract_date: formatDateOnly(vh.contract_date),
                contract_expired_date: formatDateOnly(vh.contract_expired_date),
            }))
        }));

        return vendors;
    },

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

    // findById: async (id) => {
    //     return 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,
    //                 }
    //             },
    //             createdAt: true,
    //             updatedAt: true,
    //         },
    //     });
    // },

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

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

module.exports = VendorRepository;