package divisionposition

import (
	"net/http"

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

	// ua "github.com/karincake/tumpeng/auth/svc"

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

type myBase struct{}

var O myBase

func (obj myBase) Create(w http.ResponseWriter, r *http.Request) {
	dto := e.CreateDto{}
	if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res {
		return
	}
	res, err := u.Create(dto)
	rw.DataResponse(w, res, err)
}

func (obj myBase) GetList(w http.ResponseWriter, r *http.Request) {
	dto := e.ReadListDto{}
	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) {
	code := rw.ValidateString(w, "code", r.PathValue("code"))
	if code == "" {
		return
	}
	dto := e.ReadDetailDto{}
	dto.Code = &code
	res, err := u.ReadDetail(dto)
	rw.DataResponse(w, res, err)
}

func (obj myBase) Update(w http.ResponseWriter, r *http.Request) {
	code := rw.ValidateString(w, "code", r.PathValue("code"))
	if code == "" {
		return
	}

	dto := e.UpdateDto{}
	if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res {
		return
	}
	dto.Code = code
	res, err := u.Update(dto)
	rw.DataResponse(w, res, err)
}

func (obj myBase) Delete(w http.ResponseWriter, r *http.Request) {
	code := rw.ValidateString(w, "code", r.PathValue("code"))
	if code == "" {
		return
	}

	dto := e.DeleteDto{}
	dto.Code = &code
	res, err := u.Delete(dto)
	rw.DataResponse(w, res, err)
}
