console.log(`                                                                                                                                
                                                                                                                                
KKKKKKKKK    KKKKKKK                                                                                      tttt            iiii  
K:::::::K    K:::::K                                                                                   ttt:::t           i::::i 
K:::::::K    K:::::K                                                                                   t:::::t            iiii  
K:::::::K   K::::::K                                                                                   t:::::t                  
KK::::::K  K:::::KKK  aaaaaaaaaaaaavvvvvvv           vvvvvvv eeeeeeeeeeee    rrrrr   rrrrrrrrr   ttttttt:::::ttttttt    iiiiiii 
  K:::::K K:::::K     a::::::::::::av:::::v         v:::::vee::::::::::::ee  r::::rrr:::::::::r  t:::::::::::::::::t    i:::::i 
  K::::::K:::::K      aaaaaaaaa:::::av:::::v       v:::::ve::::::eeeee:::::eer:::::::::::::::::r t:::::::::::::::::t     i::::i 
  K:::::::::::K                a::::a v:::::v     v:::::ve::::::e     e:::::err::::::rrrrr::::::rtttttt:::::::tttttt     i::::i 
  K:::::::::::K         aaaaaaa:::::a  v:::::v   v:::::v e:::::::eeeee::::::e r:::::r     r:::::r      t:::::t           i::::i 
  K::::::K:::::K      aa::::::::::::a   v:::::v v:::::v  e:::::::::::::::::e  r:::::r     rrrrrrr      t:::::t           i::::i 
  K:::::K K:::::K    a::::aaaa::::::a    v:::::v:::::v   e::::::eeeeeeeeeee   r:::::r                  t:::::t           i::::i 
KK::::::K  K:::::KKKa::::a    a:::::a     v:::::::::v    e:::::::e            r:::::r                  t:::::t    tttttt i::::i 
K:::::::K   K::::::Ka::::a    a:::::a      v:::::::v     e::::::::e           r:::::r                  t::::::tttt:::::ti::::::i
K:::::::K    K:::::Ka:::::aaaa::::::a       v:::::v       e::::::::eeeeeeee   r:::::r                  tt::::::::::::::ti::::::i
K:::::::K    K:::::K a::::::::::aa:::a       v:::v         ee:::::::::::::e   r:::::r                    tt:::::::::::tti::::::i
KKKKKKKKK    KKKKKKK  aaaaaaaaaa  aaaa        vvv            eeeeeeeeeeeeee   rrrrrrr                      ttttttttttt  iiiiiiii`)
console.log("Initializing")
let express = require('express')
let app = express()
var profanity = require('profanity-middleware');
let { sequelize } = require('./models')
let sockets = require('./lib/sockets')
var Errors = require('./lib/errors.js')
let config = require('./config/server.js')
const expressSwagger = require('express-swagger-generator')(app);
let swaggerJsdoc = require('swagger-jsdoc')
const jwt = require('jsonwebtoken');

const options = {
    definition: {
        openapi: "3.0.0",
        info: {
            title: "Kaverti",
            version: "1.0.0",
            description:
                "Kaverti Swagger",
            contact: {
                name: "Kaverti",
                url: "https://kaverti.com",
                email: "troplo@flowinity.com",
            },
        },
        servers: [
            {
                url: "http://localhost:23981",
            },
        ],
    },
    apis: ["./routes/*.js"],
};

//Middle-ware
let bodyParser = require('body-parser')
let expressSession = require('express-session')
let SequelizeStore = require('connect-session-sequelize')(expressSession.Store)
let compression = require('compression')
let helmet = require('helmet')
var Recaptcha = require('express-recaptcha').RecaptchaV3;
var recaptcha = new Recaptcha('6LdlbrwZAAAAAKvtcVQhVl_QaNOqmQ4PgyW3SKHy', '6LdlbrwZAAAAAMAWPVDrL8eNPxrws6AMDtLf1bgd');
const expAutoSan = require('express-autosanitizer');
const swaggerUi = require('swagger-ui-express');
let path = require('path')
const passport = require('passport');
const specs = swaggerJsdoc(options);
const csrf = require('csurf')
const csrfProtection = csrf({ cookie: true })
if(process.env.NODE_ENV === 'production') {
	app.set('trust proxy', 1);
}

