from database import get_connection


def create_crm_notification(
    employee_id,
    title,
    message,
    notification_type="general"
):

    conn = None
    cursor = None

    try:

        conn = get_connection()

        cursor = conn.cursor()

        cursor.execute(
            """
            INSERT INTO crm_notifications
            (
                employee_id,
                title,
                message,
                type,
                is_read
            )
            VALUES
            (
                %s,
                %s,
                %s,
                %s,
                0
            )
            """,
            (
                employee_id,
                title,
                message,
                notification_type
            )
        )

        conn.commit()

        print(
            f"CRM notification created for {employee_id}"
        )

        return True

    except Exception as e:

        if conn:
            conn.rollback()

        print(
            "CREATE CRM NOTIFICATION ERROR:",
            e
        )

        return False

    finally:

        if cursor:
            cursor.close()

        if conn:
            conn.close()
