import uuid
from unittest.mock import patch

from django.test import override_settings, TestCase

from vbv_lernwelt.core.models import User
from vbv_lernwelt.shop.services import (
    get_payment_url,
    init_transaction,
    InitTransactionException,
)

REDIRECT_URL = "http://testserver/redirect-url"


class DatatransServiceTest(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(
            username=uuid.uuid4().hex,
            email=uuid.uuid4().hex,
            password="password",
            is_active=True,
        )

    @override_settings(DATATRANS_BASIC_AUTH_KEY="BASIC_AUTH_KEY")
    @patch("vbv_lernwelt.shop.services.requests.post")
    @patch("vbv_lernwelt.shop.services.uuid.uuid4")
    def test_init_transaction_201(self, mock_uuid, mock_post):
        # GIVEN
        mock_uuid.return_value = uuid.uuid4()
        mock_post.return_value.status_code = 201
        mock_post.return_value.json.return_value = {
            "transactionId": 1234567890,
        }

        self.user.language = "it"

        # WHEN
        transaction_id = init_transaction(
            user=self.user,
            amount_chf_centimes=300_00,
            redirect_url_success=f"{REDIRECT_URL}/success",
            redirect_url_error=f"{REDIRECT_URL}/error",
            redirect_url_cancel=f"{REDIRECT_URL}/cancel",
            webhook_url=f"{REDIRECT_URL}/webhook",
        )

        self.assertEqual(1234567890, transaction_id)

        # THEN
        mock_post.assert_called_once_with(
            url="https://api.sandbox.datatrans.com/v1/transactions",
            json={
                "autoSettle": True,
                "amount": 300_00,
                "currency": "CHF",
                "language": self.user.language,
                "refno": str(mock_uuid()),
                "webhook": {"url": f"{REDIRECT_URL}/webhook"},
                "redirect": {
                    "successUrl": f"{REDIRECT_URL}/success",
                    "errorUrl": f"{REDIRECT_URL}/error",
                    "cancelUrl": f"{REDIRECT_URL}/cancel",
                },
            },
            headers={
                "Authorization": "Basic BASIC_AUTH_KEY",
                "Content-Type": "application/json",
            },
        )

    @patch("vbv_lernwelt.shop.services.requests.post")
    def test_init_transaction_500(self, mock_post):
        # GIVEN
        mock_post.return_value.status_code = 500

        # WHEN / THEN
        with self.assertRaises(InitTransactionException):
            init_transaction(
                user=self.user,
                amount_chf_centimes=300_00,
                redirect_url_success=f"/success",
                redirect_url_error=f"/error",
                redirect_url_cancel=f"/cancel",
                webhook_url=f"/webhook",
            )

    def test_get_payment_url(self):
        # GIVEN
        transaction_id = "1234567890"

        # WHEN
        url = get_payment_url(transaction_id)

        # THEN
        self.assertEqual(
            url,
            f"https://pay.sandbox.datatrans.com/v1/start/{transaction_id}",
        )
