package com.utopiaindustries.querybuilder.ctp;

import com.utopiaindustries.querybuilder.QueryBuilder;
import com.utopiaindustries.util.CTPDateTimeFormat;
import com.utopiaindustries.util.StringUtils;

import java.time.LocalDate;

public class StichedOfflineItemQueryBuilder {

    public static String buildQuery(String id, String itemId, String sku, String QaStatus,String createdStartDate, String createdEndDate, Long bundleId, Long count) {
        // format date
        String formattedDate;
        String formattedEndDate;
        String startDate1 = "";
        String endDate1 = "";
        if (!StringUtils.isNullOrEmpty( createdStartDate)) {
            formattedDate = CTPDateTimeFormat.getMySQLFormattedDateString(createdStartDate, CTPDateTimeFormat.HTML5_DATE_INPUT_FORMAT);
            formattedEndDate = CTPDateTimeFormat.getMySQLFormattedDateString(createdEndDate, CTPDateTimeFormat.HTML5_DATE_INPUT_FORMAT);
            startDate1 = String.format("'%s 00:00:01'", formattedDate);
            if (!StringUtils.isNullOrEmpty(createdEndDate)) {
                endDate1 = String.format("'%s 23:59:59'", formattedEndDate);
            } else {
                endDate1 = String.format("'%s 23:59:59'", LocalDate.now());
            }
        }
        QueryBuilder qb = (new QueryBuilder())
                .setTable("cut_to_pack.stitching_offline_item")
                .setColumns("*")
                .where()
                .columnEquals("id", id)
                .and()
                .columnEquals("sku", sku)
                .and()
                .columnEquals("item_id", itemId)
                .and()
                .columnEquals("bundle_id", bundleId)
                .and()
                .columnEqualToOrGreaterThan("created_at", startDate1)
                .and()
                .columnEqualToOrLessThan("created_at", endDate1);
                if (!StringUtils.isNullOrEmpty(QaStatus)) {
                    qb.and().columnEquals("qa_status", QaStatus);
                }
                qb.orderBy("id", "DESC")
                        .limit(count);

        return qb.build();
    }
}

