//const Logger = require('../logs/clsLogger').clsLogger;
const smgUtils = require('../utils/smgutils');
require('linqjs');

const djPlayerDB = class djPlayerDB {
    constructor({ db, moment, config, logger } = {}) {
        this.db = db;
        this.moment = moment;
        this.config = config;
        this.Schedules = this.db.Schedules;
        this.Schedules_Details = this.db.Schedules_Details;
        this._sequelize = this.db._sequelize;
        this.logger = logger;
        this.sutils = new smgUtils;
    }

    async getSchedules() {
        try {
            let nowj = this.moment().utc(true).toJSON().substr(0, 11);
            let schedules = await (await this.Schedules.findAll({include: ['Schedules_Details'], where: { deviceID: this.config.Settings.deviceID }})).select((c) => {
                return {
                    scheduleID: c.scheduleID,
                    scheduleName: c.scheduleName,
                    deviceID: c.deviceID,
                    startDate: this.moment(c.startDate + 'T' + c.startTime).utc(true),
                    endDate: this.moment(c.endDate + 'T' + c.endTime).utc(true),
                    startTime: this.moment(nowj + c.startTime).utc(true),
                    endTime: this.moment(nowj + c.endTime).utc(true),
                    daysEffected: c.daysEffected,
                    isActive: c.isActive,
                    hasPriority: c.hasPriority,
                    Volume: c.Volume,
                    isShuffleEnabled: c.isShuffleEnabled,
                    lastUpdateDate:c.lastUpdateDate,
                    Schedules_Details: c.Schedules_Details.select((d) => d.dataValues).orderBy(e => e.orderID)
                }
            });
            if(schedules.length > 0) {
                return schedules;
            }
        } catch (error) {
            this.logger.PlayerEventsDJ(`Error fetching schedules from djPlayerDB: ${error.message}`);
            return [];
        }
    }
};
const util = require('util');
const EventEmitter = require('events').EventEmitter;
util.inherits(djPlayerDB, EventEmitter);
module.exports = djPlayerDB;