/**
 * This file is part of the Meeds project (https://meeds.io/).
 *
 * Copyright (C) 2020 - 2025 Meeds Association contact@meeds.io
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.exoplatform.social.core.jpa.storage.dao.jpa;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import org.exoplatform.commons.persistence.impl.GenericDAOJPAImpl;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.social.core.jpa.storage.dao.SpaceMemberDAO;
import org.exoplatform.social.core.jpa.storage.entity.SpaceEntity;
import org.exoplatform.social.core.jpa.storage.entity.SpaceMemberEntity;

import io.meeds.social.space.constant.SpaceMembershipStatus;

import jakarta.persistence.NoResultException;
import jakarta.persistence.Query;
import jakarta.persistence.Tuple;
import jakarta.persistence.TypedQuery;

public class SpaceMemberDAOImpl extends GenericDAOJPAImpl<SpaceMemberEntity, Long> implements SpaceMemberDAO {

  private static final Log LOG = ExoLogger.getLogger(SpaceMemberDAOImpl.class);

  @Override
  public void deleteBySpace(SpaceEntity entity) {
    Query query = getEntityManager().createNamedQuery("SpaceMember.deleteBySpace");
    query.setParameter("spaceId", entity.getId());
    query.executeUpdate();
  }

  @Override
  public List<String> sortSpaceMembers(List<String> userNames,
                                       String sortField,
                                       String sortDirection,
                                       boolean filterDisabled) {
    if (userNames == null || userNames.isEmpty()) {
      return Collections.emptyList();
    }

    // Oracle and MSSQL support only 1/0 for boolean, Postgresql supports only
    // TRUE/FALSE, MySQL supports both
    String dbBoolFalse = isOrcaleDialect() || isMSSQLDialect() ? "0" : "FALSE";
    String dbBoolTrue = isOrcaleDialect() || isMSSQLDialect() ? "1" : "TRUE";
    // Oracle Dialect in Hibernate 4 is not registering NVARCHAR correctly, see
    // HHH-10495
    StringBuilder queryStringBuilder = null;
    if (isOrcaleDialect()) {
      queryStringBuilder = new StringBuilder("SELECT to_char(identity_1.remote_id), identity_1.identity_id \n");
    } else if (isMSSQLDialect()) {
      queryStringBuilder =
                         new StringBuilder("SELECT try_convert(varchar(200), identity_1.remote_id) as remote_id , identity_1.identity_id, try_convert(varchar(200) \n");
    } else if (StringUtils.isNotBlank(sortField) && StringUtils.isNotBlank(sortDirection)) {
      queryStringBuilder = new StringBuilder("SELECT DISTINCT identity_1.remote_id, identity_1.identity_id, LOWER(identity_prop.value) sort_field \n");
    } else {
      queryStringBuilder = new StringBuilder("SELECT identity_1.remote_id, identity_1.identity_id \n");
    }
    queryStringBuilder.append(" FROM SOC_IDENTITIES identity_1 \n");
    if (StringUtils.isNotBlank(sortField) && StringUtils.isNotBlank(sortDirection)) {
      queryStringBuilder.append(" INNER JOIN SOC_IDENTITY_PROPERTIES identity_prop \n");
      queryStringBuilder.append("   ON identity_1.identity_id = identity_prop.identity_id \n");
      queryStringBuilder.append("       AND identity_prop.name = '").append(escapeSQLValue(sortField)).append("' \n");
    }
    queryStringBuilder.append(" WHERE identity_1.remote_id IN (");
    for (int i = 0; i < userNames.size(); i++) {
      if (i > 0) {
        queryStringBuilder.append(",");
      }
      queryStringBuilder.append("'").append(escapeSQLValue(userNames.get(i))).append("'");
    }
    queryStringBuilder.append(")\n");

    if (filterDisabled) {
      queryStringBuilder.append(" AND identity_1.deleted = ").append(dbBoolFalse).append(" \n");
      queryStringBuilder.append(" AND identity_1.enabled = ").append(dbBoolTrue).append(" \n");
    }

    if (StringUtils.isNotBlank(sortField) && StringUtils.isNotBlank(sortDirection)) {
      queryStringBuilder.append(" ORDER BY sort_field " + escapeSQLValue(sortDirection));
    }

    Query query = getEntityManager().createNativeQuery(queryStringBuilder.toString());
    List<?> resultList = query.getResultList();

    List<String> result = new ArrayList<>();
    for (Object object : resultList) {
      Object[] resultEntry = (Object[]) object;
      if (resultEntry[0] == null) {
        continue;
      }
      String username = resultEntry[0].toString();
      result.add(username);
    }
    return result;
  }

  @Override
  public List<String> getSpaceMembers(Long spaceId, SpaceMembershipStatus status, int offset, int limit) {
    if (status == null) {
      throw new IllegalArgumentException("Status is null");
    }
    if (spaceId == null || spaceId == 0) {
      throw new IllegalArgumentException("spaceId is null or equals to 0");
    }
    if (offset < 0) {
      throw new IllegalArgumentException("offset must be positive");
    }
    if (limit <= 0) {
      throw new IllegalArgumentException("limit must be > 0");
    }
    TypedQuery<String> query = getEntityManager().createNamedQuery("SpaceMember.getSpaceMembersByStatus",
                                                                   String.class);
    query.setParameter("status", status);
    query.setParameter("spaceId", spaceId);
    query.setFirstResult(offset);
    query.setMaxResults(limit);
    return query.getResultList();
  }

  @Override
  public int countSpaceMembers(Long spaceId, SpaceMembershipStatus status) {
    if (status == null) {
      throw new IllegalArgumentException("Status is null");
    }
    if (spaceId == null || spaceId == 0) {
      throw new IllegalArgumentException("spaceId is null or equals to 0");
    }
    TypedQuery<Long> query = getEntityManager().createNamedQuery("SpaceMember.countSpaceMembersByStatus", Long.class);
    query.setParameter("status", status);
    query.setParameter("spaceId", spaceId);
    return query.getSingleResult().intValue();
  }

  @Override
  public SpaceMemberEntity getSpaceMemberShip(String remoteId, Long spaceId, SpaceMembershipStatus status) throws IllegalArgumentException {
    if (status == null) {
      TypedQuery<SpaceMemberEntity> query = getEntityManager().createNamedQuery("SpaceMember.getSpaceMemberShip", SpaceMemberEntity.class);
      query.setParameter("userId", remoteId);
      query.setParameter("spaceId", spaceId);
      try {
        List<SpaceMemberEntity> memberEntities = query.getResultList();
        if (memberEntities.size() > 1) {
          LOG.warn("we have found more than one result");
          return memberEntities.get(0);
        } else {
          return query.getSingleResult();
        }
      } catch (NoResultException ex) {
        return null;
      }
    } else {
      TypedQuery<SpaceMemberEntity> query = getEntityManager().createNamedQuery("SpaceMember.getMember", SpaceMemberEntity.class);
      query.setParameter("userId", remoteId);
      query.setParameter("spaceId", spaceId);
      query.setParameter("status", status);
      try {
        List<SpaceMemberEntity> memberEntities = query.getResultList();
        if (memberEntities.size() > 1) {
          LOG.warn("we have found more than one result");
          return memberEntities.get(0);
        } else {
          return query.getSingleResult();
        }
      } catch (NoResultException ex) {
        return null;
      }
    }
  }

  @Override
  public List<Long> getSpaceIdentityIdsByUserRole(String remoteId, SpaceMembershipStatus status, int offset, int limit) {
    TypedQuery<Long> query = getEntityManager().createNamedQuery("SpaceMember.getSpaceIdentitiesIdByMemberId", Long.class);
    query.setParameter("userId", remoteId);
    query.setParameter("status", status);
    try {
      if (limit > 0) {
        query.setFirstResult(offset);
        query.setMaxResults(limit);
      }
      return query.getResultList();
    } catch (NoResultException ex) {
      return Collections.emptyList();
    }
  }

  @Override
  public List<Long> getSpacesIdsByUserName(String remoteId, int offset, int limit) {
    return getSpaceIdentityIdsByUserRole(remoteId, SpaceMembershipStatus.MEMBER, offset, limit);
  }

  @Override
  public List<Long> getSpaceIdByMemberId(String username, int offset, int limit) {
    return getSpaceIdsByUserRole(username, SpaceMembershipStatus.MEMBER, offset, limit);
  }

  @Override
  public List<Long> getSpaceIdsByUserRole(String username, SpaceMembershipStatus status, int offset, int limit) {
    TypedQuery<Long> query = getEntityManager().createNamedQuery("SpaceMember.getSpaceIdByMemberId", Long.class);
    query.setParameter("userId", username);
    query.setParameter("status", status);
    try {
      if (limit > 0) {
        query.setFirstResult(offset);
        query.setMaxResults(limit);
      }
      return query.getResultList();
    } catch (NoResultException ex) {
      return Collections.emptyList();
    }
  }

  @Override
  public List<Tuple> getPendingSpaceRequestsToManage(String username, int offset, int limit) {
    if (offset < 0) {
      throw new IllegalArgumentException("offset must be positive");
    }
    if (limit <= 0) {
      throw new IllegalArgumentException("limit must be > 0");
    }
    TypedQuery<Tuple> query = getEntityManager().createNamedQuery("SpaceMember.getPendingSpaceRequestsToManage",
                                                                              Tuple.class);
    query.setParameter("userId", username);
    query.setParameter("status", SpaceMembershipStatus.PENDING);
    query.setParameter("user_status", SpaceMembershipStatus.MANAGER);

    query.setFirstResult(offset);
    query.setMaxResults(limit);

    try {
      return query.getResultList();
    } catch (NoResultException ex) {
      return Collections.emptyList();
    }
  }

  @Override
  public int countPendingSpaceRequestsToManage(String username) {
    TypedQuery<Long> query = getEntityManager().createNamedQuery("SpaceMember.countPendingSpaceRequestsToManage", Long.class);
    query.setParameter("userId", username);
    query.setParameter("status", SpaceMembershipStatus.PENDING);
    query.setParameter("user_status", SpaceMembershipStatus.MANAGER);
    return query.getSingleResult().intValue();
  }

  @Override
  public int countExternalMembers(Long spaceId) {
    Query q = getEntityManager().createNativeQuery("SELECT COUNT(DISTINCT USER_ID) FROM SOC_SPACES_MEMBERS ssm " +
            "INNER JOIN SOC_IDENTITIES si ON ssm.USER_ID = si.REMOTE_ID " +
            "INNER JOIN SOC_IDENTITY_PROPERTIES sip ON si.IDENTITY_ID = sip.IDENTITY_ID " +
            "WHERE sip.NAME = 'external' AND sip.VALUE = 'true' AND ssm.SPACE_ID = :spaceId AND si.ENABLED = 1");
    q.setParameter("spaceId", spaceId);
    try {
      return ((Number) q.getSingleResult()).intValue();
    } catch (NoResultException e) {
      return 0;
    }
  }

  @Override
  public Instant getSpaceMembershipDate(long spaceId, String username) {
    TypedQuery<Instant> query = getEntityManager().createNamedQuery("SpaceMember.getSpaceMembershipDate",
                                                                    Instant.class);
    query.setParameter("spaceId", spaceId);
    query.setParameter("userId", username);
    query.setMaxResults(1);
    List<Instant> resultList = query.getResultList();
    return CollectionUtils.isEmpty(resultList) ? null : resultList.get(0);
  }

  @Override
  public List<String> getDisabledSpaceMembers(Long spaceId, int offset, int limit) {
    if (spaceId == null || spaceId == 0) {
      throw new IllegalArgumentException("spaceId is null or equals to 0");
    }
    if (offset < 0) {
      throw new IllegalArgumentException("offset must be positive");
    }
    if (limit <= 0) {
      throw new IllegalArgumentException("limit must be > 0");
    }
    TypedQuery<String> query = getEntityManager().createNamedQuery("SpaceMember.getDisabledSpaceMembers",
            String.class);
    query.setParameter("spaceId", spaceId);
    query.setFirstResult(offset);
    query.setMaxResults(limit);
    return query.getResultList();
  }

  @Override
  public int countDisabledSpaceMembers(Long spaceId) {
    if (spaceId == null || spaceId == 0) {
      throw new IllegalArgumentException("spaceId is null or equals to 0");
    }
    TypedQuery<Long> query = getEntityManager().createNamedQuery("SpaceMember.countDisabledSpaceMembers", Long.class);
    query.setParameter("spaceId", spaceId);
    return query.getSingleResult().intValue();
  }

  private String escapeSQLValue(String value) {
    return value.replace("'", "'");
  }

}
