import { Configuration, App } from '@midwayjs/core';
import * as koa from '@midwayjs/koa';
import * as validate from '@midwayjs/validate';
import * as info from '@midwayjs/info';
import { join } from 'path';
import { ReportMiddleware } from './middleware/report.middleware';
import * as jwt from '@midwayjs/jwt';
import * as swagger from '@midwayjs/swagger';
import * as redis from '@midwayjs/redis';
import * as orm from '@midwayjs/typeorm';
import { CustomErrorFilter } from './filter/customError.filter';
import { DefaultErrorFilter } from './filter/defaultError.filter';
import { CheckTokenMiddleware } from './middleware/checkToken.middleware';
import { ResponseMiddleware } from './middleware/response.middleware';
@Configuration({
  imports: [
    koa,
    validate,
    jwt,
    redis,
    orm,
    {
      component: info,
      enabledEnvironment: ['local'],
    },
    {
      component: swagger,
      enabledEnvironment: ['local'],
    },
  ],
  importConfigs: [join(__dirname, './config')],
})
export class MainConfiguration {
  @App('koa')
  app: koa.Application;

  async onReady() {
    // add middleware
    this.app.useMiddleware([ReportMiddleware, CheckTokenMiddleware, ResponseMiddleware]);
    // add filter
    this.app.useFilter([CustomErrorFilter, DefaultErrorFilter]);
    // this.app.useFilter([NotFoundFilter, DefaultErrorFilter]);
  }
}
