package com.davos.assessment.legal;

import com.davos.assessment.user.AppUser;
import com.davos.assessment.project.Project;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface ConsentLogRepository extends JpaRepository<ConsentLog, Long> {
  boolean existsByUserAndConsentTypeAndConsentVersionAndGrantedTrue(
      AppUser user,
      ConsentType consentType,
      String consentVersion);

  boolean existsByProjectAndUserAndConsentTypeAndConsentVersionAndGrantedTrue(
      Project project,
      AppUser user,
      ConsentType consentType,
      String consentVersion);

  @Query("""
      select c from ConsentLog c
      join fetch c.user u
      where c.project = :project
        and c.consentType = :type
        and (:email = '' or lower(u.email) like lower(concat('%', :email, '%')))
      order by c.createdAt desc
      """)
  List<ConsentLog> findConsentRows(@Param("project") Project project, @Param("type") ConsentType type, @Param("email") String email);
}
