package logic

import (
        "context"

        "engine/internal/iotype"
        "engine/internal/logger"
        "engine/pkg/auxtool/auxjwt"

)

type LogicConfig struct {
}

type Logic struct {
        log *logger.Logger
}

func NewLogic(conf *LogicConfig) (*Logic, error) {
        var err error
        srv := &Logic{
                log: logger.NewLogger("server"),
        }
        return srv, err
}

const (
        tokenIssuer = "ENGINE"
        tokenSubject = "WEB"
        sessionID = 0
        tokenLifetime = 3600
        tokenSecret = "TW9uIEp1bCAzMSAxODox"
)

func (lg *Logic) CreateSession(ctx context.Context, nReq *iotype.CreateSessionRequest) (*iotype.CreateSessionResult, error) {
        var err error
        nRes := &iotype.CreateSessionResult{}

        //expireJWT := time.Now().Add(tokenLifetime)
        //expireUnix := expireJWT.Unix()

        secret := []byte(tokenSecret)
        tokenBytes, expireToken, err := auxjwt.MakeJWTToken(tokenIssuer, tokenSubject, sessionID, tokenLifetime, secret)
        if err != nil {
                return nRes, err
        }
        nRes.Token = string(tokenBytes)
        nRes.Expire = expireToken
        return nRes, err
}

func (lg *Logic) GetHealth(ctx context.Context, nReq *iotype.GetHealthRequest) (*iotype.GetHealthResult, error) {
        var err error
        nRes := &iotype.GetHealthResult{}
        return nRes, err
}
