# -*- coding: utf-8 -*-
# Copyright (c) 2019, JINGROW
# See license.txt


import unittest
from unittest.mock import Mock, patch

import jingrow
import responses

from jcloud.api.marketplace import (
	add_app,
	add_version,
	become_publisher,
	branches,
	change_app_plan,
	change_branch,
	create_app_plan,
	create_approval_request,
	get_app,
	get_apps,
	get_apps_with_plans,
	get_latest_approval_request,
	get_marketplace_subscriptions_for_site,
	get_publisher_profile_info,
	get_subscriptions_list,
	new_app,
	options_for_quick_install,
	releases,
	remove_version,
	reset_features_for_plan,
	subscriptions,
	update_app_description,
	update_app_links,
	update_app_plan,
	update_app_summary,
	update_app_title,
	update_publisher_profile,
)
from jcloud.marketplace.pagetype.marketplace_app_plan.test_marketplace_app_plan import (
	create_test_marketplace_app_plan,
)
from jcloud.marketplace.pagetype.marketplace_app_subscription.test_marketplace_app_subscription import (
	create_test_marketplace_app_subscription,
)
from jcloud.jcloud.pagetype.agent_job.agent_job import AgentJob
from jcloud.jcloud.pagetype.app.test_app import create_test_app
from jcloud.jcloud.pagetype.app_release.test_app_release import create_test_app_release
from jcloud.jcloud.pagetype.app_source.test_app_source import create_test_app_source
from jcloud.jcloud.pagetype.marketplace_app.test_marketplace_app import (
	create_test_marketplace_app,
)
from jcloud.jcloud.pagetype.release_group.test_release_group import (
	create_test_release_group,
)
from jcloud.jcloud.pagetype.site.test_site import create_test_bench, create_test_site
from jcloud.jcloud.pagetype.team.test_team import create_test_jcloud_admin_team

PAYLOAD = [
	{
		"name": "develop",
		"commit": {
			"sha": "d11768d928ec7996810898cf627c4d57e8bb917d",
			"url": "http://git.jingrow.com/api/v1/repos/jingrow/jingrow/commits/d11768d928ec7996810898cf627c4d57e8bb917d",
		},
		"protected": True,
	},
	{
		"name": "enterprise-staging",
		"commit": {
			"sha": "3716ef769bbb45d5376c5d6f6ed9a2d52583ef1c",
			"url": "http://git.jingrow.com/api/v1/repos/jingrow/jingrow/commits/3716ef769bbb45d5376c5d6f6ed9a2d52583ef1c",
		},
		"protected": False,
	},
]


