import stripe
from environment import Environment


class StripeService:
    stripe.api_key = Environment.STRIPE_SECRET_KEY

    @staticmethod
    def create_payment(amount, email, brand_name):
        # Search for existing customer by email
        existing_customers = stripe.Customer.list(email=email).data

        if existing_customers:
            # Use the existing customer
            customer_id = existing_customers[0].id
        else:
            # Create a new customer if not found
            customer = stripe.Customer.create(email=email)
            customer_id = customer.id

        # Create a payment intent and associate it with the customer
        payment_intent = stripe.PaymentIntent.create(
            amount=amount,
            currency="usd",
            description=f"Payment for {brand_name}",
            customer=customer_id  # Associate the customer with the payment
        )
        return payment_intent

    # 2024-03-29 Evince_dev_python
    # create client payment method

    @staticmethod
    def create_payment_method(email, payment_method):
        # Search for existing customer by email
        existing_customers = stripe.Customer.list(email=email).data

        if existing_customers:
            # Use the existing customer
            customer_id = existing_customers[0].id
        else:
            # Create a new customer if not found
            customer = stripe.Customer.create(email=email)
            customer_id = customer.id

        # associate payment method  with the customer
        client_payment_method = stripe.PaymentMethod.attach(
            payment_method,
            customer=customer_id,
        )
        return client_payment_method

    # 2024-03-29 Evince_dev_python
    # update client payment method

    @staticmethod
    def delete_payment_method(email, payment_method):
        # Search for existing customer by email
        existing_customers = stripe.Customer.list(email=email).data

        if not existing_customers:
            return "Customer not found."

        # Use the existing customer
        customer_id = existing_customers[0].id

        # Detach the existing payment method
        client_payment_method = stripe.PaymentMethod.detach(payment_method)

        return client_payment_method

    # 2024-03-29 Evince_dev_python
    # Get list of client payment methods

    @staticmethod
    def get_payment_method(email, per_page):
        # Search for existing customer by email
        existing_customers = stripe.Customer.list(email=email).data

        if not existing_customers:
            return "Customer not found."

        # Use the existing customer
        customer_id = existing_customers[0].id

        # list of clients Payment Methods
        client_payment_methods = stripe.PaymentMethod.list(
            type="card",
            limit=per_page,
            customer=customer_id,
        )
        return client_payment_methods

    # 2024-03-29 Evince_dev_python
    # Get list of client payment invoices

    @staticmethod
    def get_payment_invoices(email, per_page):
        # Search for existing customer by email
        existing_customers = stripe.Customer.list(email=email).data

        if not existing_customers:
            return "Customer not found."

        # Use the existing customer
        customer_id = existing_customers[0].id

        # list of clients Payment Invoices
        invoices = stripe.Invoice.list(customer=customer_id)

        return invoices
