from datetime import datetime
from sql_queries import InquiryQueries

class InquiryService:
    @staticmethod
    def get_inquiries_logic(cursor, pm_type=None, category=None, status=None, keyword=None):
        sql = InquiryQueries.SELECT_BASE
        params = []
        if pm_type:
            sql += " AND pm_type = %s"
            params.append(pm_type)
        if category:
            sql += " AND category = %s"
            params.append(category)
        if status:
            sql += " AND status = %s"
            params.append(status)
        if keyword:
            sql += " AND (content LIKE %s OR author LIKE %s OR project_nm LIKE %s)"
            params.extend([f"%{keyword}%", f"%{keyword}%", f"%{keyword}%"])
        
        sql += f" {InquiryQueries.ORDER_BY_DESC}"
        cursor.execute(sql, params)
        return cursor.fetchall()

    @staticmethod
    def get_inquiry_detail_logic(cursor, inquiry_id):
        cursor.execute(InquiryQueries.SELECT_BY_ID, (inquiry_id,))
        return cursor.fetchone()

    @staticmethod
    def update_inquiry_reply_logic(cursor, conn, inquiry_id, req):
        handled_date = datetime.now().strftime("%Y.%m.%d")
        cursor.execute(InquiryQueries.UPDATE_REPLY, (req.reply, req.status, req.handler, handled_date, inquiry_id))
        conn.commit()
        return {"success": True}

    @staticmethod
    def delete_inquiry_reply_logic(cursor, conn, inquiry_id):
        cursor.execute(InquiryQueries.DELETE_REPLY, (inquiry_id,))
        conn.commit()
        return {"success": True}
