package internal_reference

import (
	"net/http"

	eir "simrs-vx/internal/domain/main-entities/internal-reference"
	uir "simrs-vx/internal/use-case/main-use-case/internal-reference"

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

type myBase struct{}

var O myBase

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

	res, err := uir.Create(dto)
	rw.DataResponse(w, res, err)
}

func (obj myBase) GetList(w http.ResponseWriter, r *http.Request) {
	dto := eir.ReadListDto{}
	sf.UrlQueryParam(&dto, *r.URL)
	res, err := uir.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 := eir.ReadDetailDto{}
	sf.UrlQueryParam(&dto, *r.URL)
	dto.Id = uint(id)
	res, err := uir.ReadDetail(dto)
	rw.DataResponse(w, res, err)
}

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

	dto := eir.UpdateDto{}
	if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res {
		return
	}
	dto.Id = uint(id)
	res, err := uir.Update(dto)
	rw.DataResponse(w, res, err)
}

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

	dto := eir.DeleteDto{}
	dto.Id = uint(id)
	res, err := uir.Delete(dto)
	rw.DataResponse(w, res, err)
}
