/*
 * Copyright (C) 2000 - 2024 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception. You should have received a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "http://www.silverpeas.org/docs/core/legal/floss_exception.html"
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */
package org.silverpeas.components.questionreply.web;

import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response.Status;
import org.silverpeas.components.questionreply.QuestionReplyException;
import org.silverpeas.components.questionreply.model.Reply;
import org.silverpeas.components.questionreply.service.QuestionManager;
import org.silverpeas.core.admin.user.model.SilverpeasRole;
import org.silverpeas.core.annotation.WebService;
import org.silverpeas.core.contribution.attachment.AttachmentService;
import org.silverpeas.core.contribution.attachment.model.SimpleDocument;
import org.silverpeas.core.web.rs.annotation.Authorized;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * A REST Web resource representing a given reply to a question. It is a web service that provides
 * access to a reply referenced by its URL.
 */
@WebService
@Path(QuestionReplyBaseWebService.PATH + "/{componentId}/replies")
@Authorized
public class ReplyResource extends QuestionReplyBaseWebService {

  @PathParam("componentId")
  protected String componentId;

  @Inject
  private QuestionManager questionManager;

  @Inject
  private AttachmentService attachmentService;

  @Override
  public String getComponentId() {
    return this.componentId;
  }

  /**
   * Gets the JSON representation of the specified existing question. If the reply doesn't exist, a
   * 404 HTTP code is returned. If the user isn't authentified, a 401 HTTP code is returned. If the
   * user isn't authorized to access the question, a 403 is returned. If a problem occurs when
   * processing the request, a 503 HTTP code is returned.
   *
   * @param onQuestionId the unique identifier of the question.
   * @return the response to the HTTP GET request with the JSON representation of the asked question
   * replies.
   */
  @GET
  @Path("question/{questionId}")
  @Produces(MediaType.APPLICATION_JSON)
  public List<ReplyEntity> getAllRepliesForQuestion(@PathParam("questionId") String onQuestionId) {
    try {
      long questionId = Long.parseLong(onQuestionId);
      List<Reply> replies = questionManager.getAllReplies(questionId,
          componentId);
      return asWebEntities(extractVisibleReplies(questionId, replies), getUserProfile());
    } catch (Exception ex) {
      throw new WebApplicationException(ex, Status.SERVICE_UNAVAILABLE);
    }
  }

  @GET
  @Produces(MediaType.APPLICATION_JSON)
  @Path("public/question/{questionId}")
  public List<ReplyEntity> getPublicRepliesForQuestion(@PathParam("questionId") String onQuestionId) {
    try {
      List<Reply> replies = questionManager.getQuestionPublicReplies(
          Long.parseLong(onQuestionId), componentId);
      return asWebEntities(replies, getUserProfile());
    } catch (Exception ex) {
      throw new WebApplicationException(ex, Status.SERVICE_UNAVAILABLE);
    }
  }

  protected URI identifiedBy(URI uri) {
    return uri;
  }

  /**
   * Converts the specified list of replies into their corresponding web entities.
   *
   * @param replies the replies to convert.
   * @param profile the profile of the user.
   * @return a list with the corresponding reply entities.
   */
  protected List<ReplyEntity> asWebEntities(List<Reply> replies, SilverpeasRole profile) {
    return replies.stream()
        .map(r -> {
          URI commentURI = getUri().getRequestUriBuilder().path(r.getPK().getId()).build();
          return asWebEntity(r, identifiedBy(commentURI), profile);
        })
        .collect(Collectors.toList());
  }

  /**
   * Converts the reply into its corresponding web entity.
   *
   * @param reply the reply to convert.
   * @param replyURI the URI of the reply.
   * @param profile the profile of the user.
   * @return the corresponding reply entity.
   */
  protected ReplyEntity asWebEntity(final Reply reply, URI replyURI, SilverpeasRole profile) {
    ReplyEntity entity = ReplyEntity.fromReply(reply, getUserPreferences().getLanguage()).withURI(
        replyURI).withProfile(profile);
    Collection<SimpleDocument> attachments =
        attachmentService.listDocumentsByForeignKey(reply.getPK().toResourceReference(),
            entity.getLanguage());
    entity.withAttachments(attachments);
    AuthorEntity author = AuthorEntity.fromUser(reply.readAuthor());
    author.setAvatar(getHttpServletRequest().getContextPath() + author.getAvatar());
    return entity;
  }

  boolean isReplyVisible(String questionAuthor, Reply reply, SilverpeasRole role,
      String userId) {
    boolean isPrivate = reply.getPublicReply() <= 0;
    if (isPrivate) {
      boolean isAuthor = questionAuthor.equals(userId);
      return SilverpeasRole.USER != role && (SilverpeasRole.PUBLISHER != role || isAuthor);
    }
    return true;
  }

  List<Reply> extractVisibleReplies(long questionId, List<Reply> replies) throws
      QuestionReplyException {
    List<Reply> visibleReplies = new ArrayList<>(replies.size());
    String authorId = questionManager.getQuestion(questionId).
        getCreatorId();
    SilverpeasRole profile = getUserProfile();
    String userid = getUser().getId();
    for (Reply reply : replies) {
      if (isReplyVisible(authorId, reply, profile, userid)) {
        visibleReplies.add(reply);
      }
    }
    return visibleReplies;
  }
}