@patch.object(AgentJob, "enqueue_http_request", new=Mock())
class TestAPIMarketplace(unittest.TestCase):
	def setUp(self):
		self.app = create_test_app("jerp", "JERP")
		self.team = create_test_jcloud_admin_team()
		self.version = "v0.4"
		self.app_source = create_test_app_source(
			version=self.version, app=self.app, team=self.team.name
		)
		self.app_release = create_test_app_release(self.app_source)
		self.marketplace_app = create_test_marketplace_app(
			app=self.app.name,
			team=self.team.name,
			sources=[{"version": self.version, "source": self.app_source.name}],
		)
		self.plan_data = {
			"price_cny": 820,
			"price_usd": 10,
			"plan_title": "Test Marketplace Plan",
			"features": ["feature 1", "feature 2"],
		}

	def tearDown(self):
		jingrow.set_user("Administrator")
		jingrow.db.rollback()

	def test_create_marketplace_app_plan(self):
		jingrow.set_user(self.team.user)
		before_count = jingrow.db.count("Marketplace App Plan")
		create_app_plan(self.marketplace_app.name, self.plan_data)
		after_count = jingrow.db.count("Marketplace App Plan")
		self.assertEqual(before_count + 1, after_count)

	def test_reset_features_for_plan(self):
		plan_pg = create_app_plan(self.marketplace_app.name, self.plan_data)
		new_features = ["feature 3", "feature 4"]
		reset_features_for_plan(plan_pg, new_features)

		self.assertEqual([feature.description for feature in plan_pg.features], new_features)

	def test_options_for_quick_install(self):
		jingrow_app = create_test_app()

		jingrow_source = create_test_app_source(version=self.version, app=jingrow_app)
		jingrow_release = create_test_app_release(jingrow_source)
		create_test_marketplace_app(
			app=jingrow_app.name,
			sources=[{"version": self.version, "source": jingrow_source.name}],
		)

		group1 = create_test_release_group([jingrow_app], jingrow_version=self.version)
		group2 = create_test_release_group([jingrow_app, self.app])
		group1.db_set("team", self.team.name)
		group2.db_set("team", self.team.name)
		bench1 = create_test_bench(
			group=group1,
			apps=[
				{
					"app": jingrow_app.name,
					"hash": jingrow_release.hash,
					"source": jingrow_source.name,
					"release": jingrow_release.name,
				}
			],
		)
		bench2 = create_test_bench(
			group=group2,
			apps=[
				{
					"app": jingrow_app.name,
					"hash": jingrow_release.hash,
					"source": jingrow_source.name,
					"release": jingrow_release.name,
				},
				{
					"app": self.app.name,
					"hash": self.app_release.hash,
					"source": self.app_source.name,
					"release": self.app_release.name,
				},
			],
		)

		create_test_site(subdomain="test1", bench=bench1.name, team=self.team.name)
		create_test_site(subdomain="test2", bench=bench2.name, team=self.team.name)

		jingrow.set_user(self.team.user)
		options = options_for_quick_install(self.app.name)

		self.assertEqual(options["release_groups"][0]["name"], group1.name)

	def test_add_app(self):
		app = create_test_app("test_app", "Test App")
		app_source = create_test_app_source(version=self.version, app=app)
		marketplace_app = add_app(source=app_source.name, app=app.name)
		self.assertIsNotNone(jingrow.db.exists("Marketplace App", marketplace_app))

	def test_add_app_already_added(self):
		app = create_test_app("test_app3", "Test App 3")
		app_source = create_test_app_source(version=self.version, app=app)
		create_test_marketplace_app(
			app=app.name,
			team=self.team.name,
			sources=[{"version": self.version, "source": app_source.name}],
		)
		new_source = create_test_app_source(version="v0.1", app=app)
		jingrow.set_user(self.team.user)
		marketplace_app = add_app(source=new_source.name, app=app.name)
		self.assertIsNotNone(jingrow.db.exists("Marketplace App", marketplace_app))

	def test_get_marketplace_subscriptions_for_site(self):
		site = create_test_site(subdomain="test1", team=self.team.name)
		plan = create_test_marketplace_app_plan(self.marketplace_app.name)
		create_test_marketplace_app_subscription(
			site=site.name, app=self.app.name, team=self.team.name, plan=plan.name
		)

		self.assertIsNotNone(get_marketplace_subscriptions_for_site(site.name))

	def test_change_app_plan(self):
		subscription = create_test_marketplace_app_subscription()
		new_plan = create_test_marketplace_app_plan()
		change_app_plan(subscription.name, new_plan.name)

		self.assertEqual(
			new_plan.name,
			jingrow.db.get_value("Subscription", subscription.name, "plan"),
		)

	def test_get_subscription_list(self):
		self.assertEqual([], get_subscriptions_list("jingrow"))
		create_test_marketplace_app_subscription(app="jingrow")
		self.assertIsNotNone(get_subscriptions_list("jingrow"))

	def test_update_app_plan(self):
		m_plan = create_test_marketplace_app_plan()

		updated_plan_data = {
			"price_cny": m_plan.price_cny + 100,
			"price_usd": m_plan.price_usd + 1,
			"title": m_plan.title + " updated",
			"features": ["feature 3", "feature 4"],
		}
		update_app_plan(m_plan.name, updated_plan_data)
		m_plan.reload()

		self.assertEqual(m_plan.price_cny, updated_plan_data["price_cny"])
		self.assertEqual(m_plan.price_usd, updated_plan_data["price_usd"])
		self.assertEqual(m_plan.title, updated_plan_data["title"])
		self.assertEqual(m_plan.features[0].description, updated_plan_data["features"][0])
		self.assertEqual(m_plan.features[1].description, updated_plan_data["features"][1])

	def test_become_publisher(self):
		jingrow.set_user(self.team.user)
		become_publisher()
		self.team.reload()
		self.assertTrue(self.team.is_developer)

	def test_get_apps(self):
		jingrow.set_user(self.team.user)
		self.marketplace_app.db_set("team", self.team.name)
		apps = get_apps()
		self.assertEqual(apps[0].name, self.marketplace_app.name)

	def test_get_app(self):
		app = get_app("jerp")
		self.assertEqual(app.name, "jerp")

	def test_update_app_title(self):
		jingrow.set_user(self.team.user)
		update_app_title(self.marketplace_app.name, "New Title")
		self.marketplace_app.reload()
		self.assertEqual(self.marketplace_app.title, "New Title")

	def test_update_app_links(self):
		jingrow.set_user(self.team.user)
		update_app_links(
			self.marketplace_app.name,
			{
				"website": "https://github.com",
				"support": "https://github.com",
				"documentation": "https://github.com",
				"privacy_policy": "https://github.com",
				"terms_of_service": "https://github.com",
			},
		)
		self.marketplace_app.reload()
		self.assertEqual(self.marketplace_app.website, "https://github.com")
		self.assertEqual(self.marketplace_app.support, "https://github.com")

	def test_update_app_summary(self):
		jingrow.set_user(self.team.user)
		summary = jingrow.mock("paragraph")
		update_app_summary(self.marketplace_app.name, summary)
		self.marketplace_app.reload()
		self.assertEqual(self.marketplace_app.description, summary)

	def test_update_app_description(self):
		jingrow.set_user(self.team.user)
		desc = jingrow.mock("paragraph")
		update_app_description(self.marketplace_app.name, desc)
		self.marketplace_app.reload()
		self.assertEqual(self.marketplace_app.long_description, desc)

	def test_releases(self):
		jingrow.set_user(self.team.user)
		r = releases({"app": self.marketplace_app.name, "source": self.app_source.name})
		self.assertEqual(r[0].name, self.app_release.name)

	def test_app_release_approvals(self):
		jingrow.set_user(self.team.user)
		create_approval_request(self.marketplace_app.name, self.app_release.name)
		latest_approval = get_latest_approval_request(self.app_release.name)
		self.assertIsNotNone(latest_approval)

	def test_new_app(self):
		app = {
			"name": "email_delivery_service",
			"title": "Email Delivery Service",
			"version": "v0.4",
			"repository_url": "http://git.jingrow.com/jingrow/email_delivery_service",
			"branch": "develop",
			"github_installation_id": "",
		}
		name = new_app(app)
		self.assertEqual(name, app["name"])

	def test_get_apps_with_plans(self):
		jingrow_app = create_test_app()
		group2 = create_test_release_group(
			[jingrow_app, self.app], jingrow_version=self.version
		)
		create_test_marketplace_app(
			app=jingrow_app.name,
			sources=[{"version": self.version, "source": group2.apps[0].source}],
		)
		create_app_plan(jingrow_app.name, self.plan_data)
		apps = get_apps_with_plans(["jingrow", "jerp"], group2.name)
		self.assertEqual(apps[0].name, jingrow_app.name)

	def test_publisher_profile(self):
		jingrow.set_user(self.team.user)
		publisher_info = {
			"display_name": "Test Publisher",
			"contact_email": self.team.user,
			"website": "https://github.com",
		}
		update_publisher_profile(publisher_info)
		updated_name = "Test Publisher 2"
		update_publisher_profile({"display_name": updated_name})
		info = get_publisher_profile_info()
		self.assertEqual(info["profile_info"].contact_email, self.team.user)
		self.assertEqual(info["profile_info"].display_name, updated_name)

	def test_get_subscription(self):
		site = create_test_site(subdomain="test1", team=self.team.name)
		plan = create_test_marketplace_app_plan(self.marketplace_app.name)
		create_test_marketplace_app_subscription(
			site=site.name, app=self.app.name, team=self.team.name, plan=plan.name
		)
		jingrow.set_user(self.team.user)
		self.assertIsNotNone(subscriptions())

	def test_change_branch(self):
		old_branch = self.app_source.branch
		change_branch(
			self.marketplace_app.name, self.app_source.name, "v0.4", "develop"
		)
		self.app_source.reload()
		self.assertNotEqual(old_branch, self.app_source.branch)

	def test_add_version(self):
		old_versions = len(self.marketplace_app.sources)
		add_version(self.marketplace_app.name, "develop", "v0.1")
		self.marketplace_app.reload()
		self.assertEqual(old_versions + 1, len(self.marketplace_app.sources))

	def test_remove_version(self):
		old_versions = len(self.marketplace_app.sources)
		add_version(self.marketplace_app.name, "develop", "v0.1")
		remove_version(self.marketplace_app.name, "v0.1")
		self.marketplace_app.reload()
		self.assertEqual(old_versions, len(self.marketplace_app.sources))

	@responses.activate
	def test_branches(self):
		jingrow.set_user(self.team.user)
		responses.get(
			url=f"http://git.jingrow.com/api/v1/repos/{self.app_source.repository_owner}/{self.app_source.repository}/branches?per_page=100",
			json=PAYLOAD,
			status=200,
			headers={},
		)
		results = branches(self.app_source.name)
		self.assertEqual(len(results), 2)
