import { Controller, Post } from '@overnightjs/core';
import dotenv from 'dotenv';
import { Request, Response } from 'express';
import AuthService from '../services/AuthService';
dotenv.config();

@Controller('api/auth')
export class AuthController {

  /**
   * Register a new user
   * POST /api/auth/register
   */
  @Post('register')
  async register(req: Request, res: Response) {
    try {
      const result = await AuthService.register(req.body);
      res.status(201).json({
        success: true,
        message: 'User registered successfully with FREE TRIAL. Please check your email for confirmation.',
        user: result,
      });
    } catch (error: any) {
      console.error('Register error:', error);
      res.status(400).json({
        success: false,
        error: error.message || 'Server error during registration',
      });
    }
  }

  /**
   * Login user
   * POST /api/auth/login
   */
  @Post('login')
  async login(req: Request, res: Response) {
    try {
      const result = await AuthService.login(req.body);
      res.status(200).json({
        success: true,
        message: 'Login successful',
        ...result
      });
    } catch (error: any) {
      console.error('Login error:', error);
      const statusCode = error.message.includes('incorrect') ? 401 : 400;
      res.status(statusCode).json({
        success: false,
        error: error.message || 'Server error during login',
      });
    }
  }

  /**
   * Forgot password - send reset token
   * POST /api/auth/forgot-password
   */
  @Post('forgot-password')
  async forgotPassword(req: Request, res: Response) {
    try {
      const { email, language } = req.body;
      const result = await AuthService.forgotPassword(email, language);
      res.status(200).json({
        success: true,
        message: result.message,
      });
    } catch (error: any) {
      console.error('Forgot password error:', error);
      res.status(500).json({
        success: false,
        error: error.message || 'Server error',
      });
    }
  }

  /**
   * Reset password with token
   * POST /api/auth/reset-password
   */
  @Post('reset-password')
  async resetPassword(req: Request, res: Response) {
    try {
      const result = await AuthService.resetPassword(req.body);
      res.status(200).json({
        success: true,
        message: result.message,
      });
    } catch (error: any) {
      console.error('Reset password error:', error);
      const statusCode = error.message.includes('invalid') || error.message.includes('expired') ? 400 : 500;
      res.status(statusCode).json({
        success: false,
        error: error.message || 'Server error',
      });
    }
  }

  /**
   * Verify token
   * POST /api/auth/verify
   */
  @Post('verify')
  async verify(req: Request, res: Response) {
    try {
      const result = await AuthService.verifyToken(req.body);
      res.status(200).json({
        success: true,
        ...result
      });
    } catch (error: any) {
      res.status(401).json({
        success: false,
        error: error.message || 'Invalid or expired token',
      });
    }
  }

  /**
   * Get user profile
   * POST /api/auth/profile
   */
  @Post('profile')
  async getProfile(req: Request, res: Response) {
    try {
      const { token } = req.body;
      const result = await AuthService.getProfile(token);
      res.status(200).json({
        success: true,
        ...result
      });
    } catch (error: any) {
      res.status(401).json({
        success: false,
        error: error.message || 'Invalid or expired token',
      });
    }
  }

  /**
   * Update user profile
   * POST /api/auth/update-profile
   */
  @Post('update-profile')
  async updateProfile(req: Request, res: Response) {
    try {
      const result = await AuthService.updateProfile(req.body);
      res.status(200).json({
        success: true,
        ...result
      });
    } catch (error: any) {
      console.error('Update profile error:', error);
      res.status(500).json({
        success: false,
        error: error.message || 'Server error',
      });
    }
  }

  /**
   * Change password
   * POST /api/auth/change-password
   */
  @Post('change-password')
  async changePassword(req: Request, res: Response) {
    try {
      const result = await AuthService.changePassword(req.body);
      res.status(200).json({
        success: true,
        message: result.message,
      });
    } catch (error: any) {
      console.error('Change password error:', error);
      const statusCode = error.message.includes('incorrect') ? 401 : 400;
      res.status(statusCode).json({
        success: false,
        error: error.message || 'Server error',
      });
    }
  }
}
