package com.purui.prims.headless.core.adaptor.db;

import com.purui.prims.common.jsqlparser.SqlReplaceHelper;
import com.purui.prims.common.pojo.enums.TimeDimensionEnum;
import com.purui.prims.common.pojo.Constants;
import java.util.HashMap;
import java.util.Map;

public class ClickHouseAdaptor extends BaseDbAdaptor {

    @Override
    public String getDateFormat(String dateType, String dateFormat, String column) {
        if (dateFormat.equalsIgnoreCase(Constants.DAY_FORMAT_INT)) {
            if (TimeDimensionEnum.MONTH.name().equalsIgnoreCase(dateType)) {
                return "toYYYYMM(toDate(parseDateTimeBestEffort(toString(%s))))".replace("%s", column);
            } else if (TimeDimensionEnum.WEEK.name().equalsIgnoreCase(dateType)) {
                return "toMonday(toDate(parseDateTimeBestEffort(toString(%s))))".replace("%s", column);
            } else {
                return "toDate(parseDateTimeBestEffort(toString(%s)))".replace("%s", column);
            }
        } else if (dateFormat.equalsIgnoreCase(Constants.DAY_FORMAT)) {
            if (TimeDimensionEnum.MONTH.name().equalsIgnoreCase(dateType)) {
                return "formatDateTime(toDate(%s),'%Y-%m')".replace("%s", column);
            } else if (TimeDimensionEnum.WEEK.name().equalsIgnoreCase(dateType)) {
                return "toMonday(toDate(%s))".replace("%s", column);
            } else {
                return column;
            }
        }
        return column;
    }

    public String getDbMetaQueryTpl() {
        return " "
                + " select "
                + " name from system.databases "
                + " where name not in('_temporary_and_external_tables','benchmark','default','system');";
    }

    public String getTableMetaQueryTpl() {
        return "select name from system.tables where database = '%s';";
    }

    @Override
    public String functionNameCorrector(String sql) {
        Map<String, String> functionMap = new HashMap<>();
        functionMap.put("MONTH".toLowerCase(), "toMonth");
        functionMap.put("DAY".toLowerCase(), "toDayOfMonth");
        functionMap.put("YEAR".toLowerCase(), "toYear");
        return SqlReplaceHelper.replaceFunction(sql, functionMap);
    }

    public String getColumnMetaQueryTpl() {
        return "select name,type as dataType, comment from system.columns where database = '%s' and table='%s'";
    }

}
