const express = require('express');
const cors = require('cors');
const app = express();
const passport = require('passport');
const path = require('path');
const fs = require('fs');
const bodyParser = require('body-parser');
const db = require('./db/models');
const config = require('./config');
const swaggerUI = require('swagger-ui-express');
const swaggerJsDoc = require('swagger-jsdoc');

const authRoutes = require('./routes/auth');
const fileRoutes = require('./routes/file');
const searchRoutes = require('./routes/search');
const pexelsRoutes = require('./routes/pexels');

const organizationForAuthRoutes = require('./routes/organizationLogin');

const openaiRoutes = require('./routes/openai');

const contactFormRoutes = require('./routes/contactForm');

const usersRoutes = require('./routes/users');

const accountsRoutes = require('./routes/accounts');

const announcementsRoutes = require('./routes/announcements');

const barter_offersRoutes = require('./routes/barter_offers');

const delayed_paymentsRoutes = require('./routes/delayed_payments');

const islamic_finance_libraryRoutes = require('./routes/islamic_finance_library');

const loan_applicationsRoutes = require('./routes/loan_applications');

const partnersRoutes = require('./routes/partners');

const projectsRoutes = require('./routes/projects');

const transactionsRoutes = require('./routes/transactions');

const rolesRoutes = require('./routes/roles');

const permissionsRoutes = require('./routes/permissions');

const kooperatifRoutes = require('./routes/kooperatif');

const getBaseUrl = (url) => {
  if (!url) return '';
  return url.endsWith('/api') ? url.slice(0, -4) : url;
};

const options = {
  definition: {
    openapi: '3.0.0',
    info: {
      version: '1.0.0',
      title: 'yarkoop',
      description:
        'yarkoop Online REST API for Testing and Prototyping application. You can perform all major operations with your entities - create, delete and etc.',
    },
    servers: [
      {
        url: getBaseUrl(process.env.NEXT_PUBLIC_BACK_API) || config.swaggerUrl,
        description: 'Development server',
      },
    ],
    components: {
      securitySchemes: {
        bearerAuth: {
          type: 'http',
          scheme: 'bearer',
          bearerFormat: 'JWT',
        },
      },
      responses: {
        UnauthorizedError: {
          description: 'Access token is missing or invalid',
        },
      },
    },
    security: [
      {
        bearerAuth: [],
      },
    ],
  },
  apis: ['./src/routes/*.js'],
};

const specs = swaggerJsDoc(options);
app.use(
  '/api-docs',
  function (req, res, next) {
    swaggerUI.host =
      getBaseUrl(process.env.NEXT_PUBLIC_BACK_API) || req.get('host');
    next();
  },
  swaggerUI.serve,
  swaggerUI.setup(specs),
);

app.use(cors({ origin: true }));
require('./auth/auth');

app.use(bodyParser.json());

app.use('/api/auth', authRoutes);
app.use('/api/file', fileRoutes);
app.use('/api/pexels', pexelsRoutes);
app.enable('trust proxy');

app.use(
  '/api/users',
  passport.authenticate('jwt', { session: false }),
  usersRoutes,
);

app.use(
  '/api/accounts',
  passport.authenticate('jwt', { session: false }),
  accountsRoutes,
);

app.use(
  '/api/announcements',
  passport.authenticate('jwt', { session: false }),
  announcementsRoutes,
);

app.use(
  '/api/barter_offers',
  passport.authenticate('jwt', { session: false }),
  barter_offersRoutes,
);

app.use(
  '/api/delayed_payments',
  passport.authenticate('jwt', { session: false }),
  delayed_paymentsRoutes,
);

app.use(
  '/api/islamic_finance_library',
  passport.authenticate('jwt', { session: false }),
  islamic_finance_libraryRoutes,
);

app.use(
  '/api/loan_applications',
  passport.authenticate('jwt', { session: false }),
  loan_applicationsRoutes,
);

app.use(
  '/api/partners',
  passport.authenticate('jwt', { session: false }),
  partnersRoutes,
);

app.use(
  '/api/projects',
  passport.authenticate('jwt', { session: false }),
  projectsRoutes,
);

app.use(
  '/api/transactions',
  passport.authenticate('jwt', { session: false }),
  transactionsRoutes,
);

app.use(
  '/api/roles',
  passport.authenticate('jwt', { session: false }),
  rolesRoutes,
);

app.use(
  '/api/permissions',
  passport.authenticate('jwt', { session: false }),
  permissionsRoutes,
);

app.use(
  '/api/kooperatif',
  passport.authenticate('jwt', { session: false }),
  kooperatifRoutes,
);

app.use(
  '/api/openai',
  passport.authenticate('jwt', { session: false }),
  openaiRoutes,
);

app.use('/api/contact-form', contactFormRoutes);

app.use(
  '/api/search',
  passport.authenticate('jwt', { session: false }),
  searchRoutes,
);

app.use('/api/org-for-auth', organizationForAuthRoutes);

const publicDir = path.join(__dirname, '../public');

if (fs.existsSync(publicDir)) {
  app.use('/', express.static(publicDir));

  app.get('*', function (request, response) {
    response.sendFile(path.resolve(publicDir, 'index.html'));
  });
}

const PORT = process.env.NODE_ENV === 'dev_stage' ? 3000 : 8080;

db.sequelize.sync().then(function () {
  app.listen(PORT, () => {
    console.log(`Listening on port ${PORT}`);
  });
});

module.exports = app;
