package api

import (
	"bordro-esleme/model/company"
	"git.makki.io/makki/libgo/cmn"
	"git.makki.io/makki/libgo/dbu"
	"git.makki.io/makki/libgo/mhttp"
	"git.notitek.com.tr/common/notgo/nauth"
	"github.com/go-chi/chi/v5"
	"net/http"
)

func companyGet(w http.ResponseWriter, r *http.Request) {
	/*
			tc := auth.TokenDataFromRequest(r)
		    rbac, err := tc.RBAC(r.Context(), auth.MdCompany)
		    if err != nil {
		        mhttp.InternalServerError(w, err)
		        return
		    }
		    if !rbac.IsGrantedOp(auth.OpRead) && !tc.IsAdmin() {
		        mhttp.Forbidden(w)
		        return
		    }
	*/

	id := cmn.StrToInt64(chi.URLParam(r, "id"))
	data, err := company.DbRead(r.Context(), id)
	if err != nil {
		if dbu.IsNoRowsErr(err) {
			mhttp.NotFound(w)
			return
		} else {
			mhttp.InternalServerError(w, err)
			return
		}
	}

	mhttp.ResponseSuccess(w, data)
}

func companyCreate(w http.ResponseWriter, r *http.Request) {
	tc := nauth.TokenPayloadFromRequest(r)
	/*
	   rbac, err := tc.RBAC(r.Context(), auth.MdCompany)
	   if err != nil {
	       mhttp.InternalServerError(w, err)
	       return
	   }
	   if !rbac.IsGrantedOp(auth.OpCreate) && !tc.IsAdmin() {
	       mhttp.Forbidden(w)
	       return
	   }
	*/

	data := company.New()
	err := cmn.BodyToJsonReq(r, &data)
	if err != nil {
		mhttp.InternalServerError(w, err)
		return
	}

	err = data.DbCreate(r.Context(), tc.ClientID(), true, tc.UsrID())
	if err != nil {
		mhttp.InternalServerError(w, err)
		return
	}

	mhttp.ResponseSuccess(w, data.ID)
}

func companyUpdate(w http.ResponseWriter, r *http.Request) {
	tc := nauth.TokenPayloadFromRequest(r)
	/*
	   rbac, err := tc.RBAC(r.Context(), auth.MdCompany)
	   if err != nil {
	       mhttp.InternalServerError(w, err)
	       return
	   }
	   if !rbac.IsGrantedOp(auth.OpUpdate) && !tc.IsAdmin() {
	       mhttp.Forbidden(w)
	       return
	   }
	*/

	data := company.New()
	err := cmn.BodyToJsonReq(r, &data)
	if err != nil {
		mhttp.InternalServerError(w, err)
		return
	}

	err = data.DbUpdate(r.Context(), true, tc.UsrID())
	if err != nil {
		mhttp.InternalServerError(w, err)
		return
	}

	mhttp.ResponseSuccess(w, true)
}

func companyDelete(w http.ResponseWriter, r *http.Request) {
	tc := nauth.TokenPayloadFromRequest(r)
	/*
	   rbac, err := tc.RBAC(r.Context(), auth.MdCompany)
	   if err != nil {
	       mhttp.InternalServerError(w, err)
	       return
	   }
	   if !rbac.IsGrantedOp(auth.OpDelete) && !tc.IsAdmin() {
	       mhttp.Forbidden(w)
	       return
	   }
	*/

	id := cmn.StrToInt64(chi.URLParam(r, "id"))

	err := company.DbDelete(r.Context(), id, true, tc.UsrID())
	if err != nil {
		mhttp.InternalServerError(w, err)
		return
	}

	mhttp.ResponseSuccess(w, true)
}

func companyList(w http.ResponseWriter, r *http.Request) {
	/*
			tc := auth.TokenDataFromRequest(r)
		    rbac, err := tc.RBAC(r.Context(), auth.MdCompany)
		    if err != nil {
		        mhttp.InternalServerError(w, err)
		        return
		    }
		    if !rbac.IsGrantedOp(auth.OpRead) && !tc.IsAdmin() {
		        mhttp.Forbidden(w)
		        return
		    }
	*/

	var data []company.Company

	tr, err := dbu.NewTableRequestFromRequest(r)
	if err != nil {
		mhttp.InternalServerError(w, err)
		return
	}

	rp, err := dbu.NewRepoWithFile(r.Context(), "cm", nil)
	if err != nil {
		mhttp.InternalServerError(w, err)
		return
	}

	respData, err := rp.MList(tr, &data)
	if err != nil {
		mhttp.InternalServerError(w, err)
		return
	}

	mhttp.ResponseSuccess(w, respData)
}
