package encounter

import (
	"net/http"

	pa "simrs-vx/internal/lib/auth"

	d "github.com/karincake/dodol"
	rw "github.com/karincake/risoles"
	sf "github.com/karincake/semprit"

	// ua "github.com/karincake/tumpeng/auth/svc"
	erc "simrs-vx/internal/domain/references/common"

	e "simrs-vx/internal/domain/main-entities/encounter"
	u "simrs-vx/internal/use-case/main-use-case/encounter"
)

type myBase struct{}

var O myBase

func (obj myBase) Create(w http.ResponseWriter, r *http.Request) {
	authInfo, err := pa.GetAuthInfo(r)
	if err != nil {
		rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": err.Error()}, nil)
	}

	dto := e.CreateDto{}
	if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res {
		return
	}

	// validate SubClass
	if err := verifyClassCode(dto); err != nil {
		rw.DataResponse(w, nil, d.FieldError{
			Code:    dataValidationFail,
			Message: err.Error(),
		})
		return
	}

	dto.AuthInfo = *authInfo
	res, err := u.Create(dto)
	rw.DataResponse(w, res, err)
}

func (obj myBase) GetList(w http.ResponseWriter, r *http.Request) {
	authInfo, err := pa.GetAuthInfo(r)
	if err != nil {
		rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": err.Error()}, nil)
	}
	dto := e.ReadListDto{}
	dto.AuthInfo = *authInfo
	sf.UrlQueryParam(&dto, *r.URL)
	res, err := u.ReadList(dto)
	rw.DataResponse(w, res, err)
}

func (obj myBase) GetDetail(w http.ResponseWriter, r *http.Request) {
	id := rw.ValidateInt(w, "id", r.PathValue("id"))
	if id <= 0 {
		return
	}
	dto := e.ReadDetailDto{}
	sf.UrlQueryParam(&dto, *r.URL)
	dto.Id = uint(id)
	res, err := u.ReadDetail(dto)
	rw.DataResponse(w, res, err)
}

func (obj myBase) Update(w http.ResponseWriter, r *http.Request) {
	authInfo, err := pa.GetAuthInfo(r)
	if err != nil {
		rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": err.Error()}, nil)
	}

	id := rw.ValidateInt(w, "id", r.PathValue("id"))
	if id <= 0 {
		return
	}

	dto := e.UpdateDto{}
	if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res {
		return
	}
	dto.Id = uint(id)
	dto.AuthInfo = *authInfo

	res, err := u.Update(dto)
	rw.DataResponse(w, res, err)
}

func (obj myBase) Delete(w http.ResponseWriter, r *http.Request) {
	authInfo, err := pa.GetAuthInfo(r)
	if err != nil {
		rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": err.Error()}, nil)
	}

	id := rw.ValidateInt(w, "id", r.PathValue("id"))
	if id <= 0 {
		return
	}

	dto := e.DeleteDto{}
	dto.Id = uint(id)
	dto.AuthInfo = *authInfo

	res, err := u.Delete(dto)
	rw.DataResponse(w, res, err)
}

func (obj myBase) CheckOut(w http.ResponseWriter, r *http.Request) {
	dto := e.DischargeDto{}

	authInfo, err := pa.GetAuthInfo(r)
	if err != nil {
		rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": err.Error()}, nil)
	}

	id := rw.ValidateInt(w, "id", r.PathValue("id"))
	if id <= 0 {
		return
	}

	if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res {
		return
	}

	// validate request body
	if valid := validateRequestCheckout(w, dto); !valid {
		return
	}

	dto.Id = uint(id)
	dto.AuthInfo = *authInfo

	res, err := u.CheckOut(dto)
	rw.DataResponse(w, res, err)
}

func (obj myBase) CheckIn(w http.ResponseWriter, r *http.Request) {
	dto := e.CheckinDto{}

	authInfo, err := pa.GetAuthInfo(r)
	if err != nil {
		rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": err.Error()}, nil)
	}

	id := rw.ValidateInt(w, "id", r.PathValue("id"))
	if id <= 0 {
		return
	}

	if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res {
		return
	}

	// validate request body
	if valid := validateRequestCheckIn(w, dto); !valid {
		return
	}

	dto.Id = uint(id)
	dto.AuthInfo = *authInfo

	res, err := u.CheckIn(dto)
	rw.DataResponse(w, res, err)
}

