package com.davos.assessment.invitation;

import java.util.Optional;
import java.util.List;
import com.davos.assessment.project.Project;
import jakarta.persistence.LockModeType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface InvitationCodeRepository extends JpaRepository<InvitationCode, Long> {
  List<InvitationCode> findByProject(Project project);
  Optional<InvitationCode> findByProjectAndCodeIgnoreCase(Project project, String code);
  @Lock(LockModeType.PESSIMISTIC_WRITE)
  @Query("select code from InvitationCode code where code.project = :project and lower(code.code) = lower(:code)")
  Optional<InvitationCode> findLockedByProjectAndCodeIgnoreCase(@Param("project") Project project, @Param("code") String code);
  boolean existsByProjectAndCodeIgnoreCase(Project project, String code);
}
