import express, { Application, Request, Response } from 'express';
import cors from 'cors';
import globalErrorHandler from './middlwear/GlobalErrorHandler';
import notFound from './middlwear/notFound';
import router from './routers';
import cookieParser from 'cookie-parser';
import compression from 'compression';

const app: Application = express();

//parser
app.use(compression());
app.use(cors({
  origin: [
  'http://localhost:3000',

  // existing vercel previews
  'https://institution-dashboard-liard.vercel.app',
  'https://institution-profile.vercel.app',
  'https://your-institution.vercel.app',
  'https://catalyst-seven-kappa.vercel.app',

  // main domain
  'https://codebiruni.com',
  'https://www.codebiruni.com',

  // platform subdomains
  'https://edux-dashboard.codebiruni.com',
  'https://edux-profile.codebiruni.com',
  'https://edux-student.codebiruni.com',

  // client apps
  'https://edux-mclient.codebiruni.com', 
  'https://edux-sclient.codebiruni.com', 
]
,
  credentials: true,
}));
app.use(express.json());

app.use(cookieParser());
// routers
app.use('/api/v1', router);

// SSLCommerz payment gateway initialization
// const store_id = 'devel6836bbf891d39'
// const store_passwd = 'devel6836bbf891d39@ssl'
// const is_live = false

// app.get('/init', (req, res) => {
//     const data = {
//         total_amount: 100,
//         currency: 'BDT',
//         tran_id: 'REF123', // use unique tran_id for each api call
//         success_url: 'http://localhost:3030/success',
//         fail_url: 'http://localhost:3030/fail',
//         cancel_url: 'http://localhost:3030/cancel',
//         ipn_url: 'http://localhost:3030/ipn',
//         shipping_method: 'Courier',
//         product_name: 'Computer.',
//         product_category: 'Electronic',
//         product_profile: 'general',
//         cus_name: 'Customer Name',
//         cus_email: 'customer@example.com',
//         cus_add1: 'Dhaka',
//         cus_add2: 'Dhaka',
//         cus_city: 'Dhaka',
//         cus_state: 'Dhaka',
//         cus_postcode: '1000',
//         cus_country: 'Bangladesh',
//         cus_phone: '01711111111',
//         cus_fax: '01711111111',
//         ship_name: 'Customer Name',
//         ship_add1: 'Dhaka',
//         ship_add2: 'Dhaka',
//         ship_city: 'Dhaka',
//         ship_state: 'Dhaka',
//         ship_postcode: 1000,
//         ship_country: 'Bangladesh',
//     };
//     const sslcz = new SSLCommerzPayment(store_id, store_passwd, is_live)
//     sslcz.init(data).then(apiResponse => {
//         // Redirect the user to payment gateway
//         let GatewayPageURL = apiResponse.GatewayPageURL
//         res.redirect(GatewayPageURL)
//         console.log('Redirecting to: ', GatewayPageURL)
//     });
// })

// Handle the success, fail, and cancel URLs

app.get('/', (req: Request, res: Response) => {
  res.send('NOORE RESALAT MODEL MADRASHA SERVER IS RUNNING!...');
});

app.use(globalErrorHandler);
app.use(notFound);

export default app;
