// src/controllers/api/v1/user.ts
// Copyright (C) 2026 Robert Colbert <rob.colbert@openplatform.us>
// All Rights Reserved

import { NextFunction, Request, Response } from "express";

import { DtpController } from "../../../lib/controller.js";
import UserService from "../../../services/user.js";

export class UserApiControllerV1 extends DtpController {
  get name(): string {
    return "UserApiControllerV1";
  }
  get slug(): string {
    return "userV1";
  }
  get route(): string {
    return "/user";
  }

  constructor() {
    super();
  }

  async start(): Promise<void> {
    this.router.use(this.requireUser());

    this.router.get("/", this.getUser.bind(this));
    this.router.put("/settings", this.putUpdateSettings.bind(this));
  }

  async getUser(req: Request, res: Response): Promise<void> {
    res.status(200).json({
      success: true,
      data: req.user,
    });
  }

  async putUpdateSettings(
    req: Request,
    res: Response,
    next: NextFunction,
  ): Promise<void> {
    try {
      const updatedUser = await UserService.updateForUser(req.user!, req.body);
      res.status(200).json({
        success: true,
        data: updatedUser,
      });
    } catch (error) {
      this.log.error("failed to update user settings", { error });
      next(error);
    }
  }
}

export default UserApiControllerV1;
