import influxframework
from influxframework.utils import cstr, strip_html


def execute():
	for doctype in ("Lead", "Prospect", "Opportunity"):
		if not influxframework.db.has_column(doctype, "notes"):
			continue

		dt = influxframework.qb.DocType(doctype)
		records = (
			influxframework.qb.from_(dt)
			.select(dt.name, dt.notes, dt.modified_by, dt.modified)
			.where(dt.notes.isnotnull() & dt.notes != "")
		).run(as_dict=True)

		for d in records:
			if strip_html(cstr(d.notes)).strip():
				doc = influxframework.get_doc(doctype, d.name)
				doc.append("notes", {"note": d.notes, "added_by": d.modified_by, "added_on": d.modified})
				doc.update_child_table("notes")

		influxframework.db.sql_ddl(f"alter table `tab{doctype}` drop column `notes`")
