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

import com.sikey.wa04.business.domain.entity.User;
import com.sikey.wa04.business.infrastructure.dto.model.UserModel;
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.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface UserRepository extends JpaRepository<UserModel, String> {

    @Query("select u from UserModel u where u.phoneNumber = ?1 and u.areaCode = ?2")
    UserModel findByPhoneNumberAndAreaCode(String phoneNumber, String areaCode);

    UserModel findByEmailAndPassword(@Size(max = 125) String email, @Size(max = 36) String password);

    UserModel findByEmail(@Size(max = 125) String email);

    UserModel findByPhoneNumber(@Size(max = 125) String phoneNumber);

    @Transactional
    @Modifying
    @Query("UPDATE UserModel u SET u.isRegistered = :isRegistered WHERE u.id = :id")
    int updateIsRegisteredById(@Param("isRegistered") boolean isRegistered,@Param("id") String id);

    List<UserModel> findByIdIn(List<String> ids);

    @Transactional
    @Modifying
    @Query("UPDATE UserModel u SET u.phoneNumber = :phoneNumber WHERE u.id = :id")
    void updatePhoneNumberById(@Param("phoneNumber") String phoneNumber,@Param("id") String id);
}
