package com.ffii.fhsmsc.modules.User_group.repository;

import com.ffii.core.support.AbstractRepository;
import com.ffii.fhsmsc.modules.User_group.enity.UserGroupMembers;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.jpa.repository.Modifying;
import java.util.List;
import java.util.Optional;

@Repository
public interface UserGroupMembersRepository extends AbstractRepository<UserGroupMembers, Long> {
    
    @Query("SELECT m FROM UserGroupMembers m WHERE m.User_Grp_Org_ID = :orgId")
    List<UserGroupMembers> findByUserGrpOrgId(@Param("orgId") Long orgId);

    @Query("SELECT m FROM UserGroupMembers m WHERE m.User_Grp_Org_ID = :orgId AND m.R_Ship_Creator = :relationshipType")
    List<UserGroupMembers> findByUserGrpOrgIdAndRelationshipType(
        @Param("orgId") Long orgId, 
        @Param("relationshipType") String relationshipType
    );

    @Query("SELECT m FROM UserGroupMembers m WHERE m.User_Grp_Org_ID = :orgId AND m.User_ID = :userId")
    Optional<UserGroupMembers> findByUserGrpOrgIdAndUserId(
        @Param("orgId") Long orgId, 
        @Param("userId") Long userId
    );

    @Modifying
    @Transactional
    @Query("DELETE FROM UserGroupMembers m WHERE m.User_Grp_Org_ID = :orgId AND m.User_ID = :userId")
    void deleteByUserGrpOrgIdAndUserId(
        @Param("orgId") Long orgId, 
        @Param("userId") Long userId
    );
} 