package com.ttstd.signaling.repository;

import com.ttstd.signaling.model.LoginSession;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface LoginSessionRepository extends JpaRepository<LoginSession, String> {
    List<LoginSession> findByPrincipalId(String principalId);
    List<LoginSession> findByPrincipalIdAndPrincipalType(String principalId, com.ttstd.signaling.model.PrincipalType type);
    void deleteBySessionId(String sessionId);
    long countByPrincipalIdAndPrincipalType(String principalId, com.ttstd.signaling.model.PrincipalType type);

    @Modifying
    @Query("UPDATE LoginSession s SET s.revoked = true WHERE s.principalId = :principalId")
    int revokeByPrincipalId(String principalId);

    @Modifying
    @Query("UPDATE LoginSession s SET s.revoked = true WHERE s.sessionId = :sessionId")
    int revokeBySessionId(String sessionId);
}
