from django.db import models
from django.urls import reverse
from django.contrib.auth.models import AbstractUser
from localflavor.us.us_states import USPS_CHOICES


class Address(models.Model):
    first_name = models.CharField(max_length=256, blank=True)
    last_name = models.CharField(max_length=256, blank=True)
    street_address_1 = models.CharField(max_length=256, blank=True)
    street_address_2 = models.CharField(max_length=256, blank=True)
    city = models.CharField(max_length=256, blank=True)
    state = models.CharField(
        max_length=2,
        choices=USPS_CHOICES,
        blank=True
    )
    postal_code = models.CharField(max_length=20, blank=True)

    def __str__(self):
        return f'{self.street_address_1} — {self.city}'


class User(AbstractUser):
    addresses = models.ManyToManyField(
        Address, blank=True, related_name="user_addresses"
    )
    default_shipping_address = models.ForeignKey(
        Address, related_name="+", null=True, blank=True, on_delete=models.SET_NULL
    )
    default_billing_address = models.ForeignKey(
        Address, related_name="+", null=True, blank=True, on_delete=models.SET_NULL
    )
    stripe_id = models.CharField(max_length=255, blank=True)
