package ambulance_transport_req

import (
	"net/http"
	atr "simrs-vx/internal/domain/main-entities/ambulance-transport-req"
	uatr "simrs-vx/internal/use-case/main-use-case/ambulance-transport-req"

	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 := atr.CreateDto{}
	if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res {
		return
	}

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

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

	res, err := uatr.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 := atr.UpdateDto{}
	if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res {
		return
	}
	dto.Id = uint16(id)
	res, err := uatr.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 := atr.DeleteDto{}
	dto.Id = uint16(id)
	res, err := uatr.Delete(dto)
	rw.DataResponse(w, res, err)
}
