from unittest.mock import Mock

from saleor.product.models import ProductVariant
from saleor.product.utils.availability import get_variant_availability
from tests.api.utils import get_graphql_content

QUERY_GET_VARIANT_PRICING = """
query {
  products(first: 1) {
    edges {
      node {
        variants {
          pricing {
            available
            onSale
            
            discount {
              currency
              net {
                amount
              }
            }
            
            priceUndiscounted {
              currency
              net {
                amount
              }
            }
            
            price {
              currency
              net {
                amount
              }
            }
          }
        }
      }
    }
  }
}
"""


def test_get_variant_pricing_on_sale(api_client, sale, product):
    response = api_client.post_graphql(QUERY_GET_VARIANT_PRICING, {})
    content = get_graphql_content(response)

    pricing = (
        content['data']['products']['edges'][0]['node']
        ['variants'][0]['pricing'])

    # ensure the availability was correctly retrieved and sent
    assert pricing

    # check availability
    assert pricing['available'] is True
    assert pricing['onSale'] is True

    # check the discount
    assert pricing['discount']['currency'] == product.price.currency
    assert pricing['discount']['net']['amount'] == 5.0

    # check the undiscounted price
    assert (
        pricing['priceUndiscounted']['currency'] == product.price.currency)
    assert (
        pricing['priceUndiscounted']['net']['amount'] == product.price.amount)

    # check the discounted price
    assert pricing['price']['currency'] == product.price.currency
    assert pricing['price']['net']['amount'] == 5.0


def test_get_variant_pricing_not_on_sale(api_client, product):
    response = api_client.post_graphql(QUERY_GET_VARIANT_PRICING, {})
    content = get_graphql_content(response)

    pricing = (
        content['data']['products']['edges'][0]['node']
        ['variants'][0]['pricing'])

    # ensure the availability was correctly retrieved and sent
    assert pricing

    # check availability
    assert pricing['available'] is True
    assert pricing['onSale'] is False

    # check the discount
    assert pricing['discount'] is None

    # check the undiscounted price
    assert (
        pricing['priceUndiscounted']['currency'] == product.price.currency)
    assert (
        pricing['priceUndiscounted']['net']['amount'] == product.price.amount)

    # check the discounted price
    assert pricing['price']['currency'] == product.price.currency
    assert pricing['price']['net']['amount'] == product.price.amount


def test_variant_pricing(
        variant: ProductVariant, monkeypatch, settings, taxes):
    pricing = get_variant_availability(variant)
    assert pricing.price == variant.get_price()
    assert pricing.price_local_currency is None

    monkeypatch.setattr(
        'django_prices_openexchangerates.models.get_rates',
        lambda c: {'PLN': Mock(rate=2)})

    settings.DEFAULT_COUNTRY = 'PL'
    settings.OPENEXCHANGERATES_API_KEY = 'fake-key'

    pricing = get_variant_availability(variant, local_currency='PLN')
    assert pricing.price_local_currency.currency == 'PLN'
    assert pricing.available

    pricing = get_variant_availability(variant, taxes=taxes)
    assert pricing.price.tax.amount
    assert pricing.price_undiscounted.tax.amount
    assert pricing.price_undiscounted.tax.amount
    assert pricing.available

@@ -869,32 +869,108 @@
 sale, product):%0A
+    price = product.price%0A    discounted_price = price.amount - sale.value%0A%0A
     response = a
@@ -1430,32 +1430,24 @@
 rrency'%5D == 
-product.
 price.curren
@@ -1500,19 +1500,32 @@
 nt'%5D == 
-5.0
+discounted_price
 %0A%0A    # 
@@ -1614,32 +1614,24 @@
 rrency'%5D == 
-product.
 price.curren
@@ -1700,32 +1700,24 @@
 amount'%5D == 
-product.
 price.amount
@@ -1791,32 +1791,24 @@
 rrency'%5D == 
-product.
 price.curren
@@ -1862,11 +1862,24 @@
  == 
-5.0
+discounted_price
 %0A%0A%0Ad

Remove hardcoded discount price