// routes/user.js
const express = require("express")
const router = express.Router()
const { validate } = require("../middleware/validate.middleware")
const userService = require("../services/user.service")
const userProfileAdapter = require("../adapters/responses/userProfile.adapter")
const { endpoints } = require("@shared/contracts")

const { users } = endpoints

router.get(
  "/:userName",
  validate(users.userProfileRequestSchema, "params", "validatedUserProfile"),
  async (req, res) => {
    try {
      const { userName } = req.validatedUserProfile
      const data = await userService.getUserProfileByUsername(userName)

      const response = users.userProfileResponseSchema.parse(
        userProfileAdapter.mapUserProfileToResponse(data)
      )
      res.json(response)
    } catch (err) {
      console.error(err)
      const status = err.statusCode || 500
      res.status(status).json({
        message: err.message || "Profil bilgileri alinamadi.",
      })
    }
  }
)

module.exports = router
