import pymysql
from datetime import datetime
db_config = {
    "host":"127.0.0.1",
    "user":"sql_seo2024_ir",
    "password":"1bd0d20735dfd8",
    "database":"sql_seo2024_ir"
}


def comment_adder(data, postid):

    for comment in data['comments']:
        try:
            comment_details = {
                'comment_post_ID': int(postid),
                'comment_author': 'admin',
                'comment_author_email': 'ghostantanie29@gmail.com',
                'comment_author_IP': '127.0.0.1',  
                'comment_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
                'comment_date_gmt': datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'),
                'comment_content': str(comment['content']),
                'comment_karma': 0,
                'comment_approved': 1,
                'comment_agent': 'Python user Agent',
                'comment_type': '',
                'comment_parent': 0,
                'user_id': 275  
            }
            try:
                connection = pymysql.connect(**db_config)
                with connection.cursor() as cursor:
                    sql = """
                    INSERT INTO wp_comments (
                        comment_post_ID, 
                        comment_author, 
                        comment_author_email, 
                        comment_author_IP, 
                        comment_date, 
                        comment_date_gmt, 
                        comment_content, 
                        comment_karma, 
                        comment_approved, 
                        comment_agent, 
                        comment_type, 
                        comment_parent, 
                        user_id
                    ) VALUES (
                        %(comment_post_ID)s, 
                        %(comment_author)s, 
                        %(comment_author_email)s, 
                        %(comment_author_IP)s, 
                        %(comment_date)s, 
                        %(comment_date_gmt)s, 
                        %(comment_content)s, 
                        %(comment_karma)s, 
                        %(comment_approved)s, 
                        %(comment_agent)s, 
                        %(comment_type)s, 
                        %(comment_parent)s, 
                        %(user_id)s
                    )
                    """
                    cursor.execute(sql, comment_details)
                    connection.commit()
                    
                    comment_id = cursor.lastrowid
                    print(comment_id)
                    if comment['spoil']:
                        print("Here")
                        meta_sql = """
                        INSERT INTO wp_commentmeta (
                            comment_id, 
                            meta_key, 
                            meta_value
                        ) VALUES (
                            %s, 
                            %s, 
                            %s
                        )
                        """
                        cursor.execute(meta_sql, (comment_id, 'spoil', '1'))
                        connection.commit()
                                        
                    
                    print("Comment inserted successfully.")
            except Exception as e:
                print(f"Error: {e}")
                pass
        except Exception as e:
            print(e)
            print("CC")
            pass
            