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 FinishedItemQueryBuilder {


    public static String buildQuery(String id, String itemId, String sku, String createdStartDate, String createdEndDate, String jobCardId, String status, 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());
            }
        }

        return ( new QueryBuilder() )
                .setTable("cut_to_pack.finished_item")
                .setColumns("*")
                .where()
                .columnEquals("id", id)
                .and()
                .columnEquals("sku", sku)
                .and()
                .columnEquals("item_id", itemId )
                .and()
                .columnEquals("job_card_id", jobCardId )
                .and()
                .columnEquals("qa_status", status )
                .and()
                .columnEqualToOrGreaterThan("created_at", startDate1)
                .and()
                .columnEqualToOrLessThan("created_at", endDate1 )
                .orderBy("id","DESC")
                .limit(count)
                .build();

    }
}
