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

const HospitalRepository = {
    findAll: async ({ skip, take, where, orderBy }) => {
        return await prisma.hospital.findMany({
            where,
            skip,
            take,
            orderBy,
            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 } },
                createdAt: true,
                updatedAt: true,
            }
        });
    },

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

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

    findById: async (id) => {
        return prisma.hospital.findFirst({
            where: {
                id,
                deletedAt: null
            },
            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 } },
                createdAt: true,
                updatedAt: true,
            }
        });
    },

    update: async (id, data) => {
        return prisma.hospital.update({
            where: { id },
            data: {
                name: data.name,
                hospital_code: data.hospital_code,
                type: data.type,
                ownership: data.ownership,
                province: { connect: { id: data.province_id } },
                city: { connect: { id: data.city_id } },
                address: data.address,
                // simrs_type: data.simrs_type,
                contact: data.contact,
                note: data.note,
                image: data.image,
                progress_status: data.progress_status,
            }
        });
    },
};

module.exports = HospitalRepository;