import json

import jingrow
import requests


def send_developer_email(email, app_name, branch, repository_url, version):
	dev = jingrow.get_pg("User", {"email": email})
	developer_name = dev.full_name
	email_args = {
		"recipients": email,
		"subject": "Jingrow: Make your app's GitHub Repository Public",
		"template": "marketplace_app_visibility",
		"args": {
			"developer_name": developer_name,
			"app_name": app_name,
			"branch": branch,
			"version": version,
			"repository_url": repository_url,
		},
	}
	jingrow.enqueue(method=jingrow.sendmail, queue="short", timeout=300, **email_args)


@jingrow.whitelist()
def send_emails(columns, data):
	jingrow.only_for("System Manager")
	data = json.loads(data)
	for row in data:
		visibility = row.get("visibility")
		if visibility != "Private":
			continue
		app_name = row.get("app_name")
		branch = row.get("branch")
		repository_url = row.get("repository_url")
		email = row.get("team")
		version = row.get("version")
		send_developer_email(email, app_name, branch, repository_url, version)


def check_repository_visibility(repository_url, personal_access_token):
	try:
		repo_parts = repository_url.split("git.jingrow.com/")[1].rstrip(".git").split("/")
		owner = repo_parts[0]
		repo_name = repo_parts[1]
	except IndexError:
		return "Error: Invalid repository URL format."

	api_url = f"http://git.jingrow.com/api/v1/repos/{owner}/{repo_name}"

	headers = {"Authorization": f"token {personal_access_token}"}

	try:
		response = requests.get(api_url, headers=headers)

		if response.status_code == 200:
			repo_data = response.json()
			if repo_data.get("private"):
				return "Private"
			return "Public"
		if response.status_code == 404:
			return "Private"
		return "Private"
	except Exception:
		return "Error"


def execute(filters=None):
	jingrow.only_for("System Manager")

	columns = [
		{"fieldname": "app_name", "label": "Application Name", "fieldtype": "Data", "width": 200},
		{"fieldname": "team", "label": "Team", "fieldtype": "Data", "width": 200},
		{"fieldname": "version", "label": "Version", "fieldtype": "Data", "width": 100},
		{"fieldname": "source", "label": "Source", "fieldtype": "Data", "width": 100},
		{"fieldname": "repository_url", "label": "Repository URL", "fieldtype": "Data", "width": 300},
		{"fieldname": "branch", "label": "Branch", "fieldtype": "Data", "width": 100},
		{
			"fieldname": "visibility",
			"label": "Visibility",
			"fieldtype": "Data",
			"width": 100,
		},
	]

	data = jingrow.db.sql(
		"""
        SELECT
            ma.name AS app_name,
			t.user AS team,
            mav.version AS version,
            mav.source AS source,
            asrc.repository_url AS repository_url,
            asrc.branch AS branch
        FROM
            `tabMarketplace App` ma
        JOIN
            `tabMarketplace App Version` mav ON ma.name = mav.parent
        JOIN
            `tabApp Source` asrc ON mav.source = asrc.name
		JOIN
			`tabTeam` t ON ma.team = t.name
        """,
		as_dict=True,
	)
	personal_access_token = jingrow.db.get_value("Jcloud Settings", "None", "github_pat_token")

	visibility_cache = {}
	for row in data:
		repo_url = row["repository_url"]
		# Check if the visibility status is already cached for this repository URL
		if repo_url in visibility_cache:
			row["visibility"] = visibility_cache[repo_url]
		else:
			# Check visibility status and cache it
			visibility_status = check_repository_visibility(repo_url, personal_access_token)
			row["visibility"] = visibility_status
			# Store the result in the cache for future reference
			visibility_cache[repo_url] = visibility_status
	return columns, data
