import "reflect-metadata";
import { Controller, Post, Body, Route, Response } from "@tsoa/runtime";
import usersService from "~/services/users";

interface LoginRequest {
	userName: string;
	password: string;
}

@Route("users")
export class UsersController extends Controller {
	/**
	 * Authenticates a user with username and password
	 */
	@Post()
	@Response(401, "Unauthorized")
	public async getUser(@Body() body: LoginRequest): Promise<any> {
		const user = await usersService.getUser({
			userName: body.userName,
			password: body.password,
		});

		if (user === null) {
			this.setStatus(401);
			return {};
		}
		return user;
	}
}
