package com.sikey.wa04.business.infrastructure.repository;

import com.sikey.wa04.business.infrastructure.dto.model.ChildRefModel;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
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 org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.List;
import java.util.Set;

@Repository
public interface ChildRefRepository extends JpaRepository<ChildRefModel, Long> {
    List<ChildRefModel> findByUserId(@Size(max = 36) String userId);

    /**
     * 根据id升序排序，查询小孩列表
     * @param userId
     * @return
     */
    List<ChildRefModel> findByUserIdOrderByIdAsc(@Size(max = 36) String userId);

    List<ChildRefModel> findByChildId(@Size(max = 36) String childId);

    ChildRefModel findByUserIdAndChildId(@Size(max = 36) String userId, @Size(max = 36) String childId);

    void deleteByChildId(@Size(max = 36) String childId);

    void deleteByChildIdAndUserId(@Size(max = 36) String childId, @Size(max = 36) String userId);

    void deleteByUserId(@Size(max = 36) String userId);

    void deleteByChildIdInAndUserId(@Size(max = 36) List<String> childIds, @Size(max = 36) String userId);

    List<ChildRefModel> findByUserIdAndChildIdIn(@Size(max = 36) String userId, @Size(max = 36) Set<String> childIds);

    @Transactional
    @Modifying
    @Query("UPDATE ChildRefModel c SET c.userId = :newUserId, c.updatedAt = :updatedAt WHERE c.userId = :userId AND c.childId = :cid")
    void updateByUserIdAndChildId(String userId, String cid, String newUserId, Instant updatedAt);
}
