from django.template.defaultfilters import first
from django.urls import reverse
from django.shortcuts import render, redirect
import pytest
from apps.accounts.models import User
import json
from django.core import mail
from termcolor import colored
from apps.subscriptions.models import *
from faker import Faker


@pytest.mark.django_db
def test_find_correct_memership(client,user_signup_data):
    """ Test if plan ID is valid when select plan """
    assert Payment.objects.all().count() == 0
    url = client.post(path=reverse("accounts:signup"), data=user_signup_data)
    response = json.loads(url.content)
    assert response["result"] == "Found Success"
    assert (
        response["message"]
        == "Membership ID Found Success"
    )

    assert Payment.objects.all().count() == 0

#

@pytest.mark.django_db
def test_signup_create_subscription_and_payment(client,user_signup_data_with_order_id):
    """ Test create subscription and payment success """
    assert Subscription.objects.all().count() == 0
    request = client.post(reverse('accounts:signup'),data=user_signup_data_with_order_id)
    print(colored(request.content,'yellow'))
    response = json.loads(request.content)
    assert response['result'] == "success"
    assert response['message'] == f"You are signed up. Check your email {user_signup_data_with_order_id['email']} to activate your account."
    assert Payment.objects.all().count() == 1
    assert Subscription.objects.all().count() == 1

@pytest.mark.django_db
def test_user_already_signup_should_return_error(client, user_signup_data_already_signup: dict) -> None:
    assert User.objects.count() == 1
    url = client.post(path=reverse("accounts:signup"), data=user_signup_data_already_signup)
    response = json.loads(url.content)
    assert response["result"] == "error"
    assert response["message"] == "Email already used. Kindly Try Again With Another Email."
    # database stall the same count because user not added
    assert User.objects.count() == 1

