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

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

import java.util.List;

public interface MenuMonthlyRepository extends JpaRepository<TnDailyMenuLog, Long> {

    @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();
}