func (obj myBase) Process(w http.ResponseWriter, r *http.Request) {
	id := rw.ValidateInt(w, "id", r.PathValue("id"))
	if id <= 0 {
		return
	}

	authInfo, err := pa.GetAuthInfo(r)
	if err != nil {
		rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": err.Error()}, nil)
	}

	dto := e.UpdateStatusDto{
		Id:         uint(id),
		StatusCode: erc.DSCProcess,
		AuthInfo:   *authInfo,
	}

	res, err := u.UpdateStatusCode(dto)
	rw.DataResponse(w, res, err)
}

func (obj myBase) Cancel(w http.ResponseWriter, r *http.Request) {
	id := rw.ValidateInt(w, "id", r.PathValue("id"))
	if id <= 0 {
		return
	}

	authInfo, err := pa.GetAuthInfo(r)
	if err != nil {
		rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": err.Error()}, nil)
	}

	dto := e.UpdateStatusDto{
		Id:         uint(id),
		StatusCode: erc.DSCCancel,
		AuthInfo:   *authInfo,
	}

	res, err := u.UpdateStatusCode(dto)
	rw.DataResponse(w, res, err)
}

func (obj myBase) Reject(w http.ResponseWriter, r *http.Request) {
	id := rw.ValidateInt(w, "id", r.PathValue("id"))
	if id <= 0 {
		return
	}

	dto := e.UpdateStatusDto{
		Id:         uint(id),
		StatusCode: erc.DSCRejected,
	}

	res, err := u.UpdateStatusCode(dto)
	rw.DataResponse(w, res, err)
}

func (obj myBase) Skip(w http.ResponseWriter, r *http.Request) {
	id := rw.ValidateInt(w, "id", r.PathValue("id"))
	if id <= 0 {
		return
	}

	dto := e.UpdateStatusDto{
		Id:         uint(id),
		StatusCode: erc.DSCSkipped,
	}

	res, err := u.UpdateStatusCode(dto)
	rw.DataResponse(w, res, err)
}

func (obj myBase) RequestSwitchUnit(w http.ResponseWriter, r *http.Request) {
	dto := e.SwitchUnitDto{}
	id := rw.ValidateInt(w, "id", r.PathValue("id"))
	if id <= 0 {
		return
	}

	if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res {
		return
	}

	// validate request body
	if valid := validateRequestSwitchUnit(w, dto); !valid {
		return
	}

	authInfo, err := pa.GetAuthInfo(r)
	if err != nil {
		rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": err.Error()}, nil)
	}

	dto.AuthInfo = *authInfo
	dto.Id = uint(id)
	res, err := u.RequestSwitchUnit(dto)
	rw.DataResponse(w, res, err)
}

func (obj myBase) ApproveSwitchUnit(w http.ResponseWriter, r *http.Request) {
	dto := e.ApproveCancelUnitDto{}
	id := rw.ValidateInt(w, "id", r.PathValue("id"))
	if id <= 0 {
		return
	}

	if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res {
		return
	}

	authInfo, err := pa.GetAuthInfo(r)
	if err != nil {
		rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": err.Error()}, nil)
	}

	dto.AuthInfo = *authInfo
	dto.Id = uint(id)

	res, err := u.ApproveSwitchUnit(dto)
	rw.DataResponse(w, res, err)
}

func (obj myBase) CancelSwitchUnit(w http.ResponseWriter, r *http.Request) {
	dto := e.ApproveCancelUnitDto{}
	id := rw.ValidateInt(w, "id", r.PathValue("id"))
	if id <= 0 {
		return
	}

	if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res {
		return
	}

	authInfo, err := pa.GetAuthInfo(r)
	if err != nil {
		rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": err.Error()}, nil)
	}

	dto.AuthInfo = *authInfo
	dto.Id = uint(id)

	res, err := u.CancelSwitchUnit(dto)
	rw.DataResponse(w, res, err)
}
