package com.viettel.campaign.repository;

import com.viettel.campaign.model.CampaignCompleteCode;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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 java.util.List;

@Repository
public interface CampaignCompleteCodeRepository extends JpaRepository<CampaignCompleteCode, Long>, CampaignCompleteCodeRepositoryCustom {

    @Query(" select u FROM CampaignCompleteCode u WHERE u.status = 1")
    Page<CampaignCompleteCode> findAll(Pageable pageable);

    //    Page<CampaignCompleteCode> findAllByStatus(String status, Pageable pageable);

    @Query("FROM CampaignCompleteCode WHERE completeName LIKE concat('%', :name, '%') ")
    List<CampaignCompleteCode> findByName(@Param("name") String name, Pageable pageable);

    List<CampaignCompleteCode> findByCompleteNameContains(String name, Pageable pageable);

    @Modifying
    @Query("update CampaignCompleteCode c set c.status = 0  where c.campaignCompleteCodeId in (:ids)")
    int deleteds(@Param("ids") List<Long> ids);

    //    @Query(value="SELECT  max(completeValue) FROM CampaignCompleteCode WHERE companySiteId = :companySiteId GROUP BY companySiteId")
    //    Short findByMaxCompanySiteId(Long companySiteId);

    List<CampaignCompleteCode> findCampaignCompleteCodesByCompanySiteId(Long companySiteId);

    @Query(value="SELECT max(completeValue) FROM CampaignCompleteCode WHERE companySiteId = :companySiteId GROUP BY companySiteId")
    Short findByMaxCompanySiteId(Long companySiteId);

    //    List<CampaignCompleteCode> findCampaignCompleteCodesByCompanySiteId(Long companySiteId);

    //    List<CampaignCompleteCode> findByCampaignIdContainsAndChanel(Long campaignId, String chanel);

    @Query(value = "FROM CampaignCompleteCode WHERE status = 1 AND completeValue = :completeValue AND completeType = :completeType AND companySiteId = :companySiteId")
    List<CampaignCompleteCode> getCustomerStatus(@Param("completeValue") String completeValue, @Param("completeType") Short completeType, @Param("companySiteId") Long companySiteId);

    @Query(value = "FROM CampaignCompleteCode WHERE status = 1 AND completeValue = :completeValue AND completeType <> :completeType AND companySiteId = :companySiteId")
    List<CampaignCompleteCode> getCustomerStatusWithoutType(@Param("completeValue") String completeValue, @Param("completeType") Short completeType, @Param("companySiteId") Long companySiteId);
}
