import express, { Request, Response, Router } from "express"
import IResponse from "../interfaces/IResponse"
import IUserGetDto from "../interfaces/IUserGetDto"
import { EStatuses } from "../enum/EStatuses"
import { auth } from "../middlewares/auth"
import IModifiedRequest from "../interfaces/IModifiedRequest"
import { UsersService, usersService } from "../services/usersService"

export class UsersController {
    private service: UsersService
    private router: Router
    constructor(){
        this.service = usersService
        this.router = express.Router()
        this.router.post('/', this.createUser)
        this.router.post('/login', this.loginUser)
        this.router.get('/token', auth, this.checkToken)
    }

    public getRouter = () => {
        return this.router
    }

    private createUser = async (req: Request, res: Response): Promise<void> => {
        const response: IResponse<IUserGetDto | null> = await this.service.createUser(req.body)
        res.status(200).send(response)
    }

    public loginUser = async(req: Request, res: Response): Promise<void> => {
        const response: IResponse<IUserGetDto | null> = await this.service.loginUser(req.body)
        res.status(200).send(response)
    }

    public checkToken = async (expressReq: Request, res: Response): Promise<void> => {
        const req = expressReq as IModifiedRequest
        const response: IResponse<IUserGetDto | null> = {
            status: EStatuses.SUCCESS,
            result: req.verifiedData as IUserGetDto,
            message: 'Token is ok'
        }
        res.status(200).send(response)
    }
}

export const usersController = new UsersController()