const router = require("express").Router();
const authController = require("../controllers/auth.controller.js");
const socialAuthController = require("../controllers/social-auth.controller.js");
const { isAuthenticated, allowRoles } = require("../middlewares/authenticate.js");
const { upload } = require("../middlewares/multer.middleware.js");

// POST Routes
router.post(
  "/",
  isAuthenticated,
  /*  #swagger.tags = ['Auth']
      #swagger.description = 'Get the currently authenticated user'
  */
  authController.currentUser
);

router.post(
  "/register",
  /*  #swagger.tags = ['Auth']
      #swagger.description = 'Register a new user'
      #swagger.parameters['body'] = {
        in: 'body',
        required: true,
        schema: {
          fullName: "John Doe",
          email: "john@gmail.com",
          password: "secret123",
          phone: "+19 1234567890",
          userName: "john_doe",
          fullAddress: {
            address: "123 Main Street, Apartment 4B",
            city: "Bhopal",
            state: "Madhya Pradesh",
            pincode: "462001"
          },
          userImage: {
            id: "img_001",
            name: "avatar.png",
            mime_type: "image/png",
            path: "/uploads/v_dev.png",
            width: 256,
            height: 256,
            size_bytes: 45231
          }
        }
      }
  */
  upload.single("profileImage"),
  authController.register
);

// Register Vendor (Public - no auth required)
// router.post(
//   "/register/vendor",
//   /*  #swagger.tags = ['Auth']
//       #swagger.description = 'Register a new vendor (public - no auth required). Status will be Pending until approved by admin.'
//       #swagger.parameters['body'] = {
//         in: 'body',
//         required: true,
//         schema: {
//           vendorName: "ABC Transport",
//           displayName: "ABC Cabs",
//           email: "abc@transport.com",
//           phone: "+919876543210",
//           password: "secret123",
//           operationalZone: "65af32bc91d4f2a8c1d231ad",
//           registeredAddress: "123 Main St, Bhopal"
//         }
//       }
//   */
//   authController.registerVendor
// );


router.post(
  "/send-otp",

  /*  #swagger.tags = ['Auth']
      #swagger.description = 'Send OTP (Email or Phone)'
      #swagger.parameters['body'] = {
        in: 'body',
        schema: {
          $identifier: "+919876543210",
          $type: "phone"
        }
      }
  */
  authController.sendAuthOtp
);

router.post(
  "/login",

  /*  #swagger.tags = ['Auth']
      #swagger.description = 'Unified Login (Email/Pass OR Phone/OTP)'
      #swagger.parameters['body'] = {
        in: 'body',
        schema: {
            $email: "hltech@gmail.com",
            $password: "6543210",
            $phone: "+919876543210",
            $otp: "123456"
        }
      }
  */
  authController.login
);

// router.post(
//   "/login/user",
//   /*  #swagger.tags = ['Auth']
//       #swagger.description = 'Dedicated Login for Users via Mobile/OTP (Auto-registers new users)'
//       #swagger.parameters['body'] = {
//         in: 'body',
//         schema: {
//             $phone: "+919876543210",
//             $otp: "1234"
//         }
//       }
//   */
//   authController.userMobileLogin
// );

router.post(
  "/login/driver",
  /*  #swagger.tags = ['Auth']
      #swagger.description = 'Dedicated Login for Drivers via Mobile/OTP (Requires pre-registration)'
      #swagger.parameters['body'] = {
        in: 'body',
        schema: {
            $phone: "+919876543210",
            $otp: "1234"
        }
      }
  */
  authController.driverMobileLogin
);

router.get("/verify-license", authController.verifySystemLicense);



router.post(
  "/email/otp/verify",
  /*  #swagger.tags = ['Auth']
      #swagger.description = 'Verify the OTP sent to email'
      #swagger.parameters['body'] = {
        in: 'body',
        schema: {
          $email: "john@gmail.com",
          $otp: "123456"
        }
      }
  */
  authController.verifyEmailOtp
);

router.post(
  "/mobile/otp/verify",
  /*  #swagger.tags = ['Auth']
      #swagger.description = 'Verify the OTP sent to mobile'
      #swagger.parameters['body'] = {
        in: 'body',
        schema: {
          $phone: "+919876543210",
          $otp: "123456"
        }
      }
  */
  authController.verifyMobileOtp
);

router.post(
  "/email",
  /*  #swagger.tags = ['Auth']
      #swagger.description = 'Send an email with optional file attachments'
      #swagger.consumes = ['multipart/form-data']
      #swagger.parameters['email'] = { in: 'formData', required: true, type: 'string' }
      #swagger.parameters['subject'] = { in: 'formData', required: false, type: 'string' }
      #swagger.parameters['message'] = { in: 'formData', required: true, type: 'string' }
      #swagger.parameters['attachments'] = {
          in: 'formData',
          type: 'array',
          items: { type: 'file' },
          description: 'Optional file attachments (e.g. PDF, Images)'
      }
  */
  isAuthenticated,
  allowRoles("super-admin", "zone-admin", "vendor"),
  upload.array("attachments"),
  authController.sendEmail
);
router.post(
  "/refresh-token",
  /*  #swagger.tags = ['Auth']
      #swagger.description = 'Refresh access token using refresh token'
      #swagger.parameters['body'] = {
        in: 'body',
        schema: {
          refreshToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
        }
      }
  */
  authController.refreshToken
);

