import { Strategy } from 'passport-twitter'

import { Router } from 'express'
import passport from 'passport'

import genericStrategy from '../strategy'

const redirectPath = 'auth/twitter/redirect'
const callbackURL =
  process.env.NODE_ENV !== 'production'
    ? `${process.env.HOST_DEV}/${redirectPath}`
    : `${process.env.HOST}/${redirectPath}`

const strategyOptions = {
  consumerKey: process.env.TWITTER_CONSUMER_KEY,
  consumerSecret: process.env.TWITTER_CONSUMER_SECRET,
  callbackURL: callbackURL,
  includeEmail: true,
}

export function twitterStrategy() {
  return new Strategy(strategyOptions, genericStrategy('twitter'))
}

export const twitterRouter = Router()

twitterRouter.get('/twitter', passport.authenticate('twitter'))
twitterRouter.get(
  '/twitter/redirect',
  passport.authenticate('twitter', { successRedirect: '/museo', failureRedirect: '/' }),
)
