# application/db/inverter_queries.py

from application.db.query_executor import QueryResults
# from application.db import Database
# from application.auth.decorators import query_executor
import traceback
from flask import current_app
class EnergyQueries:

    def get_daily_energy(self,user_id):
        try:
            select_query = {
                'statement': """
                    SELECT generation_kwh , report_date FROM monthly_reports WHERE {} = %s  AND report_date = DATE_SUB(CURRENT_DATE, INTERVAL 1 DAY) ORDER BY report_date DESC 
                    LIMIT 1
                """,
                "column_name": ('user_id', ),
                'params': (user_id,)
            }
            query_results = current_app.extensions['query_results']

            result = query_results.fetch_query_results(**select_query)
            if not result:
                return result
            return result[0] # True if insertion was successful
        except Exception as e:
            # log error here
            print(traceback.print_exc())
            raise e

    # def get_monthly_energy(self,user_id,month, year):
    #     try:
    #         select_query = {
    #             'statement':  """
    #                         SELECT DISTINCT report_date, generation_kwh
    #                         FROM monthly_reports
    #                         WHERE {} = %s
    #                           AND report_date >= DATE(CONCAT(%s, '-', %s, '-01'))
    #                           AND report_date < DATE_ADD(DATE(CONCAT(%s, '-', %s, '-01')), INTERVAL 1 MONTH)
    #                         ORDER BY report_date
    #                         """,
    #             "column_name": ('user_id', ),
    #             'params': (user_id,year, month, year, month)
    #         }
    #         query_results = current_app.extensions['query_results']
    #
    #         result = query_results.fetch_query_results(**select_query)
    #         return result # True if insertion was successful
    #     except Exception as e:
    #         # log error here
    #         print(traceback.print_exc())
    #         raise e
    def get_monthly_energy(self,user_id,month, year):
        try:
            select_query = {
                'statement': """
                    SELECT DISTINCT report_date, generation_kwh
                    FROM monthly_reports
                    WHERE {} = %s
                      AND MONTH(report_date) = MONTH(CURDATE())
                      AND YEAR(report_date) = YEAR(CURDATE())
                    ORDER BY report_date
                """,
                "column_name": ('user_id',),
                'params': (user_id,)  # user hardcoded for testing
            }

            query_results = current_app.extensions['query_results']

            result = query_results.fetch_query_results(**select_query)
            return result # True if insertion was successful
        except Exception as e:
            # log error here
            print(traceback.print_exc())
            raise e
    def get_yearly_energy(self, user_id, year):
        try:
            column_name = 'user_id'  # You can parameterize this if needed
            select_query = {
                'statement': """
                    SELECT DISTINCT report_date, generation_kwh
                    FROM monthly_reports
                    WHERE {} = %s
                      AND report_date >= DATE(CONCAT(%s, '-01-01'))
                      AND report_date < DATE(CONCAT(%s + 1, '-01-01'))
                    ORDER BY report_date
                """,
                "column_name": (column_name,),
                'params': (user_id, year, year)
            }

            query_results = current_app.extensions['query_results']
            return query_results.fetch_query_results(**select_query)

        except Exception:
            traceback.print_exc()
            raise

    def get_weekly_energy(self, user_id):
        try:
            select_query = {
                'statement': """
                    SELECT report_date, generation_kwh
                    FROM monthly_reports
                       WHERE {} = %s
                      AND report_date >= DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY)
                      AND report_date < DATE_ADD(DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY), INTERVAL 7 DAY)
                    ORDER BY report_date;  
                """,
                "column_name": ('user_id',),
                'params': (user_id,)
            }
            query_results = current_app.extensions['query_results']

            result = query_results.fetch_query_results(**select_query)
            return result  # True if insertion was successful
        except Exception as e:
            # log error here
            print(traceback.print_exc())
            raise e
