from django.db import models


class BillingAddress(models.Model):
    """
    Draft of a billing address for a purchase from the shop.
    """

    user = models.OneToOneField(
        "core.User",
        on_delete=models.CASCADE,
        primary_key=True,
    )

    # user
    first_name = models.CharField(max_length=255, blank=True)
    last_name = models.CharField(max_length=255, blank=True)
    street = models.CharField(max_length=255, blank=True)
    street_number = models.CharField(max_length=255, blank=True)
    postal_code = models.CharField(max_length=255, blank=True)
    city = models.CharField(max_length=255, blank=True)
    country = models.CharField(max_length=255, blank=True)

    # company (optional)
    company_name = models.CharField(max_length=255, blank=True)
    company_street = models.CharField(max_length=255, blank=True)
    company_street_number = models.CharField(max_length=255, blank=True)
    company_postal_code = models.CharField(max_length=255, blank=True)
    company_city = models.CharField(max_length=255, blank=True)
    company_country = models.CharField(max_length=255, blank=True)


class Product(models.Model):
    sku = models.CharField(max_length=255, primary_key=True)
    price = models.IntegerField()  # 10_00 = 10.00 CHF
    name = models.CharField(max_length=255)
    description = models.CharField(max_length=255)


class CheckoutState(models.TextChoices):
    """
    The state of a checkout process transaction.

    PAID: Datatrans transaction settled/transmitted.
    ONGOING: Any state that is not final (e.g. initialized, challenge_ongoing, etc.)

    1) We use the `autoSettle` feature of DataTrans!
    -> https://docs.datatrans.ch/docs/after-the-payment
    -> https://api-reference.datatrans.ch/#tag/v1transactions/operation/status

    2) Difference between `settled` and `transmitted`:
    - https://www.datatrans.ch/en/know-how/faq/#what-does-the-status-transaction-settled-or-settledtransmitted-mean

    3) Related code: init_transaction and get_transaction_state in shop/services.py
    """

    ONGOING = "ongoing"
    PAID = "paid"
    CANCELED = "canceled"
    FAILED = "failed"


class CheckoutInformation(models.Model):
    user = models.ForeignKey("core.User", on_delete=models.PROTECT)

    product_sku = models.CharField(max_length=255)
    product_name = models.CharField(max_length=255)
    product_description = models.CharField(max_length=255)
    product_price = models.IntegerField(
        help_text="The total price of the product in centimes -> 1000 = 10.00 CHF"
    )

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    state = models.CharField(
        max_length=50,
        choices=CheckoutState.choices,
    )

    invoice_transmitted_at = models.DateTimeField(blank=True, null=True)
    transaction_id = models.CharField(max_length=255)

    # end user (required)
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    street = models.CharField(max_length=255)
    street_number = models.CharField(max_length=255)
    postal_code = models.CharField(max_length=255)
    city = models.CharField(max_length=255)
    country = models.CharField(max_length=255)

    # company (optional)
    company_name = models.CharField(max_length=255, blank=True)
    company_street = models.CharField(max_length=255, blank=True)
    company_street_number = models.CharField(max_length=255, blank=True)
    company_postal_code = models.CharField(max_length=255, blank=True)
    company_city = models.CharField(max_length=255, blank=True)
    company_country = models.CharField(max_length=255, blank=True)

    # webhook metadata
    webhook_history = models.JSONField(default=list)
