/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core.exceptions;

import static de.srsoftware.umbrella.core.Errors.*;
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_SERVER_ERROR;
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_UNPROCESSABLE;
import static de.srsoftware.umbrella.core.constants.Field.*;
import static java.net.HttpURLConnection.*;

import de.srsoftware.umbrella.core.model.Translatable;



public class UmbrellaException extends RuntimeException{
	private final int statusCode;
	private final Translatable message;

	public UmbrellaException(int statusCode, Translatable message){
		super(message.toString());
		this.statusCode = statusCode;
		this.message    = message;
	}

	public UmbrellaException(int statusCode, String rawMessage, Object ... args){
		this(statusCode, Translatable.t(rawMessage,args));
	}

	public static final UmbrellaException badRequest(String rawMessage, Object ... args){
		return new UmbrellaException(HTTP_BAD_REQUEST,rawMessage,args);
	}

	public UmbrellaException causedBy(Exception e) {
		initCause(e);
		return this;
	}

	public static UmbrellaException databaseException(String rawMessage, Object ... args) {
		return new UmbrellaException(HTTP_SERVER_ERROR,rawMessage,args);
	}

	public static UmbrellaException failedToCreateTable(String tableName){
		return databaseException(FAILED_TO_CREATE_TABLE, NAME,tableName);
	}

	public static UmbrellaException failedToDropObject(Object object){
		return databaseException(FAILED_TO_DROP_OBJECT, OBJECT,object);
	}

	public static UmbrellaException failedToDropObjectFromObject(String typeOfDropped, Object dropped, Object typeOfRelated, Object related){
		return databaseException(FAILED_TO_DROP_ENTITY_OF_ENTITY,
				"dropped_type",typeOfDropped,
				OBJECT,dropped,
				"related_type", typeOfRelated,
				"related",related);
	}

	public static UmbrellaException failedToLoadMembers(Object object){
		return databaseException(FAILED_TO_LOAD_OBJECT_MEMBERS, OBJECT,object);
	}

	public static UmbrellaException failedToLoadObject(Object object){
		return databaseException(FAILED_TO_LOAD_OBJECT, OBJECT,object);
	}

	public static UmbrellaException failedToLoadObject(Object object, Object id){
		return databaseException(FAILED_TO_LOAD_OBJECT_BY_ID, OBJECT,object, ID,id);
	}

	public static UmbrellaException failedToStoreObject(Object object){
		return databaseException(FAILED_TO_STORE_OBJECT, OBJECT,object);
	}

	public static UmbrellaException failedToReadFromTable(String field, String table){
		return databaseException(FAILED_TO_READ_FROM_TABLE, FIELD,field, TABLE,table);
	}

	public static UmbrellaException failedToSearchDb(Translatable module){
		return databaseException(FAILED_TO_SEARCH_DB, MODULE, module);
	}

	public static UmbrellaException forbidden(String rawMessage, Object ... args) {
		return new UmbrellaException(HTTP_FORBIDDEN,rawMessage,args);
	}

	public static UmbrellaException invalidField(String field, Object expected){
		return new UmbrellaException(HTTP_UNPROCESSABLE, INVALID_FIELD, FIELD, field, EXPECTED, expected);
	}

	public Translatable message(){
		return message;
	}

	public static UmbrellaException missingConfig(String key){
		return new UmbrellaException(HTTP_SERVER_ERROR,MISSING_CONFIG, KEY,key);
	}


	public static UmbrellaException missingField(String field){
		return new UmbrellaException(HTTP_UNPROCESSABLE, MISSING_FIELD, FIELD,field);
	}

	public static UmbrellaException notAmember(Object object){
		return forbidden(NO_MEMBER, OBJECT,object);
	}

	public static UmbrellaException notFound(String rawMessage, Object ... args) {
		return new UmbrellaException(HTTP_NOT_FOUND,rawMessage,args);
	}

	public static UmbrellaException serverError(String rawMessage, Object ... args){
		return new UmbrellaException(HTTP_SERVER_ERROR,rawMessage,args);
	}

	public int statusCode(){
		return statusCode;
	}

	public static UmbrellaException unauthorized(String rawMessage, Object ... args) {
		return new UmbrellaException(HTTP_UNAUTHORIZED,rawMessage,args);
	}

	public static UmbrellaException unprocessable(String rawMessage, Object ... args) {
		return new UmbrellaException(HTTP_UNPROCESSABLE,rawMessage, args);
	}
}
