package com.utopiaindustries.querybuilder.ctp;

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

import java.time.LocalDate;
import java.util.List;

public class InventoryAccountQueryBuilder {

    public static String buildQuery(String id,
                                    String title,
                                    String active,
                                    String createdBy,
                                    String startDate,
                                    String endDate,
                                    String siteId,
                                    Long count,
                                    List<Long> accountIds,
                                    String parentEntityType,
                                    String parentEntityId,
                                    Boolean isPackaging) {

        // format date
        String formattedDate;
        String formattedEndDate;
        String startDate1 = "";
        String endDate1 = "";
        if (!StringUtils.isNullOrEmpty(startDate)) {
            formattedDate = CTPDateTimeFormat.getMySQLFormattedDateString(startDate, CTPDateTimeFormat.HTML5_DATE_INPUT_FORMAT);
            formattedEndDate = CTPDateTimeFormat.getMySQLFormattedDateString(endDate, CTPDateTimeFormat.HTML5_DATE_INPUT_FORMAT);
            startDate1 = String.format("'%s 00:00:01'", formattedDate);
            if (!StringUtils.isNullOrEmpty(endDate)) {
                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.inventory_account")
                .setColumns("*")
                .where()
                .columnLikeTitle("title", title)
                .and()
                .columnEquals("id", id)
                .and()
                .columnEquals("active", active)
                .and()
                .columnLike("created_by", createdBy)
                .and()
                .columnEqualToOrGreaterThan("created_at", startDate1)
                .and()
                .columnEqualToOrLessThan("created_at", endDate1)
                .and()
                .columnEquals("location_site_id", siteId);


        if (isPackaging != null){
            qb.and()
                    .columnEquals("is_packaging", isPackaging );
        }

        if (parentEntityId != null && !parentEntityId.equals("")){
            qb.and()
                    .columnEquals("parent_entity_id", parentEntityId );
        }

        if (parentEntityType != null && !parentEntityType.equals("")){
             qb.and()
                    .columnEquals("parent_entity_type", parentEntityType );
        }

        if ( accountIds != null && !accountIds.isEmpty()) {
            qb.and()
                    .columnIn("id",  accountIds.toArray( new Long[0] ));
        }

        qb.limit(count.intValue());
        return qb.build();

    }
}
