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

const VendorHistoryRepository = {
    findAll: async ({ skip, take, where, orderBy }) => {
        return prisma.vendorHistory.findMany({
            where,
            skip,
            take,
            orderBy,
            select: {
                id: true,
                hospital: {
                    select: {
                        id: true,
                        name: true,
                        hospital_code: true,
                        type: true,
                        ownership: true,
                        province: {
                            select: {
                                id: true,
                                name: true
                            }
                        },
                        city: {
                            select: {
                                id: true,
                                name: true
                            }
                        },
                        address: true,
                        simrs_type: true,
                        contact: true,
                        image: true,
                        progress_status: true,
                        note: true,
                        user: {
                            select: {
                                id: true,
                                username: true
                            }
                        }
                    }
                },
                vendor: {
                    select: {
                        id: true,
                        name: true,
                        name_pt: true,
                        strengths: true,
                        weaknesses: true,
                        website: true,
                        user: {
                            select: {
                                id: true,
                                username: true
                            }
                        }
                    }
                },
                vendor_impression: true,
                status: true,
                contract_date: true,
                contract_expired_date: true,
                createdAt: true,
                updatedAt: true,
            },
        });
    },

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

    findById: async (id) => {
        return prisma.vendorHistory.findFirst({
            where: {
                id,
                deletedAt: null
            },
            select: {
                id: true,
                hospital: {
                    select: {
                        id: true,
                        name: true,
                        hospital_code: true,
                        type: true,
                        ownership: true,
                        province: {
                            select: {
                                id: true,
                                name: true
                            }
                        },
                        city: {
                            select: {
                                id: true,
                                name: true
                            }
                        },
                        address: true,
                        simrs_type: true,
                        contact: true,
                        image: true,
                        progress_status: true,
                        note: true,
                        user: {
                            select: {
                                id: true,
                                username: true
                            }
                        }
                    }
                },
                vendor: {
                    select: {
                        id: true,
                        name: true,
                        name_pt: true,
                        strengths: true,
                        weaknesses: true,
                        website: true,
                        user: {
                            select: {
                                id: true,
                                username: true
                            }
                        }
                    }
                },
                vendor_impression: true,
                status: true,
                contract_date: true,
                contract_expired_date: true,
                createdAt: true,
                updatedAt: true,
            },
        });
    },

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

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

module.exports = VendorHistoryRepository;