package controllers

import (
	"fmt"
	"oc-datacenter/infrastructure"
	"strconv"

	beego "github.com/beego/beego/v2/server/web"
)

type SessionController struct {
	beego.Controller
}

// @Title GetToken
// @Description find booking by id
// @Param	id	path 	string		"id of the datacenter"
// @Param	duration	path 	string		"duration of the token"
// @Success 200 {booking} models.booking
// @router /token/:id/:duration [get]
func (o *SessionController) GetToken() {
	id := o.Ctx.Input.Param(":id")
	durationSTR := o.Ctx.Input.Param(":duration")

	duration, err := strconv.Atoi(durationSTR)
	if err != nil {
		// change code to 400
		o.Ctx.Output.SetStatus(400)
		o.Data["json"] = map[string]string{"error": "duration invalid"}
		o.ServeJSON()
		return
	}

	serv, err := infrastructure.NewService()
	if err != nil {
		// change code to 500
		o.Ctx.Output.SetStatus(500)
		o.Data["json"] = map[string]string{"error": err.Error()}
		o.ServeJSON()
		return
	}
	fmt.Println("BLAPO", id, duration)
	token, err := serv.GetToken(o.Ctx.Request.Context(), id, duration)
	if err != nil {
		// change code to 500
		o.Ctx.Output.SetStatus(500)
		o.Data["json"] = map[string]string{"error": err.Error()}
		o.ServeJSON()
		return
	}
	o.Data["json"] = token
	o.ServeJSON()
}