# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE

import frappe
from frappe.query_builder import DocType, Interval
from frappe.query_builder.functions import Now


def get_notification_config():
	return {
		"for_doctype": {
			"Error Log": {"seen": 0},
			"Communication": {"status": "Open", "communication_type": "Communication"},
			"ToDo": "frappe.core.notifications.get_things_todo",
			"Event": "frappe.core.notifications.get_todays_events",
			"Error Snapshot": {"seen": 0, "parent_error_snapshot": None},
			"Workflow Action": {"status": 'Open'}
		},
	}

def get_things_todo(as_list=False):
	"""Returns a count of incomplete todos"""
	data = frappe.get_list("ToDo",
		fields=["name", "description"] if as_list else "count(*)",
		filters=[["ToDo", "status", "=", "Open"]],
		or_filters=[["ToDo", "owner", "=", frappe.session.user],
			["ToDo", "assigned_by", "=", frappe.session.user]],
		as_list=True)

	if as_list:
		return data
	else:
		return data[0][0]

def get_todays_events(as_list=False):
	"""Returns a count of todays events in calendar"""
	from frappe.desk.doctype.event.event import get_events
	from frappe.utils import nowdate
	today = nowdate()
	events = get_events(today, today)
	return events if as_list else len(events)

def get_unseen_likes():
	"""Returns count of unseen likes"""

	comment_doctype = DocType("Comment")
	return frappe.db.count(comment_doctype,
					filters=(
						(comment_doctype.comment_type == "Like")
						& (comment_doctype.modified >= Now() - Interval(years=1))
						& (comment_doctype.owner.notnull())
						& (comment_doctype.owner != frappe.session.user)
						& (comment_doctype.reference_owner == frappe.session.user)
						& (comment_doctype.seen == 0)
					)
				)


def get_unread_emails():
	"returns count of unread emails for a user"

	communication_doctype = DocType("Communication")
	user_doctype = DocType("User")
	distinct_email_accounts = (
		frappe.qb.from_(user_doctype)
		.select(user_doctype.email_account)
		.where(user_doctype.parent == frappe.session.user)
		.distinct()
	)

	return frappe.db.count(communication_doctype, filters=(
			(communication_doctype.communication_type == "Communication")
			& (communication_doctype.communication_medium == "Email")
			& (communication_doctype.sent_or_received == "Received")
			& (communication_doctype.email_status.notin(["spam", "Trash"]))
			& (communication_doctype.email_account.isin(distinct_email_accounts))
			& (communication_doctype.modified >= Now() - Interval(years=1))
			& (communication_doctype.seen == 0)
	))

@@ -1425,19 +1425,16 @@
 octype,%0A
-%09%09%09
 %09%09filter
@@ -1437,19 +1437,16 @@
 lters=(%0A
-%09%09%09
 %09%09%09(comm
@@ -1480,27 +1480,24 @@
  %22Like%22)%0A%09%09%09
-%09
-%09%09
 & (comment_d
@@ -1542,19 +1542,16 @@
 ars=1))%0A
-%09%09%09
 %09%09%09& (co
@@ -1580,27 +1580,24 @@
 tnull())%0A%09%09%09
-%09%09
-%09
 & (comment_d
@@ -1625,35 +1625,32 @@
 e.session.user)%0A
-%09%09%09
 %09%09%09& (comment_do
@@ -1695,19 +1695,16 @@
 n.user)%0A
-%09%09%09
 %09%09%09& (co
@@ -1734,16 +1734,10 @@
 )%0A%09%09
-%09%09%09)%0A%09%09%09
+)%0A
 %09)%0A%0A
@@ -2106,17 +2106,19 @@
 doctype,
- 
+%0A%09%09
 filters=
@@ -2552,11 +2552,14 @@
  == 0)%0A%09
-)
+%09)%0A%09
 )%0A

Fix indentation