'use strict';

const fs = require('fs');
const path = require('path');
const utils = require('./utils')
const routes = require('./routes');
//const redisConnect = require('./service/redis')
const socketConect = require('./service/socket')
const paasRequest = require('./service/paasRequest');
const authenticator = require('./middlewares/authenticator');
const clickHouseClient = require('./service/clickHouseClient')
const schedule = require('./schedule')
// const apiLog = require('./middlewares/api-log');

module.exports.entry = function (app, router, opts) {
    app.fs.logger.log('info', '[FS-AUTH]', 'Inject auth and api mv into router.');

    app.fs.api = app.fs.api || {};
    app.fs.opts = opts || {};
    app.fs.utils = app.fs.utils || {};
    app.fs.api.authAttr = app.fs.api.authAttr || {};
    app.fs.api.logAttr = app.fs.api.logAttr || {};

    // 顺序固定 ↓ 
    //redisConnect(app, opts)
    socketConect(app, opts)

    // 实例其他平台请求方法
    paasRequest(app, opts)

    // clickHouse 数据库 client 
    clickHouseClient(app, opts)

    // 工具类函数
    utils(app, opts)

    // 定时任务
    schedule(app, opts)

    //鉴权中间件
    router.use(authenticator(app, opts));

    // 日志记录
    // router.use(apiLog(app, opts));

    router = routes(app, router, opts);
};

module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Sequelize, models: {} }

    // 模型关系摘出来 初始化之后再定义关系才行
    fs.readdirSync(path.join(__dirname, '/models')).forEach((filename) => {
        require(`./models/${filename}`)(dc)
    });

    const { Department, User, UserResource, Resource, Project, Point,
        PatrolPlan, PatrolRecord, ReportInfo, PatrolPlanUser,
        CheckItems, CheckItemsGroup,
        PatrolTemplate, PatrolTemplateCheckItems, PatrolRecordIssueHandle,
        Device, PointDevice,Network
    } = dc.models;

    PatrolRecord.belongsTo(PatrolPlan, { foreignKey: 'patrolPlanId', targetKey: 'id' });
    PatrolPlan.hasMany(PatrolRecord, { foreignKey: 'patrolPlanId', sourceKey: 'id' });

    PatrolRecord.belongsTo(Project, { foreignKey: 'projectId', targetKey: 'id' });
    Project.hasMany(PatrolRecord, { foreignKey: 'projectId', sourceKey: 'id' });

    PatrolRecordIssueHandle.belongsTo(PatrolRecord, { foreignKey: 'patrolRecordId', targetKey: 'id' });
    PatrolRecord.hasMany(PatrolRecordIssueHandle, { foreignKey: 'patrolRecordId', sourceKey: 'id' });

    PatrolRecord.belongsTo(Point, { foreignKey: 'pointId', targetKey: 'id' });
    Point.hasMany(PatrolRecord, { foreignKey: 'pointId', sourceKey: 'id' });

    UserResource.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' });
    User.hasMany(UserResource, { foreignKey: 'userId', sourceKey: 'id' });

    PatrolPlan.belongsTo(PatrolTemplate, { foreignKey: 'templateId', targetKey: 'id' });
    PatrolTemplate.hasMany(PatrolPlan, { foreignKey: 'templateId', sourceKey: 'id' });

    PatrolTemplate.belongsTo(User, { foreignKey: 'createUserId', targetKey: 'id' });
    User.hasMany(PatrolTemplate, { foreignKey: 'createUserId', sourceKey: 'id' });

    CheckItems.belongsToMany(PatrolTemplate, { through: PatrolTemplateCheckItems, foreignKey: 'checkItemsId', otherKey: 'templateId' });
    PatrolTemplate.belongsToMany(CheckItems, { through: PatrolTemplateCheckItems, foreignKey: 'templateId', otherKey: 'checkItemsId' });

    UserResource.belongsTo(Resource, { foreignKey: 'resourceId', targetKey: 'code' });
    Resource.hasMany(UserResource, { foreignKey: 'resourceId', sourceKey: 'code' });
    Resource.hasMany(Resource, { foreignKey: 'parentResource', sourceKey: 'code' });

    User.belongsTo(Department, { foreignKey: 'departmentId', targetKey: 'id' });
    Department.hasMany(User, { foreignKey: 'departmentId', sourceKey: 'id' });

    Point.belongsTo(Project, { foreignKey: 'projectId', targetKey: 'id' });
    Project.hasMany(Point, { foreignKey: 'projectId', sourceKey: 'id' });

    PatrolPlan.belongsTo(Project, { foreignKey: 'structureId', targetKey: 'id' });
    Project.hasMany(PatrolPlan, { foreignKey: 'structureId', sourceKey: 'id' });

    PatrolPlan.belongsToMany(User, { through: PatrolPlanUser, foreignKey: 'patrolPlanId', otherKey: 'userId' });
    User.belongsToMany(PatrolPlan, { through: PatrolPlanUser, foreignKey: 'userId', otherKey: 'patrolPlanId' });

    ReportInfo.belongsTo(Project, { foreignKey: 'projectId', targetKey: 'id' });
    Project.hasMany(ReportInfo, { foreignKey: 'projectId', sourceKey: 'id' });

    CheckItems.belongsTo(CheckItemsGroup, { foreignKey: 'groupId', targetKey: 'id' });
    CheckItemsGroup.hasMany(CheckItems, { foreignKey: 'groupId', sourceKey: 'id' });

    PointDevice.belongsTo(Point, { foreignKey: 'pointId', targetKey: 'id' });
    Point.hasMany(PointDevice, { foreignKey: 'pointId', sourceKey: 'id' });

    PointDevice.belongsTo(Device, { foreignKey: 'deviceId', targetKey: 'id' });
    Device.hasMany(PointDevice, { foreignKey: 'deviceId', sourceKey: 'id' });

    Device.belongsToMany(Point,{ through: PointDevice, foreignKey: 'deviceId', otherKey: 'pointId' })
    Point.belongsToMany(Device,{ through: PointDevice, foreignKey: 'pointId', otherKey: 'deviceId' })

    Network.belongsTo(Project,{ foreignKey: 'projectId', otherKey: 'id' })
    Project.hasMany(Network,{ foreignKey: 'projectId', sourceKey: 'id' })
};