package com.bcxin.rest.web.commons;

import com.bcxin.Infrastructures.TenantContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

public abstract class ControllerAbstract {
    protected <T> ResponseEntity<T> ok(T data) {
        return ResponseBuilder.build(HttpStatus.OK, data);
    }

    protected <T> ResponseEntity<T> ok() {
        return ResponseBuilder.build(HttpStatus.OK, null);
    }

    protected ResponseEntity notFound() {
        return ResponseBuilder.build(HttpStatus.NOT_FOUND, null);
    }

    protected ResponseEntity badRequest(String msg) {
        return ResponseBuilder.build(HttpStatus.BAD_REQUEST, null);
    }

    protected <T> ResponseEntity<T> badRequest(T data) {
        return ResponseBuilder.build(HttpStatus.BAD_REQUEST, data);
    }

    protected ResponseEntity badRequest(Throwable ex) {
        return ResponseEntity.badRequest().build();
    }

    protected String getSelectedEmployeeId() {
        if (TenantContext.getInstance().getUserContext().get() == null) {
            return null;
        }

        return TenantContext.getInstance().getUserContext().get().getEmployeeId();
    }

    protected String getSelectedOrganId() {
        if (TenantContext.getInstance().getUserContext().get() == null) {
            return null;
        }

        return TenantContext.getInstance().getUserContext().get().getOrganId();
    }

    protected String getCurrentUserId() {
        if (TenantContext.getInstance().getUserContext().get() == null) {
            return null;
        }

        return TenantContext.getInstance().getUserContext().get().getId();
    }
}
