# Copyright (c) 2020, JINGROW
# See license.txt
from __future__ import annotations

import unittest
from unittest.mock import Mock, patch

import jingrow
from jingrow.tests.ui_test_helpers import create_test_user

from jcloud.jcloud.pagetype.account_request.test_account_request import (
	create_test_account_request,
)
from jcloud.jcloud.pagetype.team.team import Team


def create_test_jcloud_admin_team(email: str | None = None) -> Team:
	"""Create test jcloud admin user."""
	if not email:
		email = jingrow.mock("email")
	create_test_user(email)
	user = jingrow.get_pg("User", {"email": email})
	user.remove_roles(*jingrow.get_all("Role", pluck="name"))
	user.add_roles("Jcloud Admin")
	return create_test_team(email)


@patch.object(Team, "update_billing_details_on_jingrowio", new=Mock())
@patch.object(Team, "create_stripe_customer", new=Mock())
def create_test_team(email: str | None = None, country="China") -> Team:
	"""Create test team pg."""
	if not email:
		email = jingrow.mock("email")
	create_test_user(email)  # ignores if user already exists
	user = jingrow.get_value("User", {"email": email}, "name")
	team = jingrow.get_pg({"pagetype": "Team", "user": user, "enabled": 1, "country": country}).insert(
		ignore_if_duplicate=True
	)
	team.reload()
	return team


class TestTeam(unittest.TestCase):
	def tearDown(self):
		jingrow.db.rollback()

	def test_create_new_method_works(self):
		account_request = create_test_account_request("testsubdomain")
		team_count_before = jingrow.db.count("Team")
		with patch.object(Team, "create_stripe_customer"):
			Team.create_new(account_request, "first name", "last name", "test@email.com", country="China")
		team_count_after = jingrow.db.count("Team")
		self.assertGreater(team_count_after, team_count_before)

	def test_new_team_has_correct_billing_name(self):
		account_request = create_test_account_request("testsubdomain")
		with patch.object(Team, "create_stripe_customer"):
			team = Team.create_new(
				account_request, "first name", "last name", "test@email.com", country="China"
			)
		self.assertEqual(team.billing_name, "first name last name")

	def test_create_user_for_member_adds_team_member(self):
		# create system manager to pass mandatory site requirement
		Team.create_user("sys_mgr", email="testuser1@gmail.com", role="System Manager")

		team = create_test_team()
		email = "testuser@jingrow.cloud"
		team.create_user_for_member("test", "user", "testuser@jingrow.cloud")
		self.assertTrue(team.has_member(email))  # kinda dumb because we assume has_member method is correct

	def test_new_team_has_correct_currency_set(self):
		account_request1 = create_test_account_request("testsubdomain")
		with patch.object(Team, "create_stripe_customer"):
			team1 = Team.create_new(account_request1, "Jon", "Doe", "test@gmail.com", country="China")
		self.assertEqual(team1.currency, "CNY")

		account_request2 = create_test_account_request("testsubdomain2")
		with patch.object(Team, "create_stripe_customer"):
			team2 = Team.create_new(
				account_request2, "John", "Meyer", "jonmeyer@gmail.com", country="Pakistan"
			)
		self.assertEqual(team2.currency, "USD")
