package com.dbnt.kcscbackend.admin.dashboard.repository;

import com.dbnt.kcscbackend.admin.logs.entity.TnDailyMenuLog;
import com.dbnt.kcscbackend.admin.logs.entity.TnDailyUserLog;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface DashboardRepository extends JpaRepository<TnDailyMenuLog, Long> {

    @Query(value = "WITH this_month_logs AS ( " +
            "  SELECT sum(log_cnt) AS this_month_cnt " +
            "  FROM tn_daily_user_log " +
            "  WHERE log_dt >= DATE_TRUNC('month', CURRENT_DATE) " +
            "    AND log_dt < DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 month' " +
            "), " +
            "last_month_logs AS ( " +
            "  SELECT sum(log_cnt) AS last_month_cnt " +
            "  FROM tn_daily_user_log " +
            "  WHERE log_dt >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '1 month') " +
            "    AND log_dt < DATE_TRUNC('month', CURRENT_DATE) " +
            ") " +
            "SELECT " +
            "  this_month_cnt, " +
            "  CASE " +
            "   WHEN last_month_cnt = 0 AND this_month_cnt = 0 THEN 0" +
            "   WHEN last_month_cnt = 0 THEN 100" +
            "   ELSE ROUND(((CAST(this_month_cnt AS NUMERIC) - last_month_cnt) / last_month_cnt * 100), 1)" +
            "  END AS ratio, " +
            "  last_month_cnt " +
            "FROM this_month_logs, last_month_logs", nativeQuery = true)
    List<Object[]> ConnMonthlyCount();


    @Query(value = "WITH this_month_logs AS ( " +
            "  SELECT count(*) AS this_month_cnt " +
            "  FROM tn_document_content " +
            "  WHERE use_yn = 'Y' AND COALESCE(error_cd, '') <> '' " +
            "    AND frst_crt_dt >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '1 month') " +
            "    AND frst_crt_dt < DATE_TRUNC('month', CURRENT_DATE) " +
            "), " +
            "last_month_logs AS ( " +
            "  SELECT count(*) AS last_month_cnt " +
            "  FROM tn_document_content " +
            "  WHERE use_yn = 'Y' AND COALESCE(error_cd, '') <> '' " +
            "    AND frst_crt_dt >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '1 month') " +
            "    AND frst_crt_dt < DATE_TRUNC('month', CURRENT_DATE) " +
            ") " +
            "SELECT " +
            "  this_month_cnt, " +
            "  CASE " +
            "   WHEN last_month_cnt = 0 AND this_month_cnt = 0 THEN 0" +
            "   WHEN last_month_cnt = 0 THEN 100" +
            "   ELSE ROUND(((CAST(this_month_cnt AS NUMERIC) - last_month_cnt) / last_month_cnt * 100), 1)" +
            "  END AS ratio, " +
            "  last_month_cnt " +
            "FROM this_month_logs, last_month_logs", nativeQuery = true)
    List<Object[]> ErrorMonthlyCount();


    @Query(value = "WITH this_month_logs AS ( " +
            "  SELECT count(*) AS this_month_cnt " +
            "  FROM tn_document_info " +
            "  WHERE last_yn = 'Y' AND use_yn = 'Y' " +
            "    AND frst_crt_dt >= DATE_TRUNC('month', CURRENT_DATE) " +
            "    AND frst_crt_dt < DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 month' " +
            "), " +
            "last_month_logs AS ( " +
            "  SELECT count(*) AS last_month_cnt " +
            "  FROM tn_document_info " +
            "  WHERE last_yn = 'Y' AND use_yn = 'Y' " +
            "    AND frst_crt_dt >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '1 month') " +
            "    AND frst_crt_dt < DATE_TRUNC('month', CURRENT_DATE) " +
            ") " +
            "SELECT " +
            "  this_month_cnt, " +
            "  CASE " +
            "   WHEN last_month_cnt = 0 AND this_month_cnt = 0 THEN 0" +
            "   WHEN last_month_cnt = 0 THEN 100" +
            "   ELSE ROUND(((CAST(this_month_cnt AS NUMERIC) - last_month_cnt) / last_month_cnt * 100), 1) " +
            "  END AS ratio, " +
            "  last_month_cnt " +
            "FROM this_month_logs, last_month_logs", nativeQuery = true)
    List<Object[]> DocuMonthlyCount();


    @Query(value = "WITH this_month_logs AS ( " +
            "  SELECT count(*) AS this_month_cnt " +
            "  FROM tn_bbs_contents " +
            "  WHERE bbs_id = 'KCSC-QA' AND use_yn = 'Y' " +
            "    AND frst_crt_dt >= DATE_TRUNC('month', CURRENT_DATE) " +
            "    AND frst_crt_dt < DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 month' " +
            "), " +
            "last_month_logs AS ( " +
            "  SELECT count(*) AS last_month_cnt " +
            "  FROM tn_bbs_contents " +
            "  WHERE bbs_id = 'KCSC-QA'AND use_yn = 'Y' " +
            "    AND frst_crt_dt >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '1 month') " +
            "    AND frst_crt_dt < DATE_TRUNC('month', CURRENT_DATE) " +
            ") " +
            "SELECT " +
            "  this_month_cnt, " +
            "  CASE " +
            "   WHEN last_month_cnt = 0 AND this_month_cnt = 0 THEN 0" +
            "   WHEN last_month_cnt = 0 THEN 100" +
            "   ELSE ROUND(((CAST(this_month_cnt AS NUMERIC) - last_month_cnt) / last_month_cnt * 100), 1) " +
            "  END AS ratio, " +
            "  last_month_cnt " +
            "FROM this_month_logs, last_month_logs", nativeQuery = true)
    List<Object[]> CompMonthlyCount();


    @Query(value = "WITH all_months AS (" +
            "    SELECT generate_series(DATE_TRUNC('year', CURRENT_DATE), DATE_TRUNC('year', CURRENT_DATE) + INTERVAL '11 month', INTERVAL '1 month') AS month_start" +
            ")" +
            "SELECT COALESCE(SUM(tn.log_cnt), 0) AS log_cnt " +
            "FROM all_months " +
            "LEFT JOIN tn_daily_menu_log tn ON TO_CHAR(all_months.month_start, 'yyyy-mm') = TO_CHAR(tn.log_dt, 'yyyy-mm') " +
            "GROUP BY TO_CHAR(all_months.month_start, 'yyyy-mm') " +
            "ORDER BY TO_CHAR(all_months.month_start, 'yyyy-mm') ASC", nativeQuery = true)
    List<Long> MenuMonthlyList();

    @Query(value = "WITH all_days AS (" +
            "    SELECT generate_series(" +
            "               DATE_TRUNC('year', now())," +
            "               DATE_TRUNC('year', now()) + INTERVAL '1 year' - INTERVAL '1 day'," +
            "               INTERVAL '1 day'" +
            "           ) AS day" +
            ")" +
            "SELECT COALESCE(SUM(tn.log_cnt), 0) AS log_cnt " +
            "FROM all_days ad " +
            "LEFT JOIN tn_daily_menu_log tn ON ad.day = DATE_TRUNC('day', tn.log_dt) " +
            "GROUP BY TO_CHAR(ad.day, 'Day') " +
            "ORDER BY MIN(ad.day)", nativeQuery = true)
    List<Long> MenuDailyList();


    @Query(value = "WITH all_months AS (" +
            "    SELECT generate_series(" +
            "               DATE_TRUNC('year', now())," +
            "               DATE_TRUNC('year', now()) + INTERVAL '11 month'," +
            "               INTERVAL '1 month'" +
            "           ) AS month_start" +
            ")" +
            "SELECT COALESCE(SUM(tn.log_cnt), 0) AS log_cnt " +
            "FROM all_months " +
            "LEFT JOIN tn_daily_user_log tn ON TO_CHAR(all_months.month_start, 'yyyy-mm') = TO_CHAR(tn.log_dt, 'yyyy-mm') " +
            "GROUP BY TO_CHAR(all_months.month_start, 'yyyy-mm') " +
            "ORDER BY TO_CHAR(all_months.month_start, 'yyyy-mm') ASC", nativeQuery = true)
    List<Long> LoginMonthlyList();


    @Query(value = "WITH all_days AS (" +
            "    SELECT generate_series(" +
            "               DATE_TRUNC('year', CURRENT_DATE)," +
            "               DATE_TRUNC('year', CURRENT_DATE) + INTERVAL '1 year' - INTERVAL '1 day'," +
            "               INTERVAL '1 day'" +
            "           ) AS day" +
            ")" +
            "SELECT COALESCE(SUM(tn.log_cnt), 0) AS log_cnt " +
            "FROM all_days ad " +
            "LEFT JOIN tn_daily_user_log tn ON ad.day = tn.log_dt " +
            "GROUP BY TO_CHAR(ad.day, 'Day') " +
            "ORDER BY MIN(ad.day)", nativeQuery = true)
    List<Long> LoginDailyList();


    @Query(value = "WITH all_days AS (" +
            "    SELECT generate_series(" +
            "               DATE_TRUNC('year', CURRENT_DATE)," +
            "               DATE_TRUNC('year', CURRENT_DATE) + INTERVAL '1 year' - INTERVAL '1 day'," +
            "               INTERVAL '1 day'" +
            "           ) AS day" +
            ")" +
            "SELECT COALESCE(COUNT(tn.access_dt), 0) AS log_cnt " +
            "FROM all_days ad " +
            "LEFT JOIN (SELECT access_dt FROM public.th_attach_file_log WHERE\n" +
            "    access_dt >= CURRENT_DATE - INTERVAL '6 day') tn ON ad.day = DATE_TRUNC('day', tn.access_dt) " +
            "GROUP BY TO_CHAR(ad.day, 'Day') " +
            "ORDER BY MIN(ad.day)", nativeQuery = true)
    List<Long> FileDailyList();


    @Query(value = "SELECT bbs_cont_title, frst_crt_dt, frst_crt_id " +
            "FROM tn_bbs_contents a " +
            "WHERE bbs_id = 'KCSC-QA' AND use_yn = 'Y'  " +
            "AND (SELECT bbs_cont_seq b FROM public.tn_bbs_contents b WHERE bbs_id = 'KCSC-QA' AND use_yn = 'Y' AND b.bbs_cont_seq_group=a.bbs_cont_seq) IS NULL " +
            "ORDER BY frst_crt_dt desc", nativeQuery = true)
    List<Object[]> QuestionList();


    @Query(value = "SELECT sum(pc_cnt) AS p_cnt, sum(mobile_cnt) AS m_cnt " +
            "FROM tn_daily_user_log " +
            "WHERE log_dt >= DATE_TRUNC('month', CURRENT_DATE) " +
            "AND log_dt < DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 month'", nativeQuery = true)
    List<Object[]> findConnMonthly();
}
