from sql_queries import DashboardQueries

class ProjectService:
    @staticmethod
    def get_available_dates_logic(cursor):
        cursor.execute(DashboardQueries.GET_AVAILABLE_DATES)
        rows = cursor.fetchall()
        return [row['crawl_date'].strftime("%Y.%m.%d") for row in rows if row['crawl_date']]

    @staticmethod
    def get_project_data_logic(cursor, date_str):
        target_date = date_str.replace(".", "-") if date_str and date_str != "-" else None
        
        if not target_date:
            cursor.execute(DashboardQueries.GET_LAST_CRAWL_DATE)
            res = cursor.fetchone()
            target_date = res['last_date']
        
        if not target_date:
            return {"projects": []}

        cursor.execute(DashboardQueries.GET_PROJECT_LIST, (target_date,))
        rows = cursor.fetchall()
        
        projects = []
        for r in rows:
            name = r['short_nm'] if r['short_nm'] and r['short_nm'].strip() else r['project_nm']
            projects.append([
                name, r['department'], r['master'], 
                r['recent_log'], r['file_count'], 
                r['continent'], r['country']
            ])
        return {"projects": projects}
