from django.db import models
from common.models.base import TimeStampedModel

# from api.producers_api.accounts.models import User
from users.models import User

from producers_products_variants.models.products_vairants import ProductsVariants

class Coupon(TimeStampedModel):
    PERCENTAGE = 'percentage'
    FIXED = 'fixed'
    DISCOUNT_TYPE = [
        (PERCENTAGE, 'Percentage'),
        (FIXED, 'Fixed Amount')
    ]

    APPLIES_TO = [
        ('order_items', 'Order Items'),
        ('shipping', 'Shipping'),
        ('both', 'Both'),
    ]

    code = models.CharField(max_length=50, unique=True)
    discount_type = models.CharField(max_length=20, choices=DISCOUNT_TYPE)
    discount_value = models.DecimalField(max_digits=10, decimal_places=2)
    valid_from = models.DateTimeField()
    valid_to = models.DateTimeField()
    applies_to = models.CharField(max_length=20, choices=APPLIES_TO)

class ShippingMethod(TimeStampedModel):
    name = models.CharField(max_length=50)
    cost = models.DecimalField(max_digits=6, decimal_places=2, default=0)




class Order(TimeStampedModel):
    STATUS = [
        ('pending', 'Pending'),
        ('confirmed', 'Confirmed'),
        ('shipped', 'Shipped'),
        ('delivered', 'Delivered'),
        ('cancelled', 'Cancelled'),
    ]

    customer = models.ForeignKey(User, on_delete=models.CASCADE, related_name='orders')
    tracking_number = models.CharField(max_length=100, null=True, blank=True)
    status = models.CharField(max_length=20, choices=STATUS, default='pending')
    coupon = models.ForeignKey(Coupon, on_delete=models.SET_NULL, null=True, blank=True)
    total_price = models.DecimalField(max_digits=10, decimal_places=2)
    discount_amount = models.DecimalField(max_digits=10, decimal_places=2, default=0)
    final_total = models.DecimalField(max_digits=10, decimal_places=2)
    shipping_method = models.ForeignKey(ShippingMethod, on_delete=models.SET_NULL, null=True)
    shipping_cost = models.DecimalField(max_digits=6, decimal_places=2)
    

class OrderItem(TimeStampedModel):
    order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name='items')
    product_variant = models.ForeignKey(ProductsVariants, on_delete=models.SET_NULL, null=True)
    # product_variant = models.PositiveIntegerField()  # Assuming ProductVariant is an integer ID for simplicity
    quantity = models.PositiveIntegerField()
    price_each = models.DecimalField(max_digits=10, decimal_places=2)
    subtotal = models.DecimalField(max_digits=10, decimal_places=2)

class OrderPayment(TimeStampedModel):
    PAYMENT_STATUS = [
        ('paid', 'Paid'),
        ('failed', 'Failed'),
        ('refunded', 'Refunded'),
    ]

    order = models.OneToOneField(Order, on_delete=models.CASCADE, related_name='payments')
    payment_method = models.CharField(max_length=50)
    status = models.CharField(max_length=20, choices=PAYMENT_STATUS)
    total_amount = models.DecimalField(max_digits=10, decimal_places=2)
    paid_at = models.DateTimeField(null=True, blank=True)

class ShippingAddress(TimeStampedModel):
    order = models.OneToOneField(Order, on_delete=models.CASCADE, related_name='shipping_address')
    recipient_name = models.CharField(max_length=100)
    company_name = models.CharField(max_length=100, blank=True, null=True)
    appartment_number = models.CharField(max_length=50, blank=True, null=True)
    street_address = models.CharField(max_length=255)
    city = models.CharField(max_length=100)
    state = models.CharField(max_length=100)
    postal_code = models.CharField(max_length=20, blank=True, null=True)
    country = models.CharField(max_length=100)
    phone_number = models.CharField(max_length=15)
    email = models.EmailField()
    is_check_save_info_faster_checkout = models.BooleanField(default=False)