import express from 'express'
import { loginController } from '../controllers/loginController.js'
import { authMiddleware } from '../middlewares/auth.js'
import { OtpController } from '../../src/controllers/otpController/otpcontroller.js'
import { OtpVerifyController } from '../../src/controllers/otpController/otpVerifycontroller.js'
import { RegisterController } from '../controllers/registercontroller.js';
import { VerifyEmailController } from '../controllers/verifyemailcontroller.js';
import { ResetPasswordController } from '../controllers/otpController/resetpasswordcontroller.js';

// 🧱 Controller Instances
const registerController = new RegisterController();
const verifyEmailController = new VerifyEmailController();
const resetPasswordController = new ResetPasswordController();;

const router = express.Router()

const controller_login_post = new loginController()
const otpController = new OtpController()
const otpVerifyController = new OtpVerifyController()

router.post('/login/login', async (req, res) => {const result = await controller_login_post.onNavigate(req, res); return res.json(result)})

router.post('/login/otp/send', async (req, res) => {
  const result = await otpController.onSendOtp(req, res)
  if (result) return res.json(result)
})
router.post('/login/register', async (req, res) => {
  const result = await registerController.onNavigate(req, res);
  if (result) return res.json(result);
});

router.get('/login/verify-email', async (req, res) => {
  const result = await verifyEmailController.onVerifyEmail(req, res);
  if (result?.code && result.code !== '200') return res.status(400).send(result.message_th);
  // ถ้า controller ส่ง HTML string กลับมา → render ตรง ๆ
  if (typeof result === 'string') return res.send(result);
  return res.json(result);
});


router.post('/login/reset-password', async (req, res) => {
  const result = await resetPasswordController.onNavigate(req, res);
  if (result) return res.json(result);
});


router.post('/login/otp/verify', async (req, res) => {
  const result = await otpVerifyController.onNavigate(req, res)
  if (result) return res.json(result)
})

export default router
