import logging
from decimal import Decimal

from django.test import TestCase, Client, RequestFactory
from django.urls import reverse
from django.conf import settings
from measurement.measures import Weight
from paypalcheckoutsdk.orders import OrdersCreateRequest, OrdersCaptureRequest
from paypalcheckoutsdk.core import PayPalHttpClient, SandboxEnvironment

from accounts.models import User, Address
from core.models import Product, Order, Coupon
from core import CoffeeGrind
from dashboard.forms import (
    CouponForm,
    OrderLineFulfillForm,
    OrderTrackingForm,
    ProductPhotoForm,
)
from dashboard.views import (
    DashboardHomeView,
    DashboardConfigView,
    CatalogView,
    StockView,
    ShippingRateDetailView,
    ShippingRateCreateView,
    ShippingRateUpdateView,
    ShippingRateDeleteView,
    CouponListView,
    CouponCreateView,
    CouponDetailView,
    CouponUpdateView,
    CouponDeleteView,
    OrderListView,
    OrderDetailView,
    OrderFulfillView,
    OrderCancelView,
    OrderTrackingView,
    CategoryListView,
    CategoryCreateView,
    CategoryDetailView,
    CategoryUpdateView,
    CategoryDeleteView,
    ProductListView,
    ProductDetailView,
    ProductCreateView,
    ProductUpdateView,
    ProductDeleteView,
    ProductPhotoCreateView,
    ProductPhotoDeleteView,
    ProductVariantCreateView,
    ProductVariantUpdateView,
    ProductVariantDeleteView,
    ProductVariantStockUpdateView,
    ProductOptionDetailView,
    ProductOptionCreateView,
    ProductOptionUpdateView,
    ProductOptionDeleteView,
    CustomerListView,
    CustomerDetailView,
    CustomerUpdateView,
)

logger = logging.getLogger(__name__)


class ProductCreateViewTests(TestCase):
    fixtures = [
        'site_settings_and_shipping_rates.json',
        'accounts.json',
        'products.json',
        'coupons.json',
        'orders.json'
    ]

    @classmethod
    def setUpTestData(cls):
        cls.admin_user = User.objects.get(pk=1)

    def setUp(self):
        self.client = Client()
        self.client.force_login(self.admin_user)

    def test_view_url_exists_at_desired_location(self):
        response = self.client.get('/dashboard/products/new/')
        self.assertEqual(response.status_code, 200)

    def test_view_url_accesible_by_name(self):
        response = self.client.get(
            reverse('dashboard:product-create')
        )
        self.assertEqual(response.status_code, 200)

    def test_view_uses_correct_template(self):
        response = self.client.get(
            reverse('dashboard:product-create')
        )
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'dashboard/product/create_form.html')


class OrderCancelViewTests(TestCase):
    fixtures = [
        'site_settings_and_shipping_rates.json',
        'accounts.json',
        'products.json',
        'coupons.json',
        'orders.json'
    ]

    @classmethod
    def setUpTestData(cls):
        cls.admin_user = User.objects.get(pk=1)

    def setUp(self):
        self.client = Client()
        self.client.force_login(self.admin_user)

    def test_view_url_exists_at_desired_location(self):
        response = self.client.get('/dashboard/orders/1/cancel/')
        self.assertEqual(response.status_code, 200)

    def test_view_url_accesible_by_name(self):
        response = self.client.get(
            reverse('dashboard:order-cancel', kwargs={'pk': 1})
        )
        self.assertEqual(response.status_code, 200)

    def test_view_uses_correct_template(self):
        response = self.client.get(
            reverse('dashboard:order-cancel', kwargs={'pk': 1})
        )
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'dashboard/order/cancel_form.html')
