import logging
from django import forms

from core import OrderStatus
from core.models import (
    Order,
    OrderLine,
    ShippingMethod,
    TrackingNumber,
    Coupon,
    ProductPhoto
)

logger = logging.getLogger(__name__)


class CouponForm(forms.ModelForm):
    class Meta:
        model = Coupon
        fields = (
            'type',
            'name',
            'code',
            'valid_from',
            'valid_to',
            'discount_value_type',
            'discount_value',
            'products',
        )
        widgets = {
            'valid_from': forms.DateInput(attrs={
                'type': 'date'
            }),
            'valid_to': forms.DateInput(attrs={
                'type': 'date'
            }),
        }


class OrderLineFulfillForm(forms.ModelForm):
    # send_shipment_details_to_customer = forms.BooleanField(initial=True)

    class Meta:
        model = OrderLine
        fields = ('quantity_fulfilled',)
        widgets = {
            'quantity_fulfilled': forms.NumberInput(attrs={
                'min': 0,
            })
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['quantity_fulfilled'].widget.attrs['max'] = self.instance.quantity


OrderLineFormset = forms.inlineformset_factory(
    Order, OrderLine, form=OrderLineFulfillForm,
    extra=0, can_delete=False
)


class OrderCancelForm(forms.ModelForm):
    class Meta:
        model = Order
        fields = ('status',)
        widgets = {
            'status': forms.HiddenInput()
        }


class OrderTrackingForm(forms.ModelForm):
    # send_shipment_details_to_customer = forms.BooleanField(initial=True)

    class Meta:
        model = TrackingNumber
        fields = ('tracking_id',)


OrderTrackingFormset = forms.inlineformset_factory(
    Order, TrackingNumber, form=OrderTrackingForm,
    extra=1, can_delete=False
)


class ProductPhotoForm(forms.ModelForm):
    class Meta:
        model = ProductPhoto
        fields = ('image',)
