package mil.dds.anet.utils;

import java.lang.invoke.MethodHandles;
import java.time.temporal.ChronoUnit;
import mil.dds.anet.database.CommentDao;
import mil.dds.anet.database.OrganizationDao;
import mil.dds.anet.database.PersonDao;
import mil.dds.anet.database.PositionDao;
import mil.dds.anet.database.ReportDao;
import mil.dds.anet.database.ReportSensitiveInformationDao;
import org.jdbi.v3.core.statement.SqlLogger;
import org.jdbi.v3.core.statement.StatementContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Logger that specifically looks for the very long column definitions that ANET2 uses and will
 * replace them in the log with a shortened version. This just makes the logs easier to read and
 * debug.
 */
public class AnetDbLogger implements SqlLogger {
  private static final Logger logger =
      LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

  @Override
  public void logAfterExecution(StatementContext context) {
    final String msg =
        context.getRenderedSql().replace(PersonDao.PERSON_FIELDS, " <PERSON_FIELDS> ")
            .replace(PersonDao.PERSON_FIELDS_NOAS, " <PERSON_FIELDS> ")
            .replace(PositionDao.POSITIONS_FIELDS, " <POSITION_FIELDS> ")
            .replace(OrganizationDao.ORGANIZATION_FIELDS, " <ORGANIZATION_FIELDS> ")
            .replace(ReportDao.REPORT_FIELDS, " <REPORT_FIELDS> ")
            .replace(ReportSensitiveInformationDao.REPORTS_SENSITIVE_INFORMATION_FIELDS,
                " <REPORTS_SENSITIVE_INFORMATION_FIELDS> ")
            .replace(CommentDao.COMMENT_FIELDS, " <COMMENT_FIELDS> ")
            .replaceAll("LEFT JOIN (CONTAINS|FREETEXT)TABLE[^=]*= (\\S+)\\.\\[Key\\]", "<$1_$2>")
            .replaceFirst("(EXP|ISNULL|CASE).* AS (search_rank)", "<$1>");
    logger.debug("{}\t{}", context.getElapsedTime(ChronoUnit.MILLIS), msg);
  }
}

@@ -1716,16 +1716,102 @@
 eFirst(%22
+LEFT JOIN (mv_fts_%5C%5CS+) ON %5C%5CS+%5C%5Cs*=%5C%5Cs*%5C%5CS+%22, %22%3C$1%3E%22)%0A            .replaceFirst(%22%5C%5C(?
 (EXP%7CISN
@@ -1818,16 +1818,24 @@
 ULL%7CCASE
+%7Cts_rank
 ).* AS (
@@ -1852,17 +1852,17 @@
 k)%22, %22%3C$
-1
+2
 %3E%22);%0A   

Make PostgreSQL full-text searches a little more readable in the dbLog