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

const ExecutivesHistoryRepository = {
    findAll: async ({ skip, take, where, orderBy }) => {
        return prisma.executivesHistory.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
                            }
                        }
                    }
                },
                executive_name: true,
                contact: true,
                status: true,
                start_term: true,
                end_term: true,
                createdAt: true,
                updatedAt: true,
            },
        });
    },

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

    findById: async (id) => {
        return prisma.executivesHistory.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
                            }
                        }
                    }
                },
                executive_name: true,
                contact: true,
                status: true,
                start_term: true,
                end_term: true,
                createdAt: true,
                updatedAt: true,
            },
        });
    },

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

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

module.exports = ExecutivesHistoryRepository;