app.use(helmet())
app.use(compression())
app.use(bodyParser.json({ limit: '5mb' }))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(expAutoSan.all);

if(process.env.NODE_ENV !== 'test' && process.env.NODE_ENV !== 'production') {
	app.use(require('morgan')('dev'))
}
if(!config.maintenance) {
    app.use('/api/v1/user/', require('./routes/user'))
    app.use('/api/v1/passkey', require('./routes/user_passkey'))
    app.use('/api/v1/admin/admin_token', require('./routes/admin_token'))
    app.use('/api/v1/admin/passkey', require('./routes/user_passkey'))
    app.use('/api/v1/forums/category', require('./routes/category'))
    app.use('/api/v1/forums/thread', require('./routes/thread'))
    app.use('/api/v1/users/notification', require('./routes/notification'))
    app.use('/api/v1/forums/post', require('./routes/post'))
    app.use('/api/v1/kaverti/state', require('./routes/state'))
    app.use('/api/v1/users/report', require('./routes/report'))
    app.use('/api/v1/users/unban-request', require('./routes/UnbanRequest'))
    app.use('/api/v1/admin/ban', require('./routes/ban'))
    app.use('/api/v1/kaverti/search', require('./routes/search'))
    app.use('/api/v1/log', require('./routes/log'))
    app.use('/api/v1/forums/poll', require('./routes/poll'))
    app.use('/api/v1/forums/link_preview', require('./routes/link_preview'))
    app.use('/api/v1/kaverti/stats', require('./routes/stats'))
    app.use('/api/v1/users/login_status', require('./routes/login_status'))
    app.use('/api/v1/users/', require('./routes/userutils'))
    app.use('/api/v1/admin/killsession', require('./routes/admin_kill_session'))
    app.use('/api/v1/admin/ban', require('./routes/ban'))
    app.use('/api/v1/kaverti/job-apply', require('./routes/StaffApplications'))
    app.use('/api/v1/admin/', require('./routes/admin'))
    app.use('/api/v1/users/render', require('./routes/avatar'))
    app.use('/api/v1/users/render/', require('./routes/avatar'))
    app.use('/api/v1/userinfo', require('./routes/userinfo'))
    app.use('/api/v1/wall', require('./routes/user_wall'))
    app.use('/api/v1/chat/conversation', require('./routes/conversation'));
    app.use('/api/v1/chat/message', require('./routes/message'));
    app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs, {explorer: true}));
    app.use('/api/v1/teams/', require('./routes/team'))
    app.use('/api/v1/teams/admin/', require('./routes/team_admin'))
    app.use('/api/v1/teams/wall/', require('./routes/team_wall'))
    app.use('/api/v1/marketplace', require('./routes/marketplace'))
    app.use('/api/v1/inventory', require('./routes/inventory'))
    app.use('/api/v1/transactions', require('./routes/transactions'))
    app.use(require('./lib/errorHandler'))
    app.set('trust proxy', true)
} else {
    app.use('/api/v1/userinfo', require('./routes/userinfo'))
    app.use('/api/v1/kaverti/state', require('./routes/state'))
    app.use('/api/v1/', require('./routes/maintenance'))
    app.use(require('./lib/errorHandler'))
    app.set('trust proxy', true)
}
function main () {
	let server = app.listen(config.port, () => {
        console.log('Initialized')
        console.log('Listening on port 0.0.0.0:' + config.port)

		app.locals.appStarted = true
		app.emit('appStarted')
	})

	sockets.init(app, server)
}
if(process.env.NODE_ENV === 'test') {
	sequelize.sync({ force: true }).then(main)
} else {
	main()
}
module.exports = app

