const MONGO_DB_URL = "mongo.lpaconsulting.fr"
const CAPTCHA_ERROR_COLLECTION_PREFIX = "CAPTCHA_ERROR_"
const BLACK_LIST = "BLACK_LIST"
const DB_NAME = "appointment"
const COLLECTION_NAME = "appointment"
const ACCEPTED_APPOINTMENT_LIST = "ACCEPTED_APPOINTMENT_LIST"
const EMAIL_LIST = "EMAIL_LIST"
const DESTINATION_EMAIL_LIST = "DESTINATION_EMAIL_LIST"

const {MongoClient} = require('mongodb');
const ReserveResultPojo = require("../models/ReserveResultPojo");
require("../models/ContactPojo");

function formatDate(date) {
    let d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2)
        month = '0' + month;
    if (day.length < 2)
        day = '0' + day;

    return [year, month, day].join('-');
}

class MongoManager {
    constructor() {
        //init mongoDb
        let uri = `mongodb://appointment:Rdv%402022@${MONGO_DB_URL}/appointment`
        this.client = new MongoClient(uri)
    }

    async connect() {
        await this.client.connect()
        this.db = this.client.db("appointment")
    }

   async getAllSuccessfulItemsForDay(day) {
        return await this.db.collection(day).find().toArray()
    }

    async saveReserveToDb(reservePojo) {
        let collectionName = formatDate(new Date())
        return await this.db.collection(collectionName).replaceOne({'_id': reservePojo.id,}, reservePojo, {upsert: true})
    }
}


module.exports = {MongoManager, formatDate}