router.post(
  "/logout",
  isAuthenticated,
  /*  #swagger.tags = ['Auth']
      #swagger.description = 'Logout user and clear cookies'
      #swagger.security = [{ "bearerAuth": [] }]
  */
  authController.logout
);

router.post(
  "/forget-password",
  /*  #swagger.tags = ['Auth']
      #swagger.description = 'Send reset password link'
      #swagger.parameters['body'] = {
        in: 'body',
        schema: {
          $email: "john@example.com"
        }
      }
  */
  authController.forgetPassword
);

router.post(
  "/reset-password/:token",
  /*  #swagger.tags = ['Auth']
      #swagger.description = 'Reset password using token'
      #swagger.parameters['token'] = { in: 'path', type: 'string', required: true }
      #swagger.parameters['body'] = {
        in: 'body',
        schema: {
          $password: "newSecret123"
        }
      }
  */
  authController.resetPassword
);

// GET Routes
router.get(
  "/profile",
  isAuthenticated,
  /*  #swagger.tags = ['Auth']
      #swagger.description = 'Get authenticated user profile'
      #swagger.security = [{ "bearerAuth": [] }]
  */
  authController.getProfile
);

// PATCH/UPDATE Routes
router.patch(
  "/profile",
  isAuthenticated,
  /*  #swagger.tags = ['Auth']
      #swagger.description = 'Update user profile (partial update)'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['body'] = {
        in: 'body',
        schema: {
          fullName: "John Doe",
          phone: "1234567890",
          profileImageUrl: "https://example.com/image.jpg"
        }
      }
  */
  authController.updateProfile
);

router.patch(
  "/password",
  isAuthenticated,
  /*  #swagger.tags = ['Auth']
      #swagger.description = 'Change password'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['body'] = {
        in: 'body',
        schema: {
          $currentPassword: "oldPass123",
          $newPassword: "newPass123"
        }
      }
  */
  authController.changePassword
);

// ============================================================================
// DEPRECATED: User-level language preference route
// Marked for removal - Language is now controlled globally by Super Admin
// Use GET /api/v1/settings/language to fetch global language
// ============================================================================
router.patch(
  "/language",
  isAuthenticated,
  /*  #swagger.tags = ['Auth']
      #swagger.deprecated = true
      #swagger.description = 'DEPRECATED: Update user language preference. Use global language settings instead.'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['body'] = {
        in: 'body',
        schema: {
          $language: "hi"
        }
      }
  */
  authController.updateLanguagePreference
);

// DELETE Routes
router.delete(
  "/profile",
  isAuthenticated,
  /*  #swagger.tags = ['Auth']
      #swagger.description = 'Delete own account'
      #swagger.security = [{ "bearerAuth": [] }]
  */
  authController.deleteProfile
);

// ==========================================
// SOCIAL LOGIN ROUTES
// ==========================================

router.get(
  "/social/config",
  /*  #swagger.tags = ['Social Auth']
      #swagger.description = 'Get social login configuration (enabled providers and client IDs)'
  */
  socialAuthController.getSocialLoginConfig
);

router.post(
  "/social/google",
  /*  #swagger.tags = ['Social Auth']
      #swagger.description = 'Login or register with Google OAuth'
      #swagger.parameters['body'] = {
        in: 'body',
        required: true,
        schema: {
          $token: "google_id_token_or_access_token",
          tokenType: "id_token",
          userType: "user"
        }
      }
  */
  socialAuthController.googleLogin
);

router.post(
  "/social/facebook",
  /*  #swagger.tags = ['Social Auth']
      #swagger.description = 'Login or register with Facebook OAuth'
      #swagger.parameters['body'] = {
        in: 'body',
        required: true,
        schema: {
          $accessToken: "facebook_access_token",
          userType: "user"
        }
      }
  */
  socialAuthController.facebookLogin
);

router.post(
  "/social/link/google",
  isAuthenticated,
  /*  #swagger.tags = ['Social Auth']
      #swagger.description = 'Link Google account to existing user'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['body'] = {
        in: 'body',
        required: true,
        schema: {
          $token: "google_id_token_or_access_token",
          tokenType: "id_token"
        }
      }
  */
  socialAuthController.linkGoogleAccount
);

router.post(
  "/social/link/facebook",
  isAuthenticated,
  /*  #swagger.tags = ['Social Auth']
      #swagger.description = 'Link Facebook account to existing user'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['body'] = {
        in: 'body',
        required: true,
        schema: {
          $accessToken: "facebook_access_token"
        }
      }
  */
  socialAuthController.linkFacebookAccount
);

router.delete(
  "/social/unlink/:provider",
  isAuthenticated,
  /*  #swagger.tags = ['Social Auth']
      #swagger.description = 'Unlink social account (google or facebook)'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['provider'] = {
        in: 'path',
        required: true,
        type: 'string',
        enum: ['google', 'facebook']
      }
  */
  socialAuthController.unlinkSocialAccount
);

module.exports = router;