package com.viettel.campaign.repository.ccms_full.impl;

import com.viettel.campaign.repository.ccms_full.CampaignCustomerListColumnRepositoryCustom;
import com.viettel.campaign.utils.Constants;
import com.viettel.campaign.utils.HibernateUtil;
import com.viettel.campaign.web.dto.ResultDTO;
import com.viettel.campaign.web.dto.request_dto.CampaignCustomerListColumnRequestDTO;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.hibernate.type.DateType;
import org.hibernate.type.LongType;
import org.hibernate.type.ShortType;
import org.hibernate.type.StringType;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class CampaignCustomerListColumnRepositoryImpl implements CampaignCustomerListColumnRepositoryCustom {
    @Override
    public List<CampaignCustomerListColumnRequestDTO> getCustomerInfor(Long companySiteId, Long customerId, Long campaignId) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        List<CampaignCustomerListColumnRequestDTO> data = new ArrayList<>();

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        try {
            StringBuilder sb = new StringBuilder();

            sb.append("SELECT");
            sb.append("    a.CUSTOMIZE_FIELD_TITLE customizeFieldTitle,");
            sb.append("    b.VALUE_TEXT valueText,");
            sb.append("    b.VALUE_NUMBER valueNumber,");
            sb.append("    b.VALUE_DATE valueDate,");
            sb.append("    b.VALUE_CHECKBOX valueCheckbox,");
            sb.append("    c.TYPE type,");
            sb.append("    d.NAME valueCombobox");
            sb.append(" FROM CAMPAIGN_CUSTOMERLIST_COLUMN a");
            sb.append("    JOIN CUSTOMIZE_FIELD_OBJECT b ON a.CUSTOMIZE_FIELD_ID = b.CUSTOMIZE_FIELDS_ID");
            sb.append("    JOIN CUSTOMIZE_FIELDS c ON a.CUSTOMIZE_FIELD_ID = c.CUSTOMIZE_FIELD_ID");
            sb.append("    LEFT JOIN CUSTOMIZE_FIELD_OPTION_VALUE d ON b.FIELD_OPTION_VALUE_ID = d.FIELD_OPTION_VALUE_ID");
            sb.append(" WHERE 1 = 1");
            sb.append("    AND b.FUNCTION_CODE = 'CUSTOMER'");
            sb.append("    AND a.COMPANY_SITE_ID = :p_company_site_id");
            sb.append("    AND b.OBJECT_ID = :p_customer_id");
            sb.append("    AND a.CAMPAIGN_ID = :p_campaign_id");

            SQLQuery query = session.createSQLQuery(sb.toString());

            query.setParameter("p_company_site_id", companySiteId);
            query.setParameter("p_customer_id", customerId);
            query.setParameter("p_campaign_id", campaignId);

            query.addScalar("customizeFieldTitle", new StringType());
            query.addScalar("valueText", new StringType());
            query.addScalar("valueNumber", new LongType());
            query.addScalar("valueDate", new DateType());
            query.addScalar("valueCheckbox", new ShortType());
            query.addScalar("type", new StringType());
            query.addScalar("valueCombobox", new StringType());

            query.setResultTransformer(Transformers.aliasToBean(CampaignCustomerListColumnRequestDTO.class));

            data = query.list();
        } catch (Exception e) {
            // e.printStackTrace();
        } finally {
            session.close();
        }

        return data;
    }
}
