package com.mzh.library.service;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

public class ServiceResult<T> {
    private final boolean successful;
    private final T data;
    private final String message;
    private final Map<String, String> errors;

    private ServiceResult(boolean successful, T data, String message, Map<String, String> errors) {
        this.successful = successful;
        this.data = data;
        this.message = message;
        this.errors = Collections.unmodifiableMap(new LinkedHashMap<>(errors));
    }

    public static <T> ServiceResult<T> success(T data) {
        return new ServiceResult<>(true, data, null, Collections.emptyMap());
    }

    public static <T> ServiceResult<T> success(T data, String message) {
        return new ServiceResult<>(true, data, message, Collections.emptyMap());
    }

    public static <T> ServiceResult<T> failure(String message) {
        return new ServiceResult<>(false, null, message, Collections.emptyMap());
    }

    public static <T> ServiceResult<T> validationFailure(String message, Map<String, String> errors) {
        return new ServiceResult<>(false, null, message, errors);
    }

    public boolean isSuccessful() {
        return successful;
    }

    public T getData() {
        return data;
    }

    public String getMessage() {
        return message;
    }

    public Map<String, String> getErrors() {
        return errors;
    }

    public boolean hasErrors() {
        return !errors.isEmpty();
    }
}
