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 maintenance = require('maintenance');
var Errors = require('./lib/errors.js')
let config = require('./config/server.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');

let path = require('path')


let session = expressSession({
	secret: config.sessionSecret,
	resave: true,
	saveUninitialized: true,
	store: new SequelizeStore({ db: sequelize })
})
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(session)
app.use(expAutoSan.all);

var maintoptions = {
	current: config.maintenance,        // current state, default **false**
	httpEndpoint: true,					// expose http endpoint for hot-switch, default **false**,
	url: '/api/v1/kaverti/maintenance',	// if `httpEndpoint` is on, customize endpoint url, default **'/maintenance'**
	api: '/api/v1',						// for rest API, species root URL to apply, default **undefined**
	status: 503,						// status code for response, default **503**
	message: 'will be back'				// response message, default **'sorry, we are on maintenance'**
};

maintenance(app, maintoptions);

if(process.env.NODE_ENV !== 'test' && process.env.NODE_ENV !== 'production') {
	app.use(require('morgan')('dev'))
}
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/settings'))
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/user/login_status', require('./routes/login_status'))
app.use('/api/v1/admin/users', require('./routes/admin_user_list'))
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/user/', require('./routes/admin_user'))
app.use(require('./lib/errorHandler'))
app.use(profanity.init);
function main () {
	let server = app.listen(config.port, () => {
		console.log('Listening on port 0.0.0.0:' + config.port)

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

	sockets.init(app, server, session)
}

if(process.env.NODE_ENV === 'test') {
	sequelize.sync({ force: true }).then(main)
} else {
	main()
}

module.exports = app

