package com.viettel.campaign.repository.ccms_full;

import com.viettel.campaign.config.DataSourceQualify;
import com.viettel.campaign.model.ccms_full.CampaignCustomer;
import org.springframework.data.jpa.repository.JpaRepository;
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
@Transactional(DataSourceQualify.CCMS_FULL)
public interface CampaignCustomerRepository extends JpaRepository<CampaignCustomer, Long>, CampaignCustomerRepositoryCustom {

    @Query(value = "SELECT COUNT(*) " +
            " FROM CAMPAIGN_CUSTOMER CC " +
            " JOIN CAMPAIGN C ON CC.CAMPAIGN_ID = C.CAMPAIGN_ID " +
            " LEFT JOIN CAMPAIGN_COMPLETE_CODE CCC ON CC.STATUS = CCC.COMPLETE_VALUE" +
            " WHERE CC.COMPANY_SITE_ID = :companySiteId " +
            " AND CC.AGENT_ID = :agentId" +
            " AND C.STATUS = 2 " +
            " AND CC.RECALL_TIME <= SYSDATE " +
            " AND CCC.IS_RECALL = 1", nativeQuery = true)
    Long countRecallCustomer(@Param("companySiteId") Long pCompanySiteId, @Param("agentId") Long agentId);

    @Query(value = "SELECT COUNT(*) " +
            "FROM CAMPAIGN_CUSTOMER CC JOIN CAMPAIGN_COMPLETE_CODE CCC ON CC.CAMPAIGN_ID = CCC.CAMPAIGN_ID " +
            "WHERE CC.CAMPAIGN_ID = :campaignId " +
            "AND CC.CUSTOMER_ID = :customerId " +
            "AND CC.STATUS = 3 " +
            "AND CCC.IS_RECALL = 1 " +
            "AND CCC.STATUS = 1 ", nativeQuery = true)
    Long getCustomerRecall(@Param("campaignId") Long campaignId, @Param("customerId") Long customerId);

    CampaignCustomer findCampaignCustomerByCampaignCustomerId(Long id);



    @Query(value = "select C.NAME, " +
            "C.EMAIL, " +
            "C.PLACE_OF_BIRTH, " +
            "C.AREA_CODE, " +
            "C.COMPANY_NAME, " +
            "C.CURRENT_ADDRESS, " +
            "C.CUSTOMER_TYPE, " +
            "C.DATE_OF_BIRTH, " +
            "C.CUSTOMER_ID,"+
            "C.USERNAME,"+
            "C.GENDER, CF.TITLE " +
//            " from CAMPAIGN_COMPLETE_CODE cm " +
            "from CUSTOMER C " +
            "left join CUSTOMIZE_FIELDS CF " +
            "on C.CUSTOMER_ID = CF.CUSTOMIZE_FIELD_ID "+
            "where CF.FUNCTION_CODE = 'CUSTORMER' and c.STATUS =1 and CF.ACTIVE =1 and CF.SITE_ID =?"
            , nativeQuery = true)
    Long searchCustomer(@Param("site_id") Long pSiteId);





    @Query(value = "SELECT * FROM CAMPAIGN_CUSTOMER CC " +
            "WHERE CC.STATUS = 0 " +
            "AND CC.CAMPAIGN_ID = :campaignId " +
            "AND CC.COMPANY_SITE_ID = :companySiteId " +
            "AND CC.CUSTOMER_LIST_ID = :customerListId", nativeQuery = true)
    List<CampaignCustomer> findCustomerNoContact(@Param("campaignId") Long campaignId, @Param("companySiteId") Long companySiteId, @Param("customerListId") Long customerListId);

    @Query(value = "SELECT * FROM CAMPAIGN_CUSTOMER CC " +
            "WHERE CC.STATUS IN (SELECT COMPLETE_VALUE from CAMPAIGN_COMPLETE_CODE where STATUS = 1 and IS_FINISH = 0 and IS_RECALL = 0)" +
            "AND CC.CAMPAIGN_ID = :campaignId " +
            "AND CC.COMPANY_SITE_ID = :companySiteId " +
            "AND CC.CUSTOMER_LIST_ID = :customerListId", nativeQuery = true)
    List<CampaignCustomer> findCustomerContacted(@Param("campaignId") Long campaignId, @Param("companySiteId") Long companySiteId, @Param("customerListId") Long customerListId);

    @Query(value = "with status_customer as (\n" +
            "select complete_value \n" +
            "from campaign_complete_code\n" +
            "where complete_value <> 4\n" +
            "    and is_finish <> 1\n" +
            "    and campaign_type = 1\n" +
            "    and company_site_id = :p_company_site_id\n" +
            ")\n" +
            "select * from campaign_customer\n" +
            "where campaign_id = :p_campaign_id\n" +
            "    and customer_list_id = :p_cus_list_id\n" +
            "    and (status = 0 or status in (select * from status_customer))", nativeQuery = true)
    List<CampaignCustomer> findListCustomerToDel(@Param("p_company_site_id") Long companySiteId, @Param("p_campaign_id") Long campaignId, @Param("p_cus_list_id") Long customerListId);
}
