// File generated from our OpenAPI spec by Stainless.

package com.increase.api.models.cards

import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
import com.increase.api.core.jsonMapper
import java.time.OffsetDateTime
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

internal class CardTest {

    @Test
    fun create() {
        val card =
            Card.builder()
                .id("card_oubs0hwk5rn6knuecxg2")
                .accountId("account_in71c4amph0vgo2qllky")
                .authorizationControls(
                    Card.AuthorizationControls.builder()
                        .merchantAcceptorIdentifier(
                            Card.AuthorizationControls.MerchantAcceptorIdentifier.builder()
                                .addAllowed(
                                    Card.AuthorizationControls.MerchantAcceptorIdentifier.Allowed
                                        .builder()
                                        .identifier("identifier")
                                        .build()
                                )
                                .addBlocked(
                                    Card.AuthorizationControls.MerchantAcceptorIdentifier.Blocked
                                        .builder()
                                        .identifier("identifier")
                                        .build()
                                )
                                .build()
                        )
                        .merchantCategoryCode(
                            Card.AuthorizationControls.MerchantCategoryCode.builder()
                                .addAllowed(
                                    Card.AuthorizationControls.MerchantCategoryCode.Allowed
                                        .builder()
                                        .code("code")
                                        .build()
                                )
                                .addBlocked(
                                    Card.AuthorizationControls.MerchantCategoryCode.Blocked
                                        .builder()
                                        .code("5734")
                                        .build()
                                )
                                .build()
                        )
                        .merchantCountry(
                            Card.AuthorizationControls.MerchantCountry.builder()
                                .addAllowed(
                                    Card.AuthorizationControls.MerchantCountry.Allowed.builder()
                                        .country("country")
                                        .build()
                                )
                                .addBlocked(
                                    Card.AuthorizationControls.MerchantCountry.Blocked.builder()
                                        .country("country")
                                        .build()
                                )
                                .build()
                        )
                        .usage(
                            Card.AuthorizationControls.Usage.builder()
                                .category(Card.AuthorizationControls.Usage.Category.MULTI_USE)
                                .multiUse(
                                    Card.AuthorizationControls.Usage.MultiUse.builder()
                                        .addSpendingLimit(
                                            Card.AuthorizationControls.Usage.MultiUse.SpendingLimit
                                                .builder()
                                                .interval(
                                                    Card.AuthorizationControls.Usage.MultiUse
                                                        .SpendingLimit
                                                        .Interval
                                                        .PER_MONTH
                                                )
                                                .addMerchantCategoryCode(
                                                    Card.AuthorizationControls.Usage.MultiUse
                                                        .SpendingLimit
                                                        .MerchantCategoryCode
                                                        .builder()
                                                        .code("code")
                                                        .build()
                                                )
                                                .settlementAmount(100000L)
                                                .build()
                                        )
                                        .build()
                                )
                                .singleUse(
                                    Card.AuthorizationControls.Usage.SingleUse.builder()
                                        .settlementAmount(
                                            Card.AuthorizationControls.Usage.SingleUse
                                                .SettlementAmount
                                                .builder()
                                                .comparison(
                                                    Card.AuthorizationControls.Usage.SingleUse
                                                        .SettlementAmount
                                                        .Comparison
                                                        .EQUALS
                                                )
                                                .value(0L)
                                                .build()
                                        )
                                        .build()
                                )
                                .build()
                        )
                        .build()
                )
                .billingAddress(
                    Card.BillingAddress.builder()
                        .city("New York")
                        .line1("33 Liberty Street")
                        .line2(null)
                        .postalCode("10045")
                        .state("NY")
                        .build()
                )
                .bin("42424242")
                .createdAt(OffsetDateTime.parse("2020-01-31T23:59:59Z"))
                .description("Office Expenses")
                .digitalWallet(
                    Card.DigitalWallet.builder()
                        .digitalCardProfileId("digital_card_profile_s3puplu90f04xhcwkiga")
                        .email("user@example.com")
                        .phone("+16505046304")
                        .build()
                )
                .entityId(null)
                .expirationMonth(11L)
                .expirationYear(2028L)
                .idempotencyKey(null)
                .last4("4242")
                .status(Card.Status.ACTIVE)
                .type(Card.Type.CARD)
                .build()

        assertThat(card.id()).isEqualTo("card_oubs0hwk5rn6knuecxg2")
        assertThat(card.accountId()).isEqualTo("account_in71c4amph0vgo2qllky")
        assertThat(card.authorizationControls())
            .contains(
                Card.AuthorizationControls.builder()
                    .merchantAcceptorIdentifier(
                        Card.AuthorizationControls.MerchantAcceptorIdentifier.builder()
                            .addAllowed(
                                Card.AuthorizationControls.MerchantAcceptorIdentifier.Allowed
                                    .builder()
                                    .identifier("identifier")
                                    .build()
                            )
                            .addBlocked(
                                Card.AuthorizationControls.MerchantAcceptorIdentifier.Blocked
                                    .builder()
                                    .identifier("identifier")
                                    .build()
                            )
                            .build()
                    )
                    .merchantCategoryCode(
                        Card.AuthorizationControls.MerchantCategoryCode.builder()
                            .addAllowed(
                                Card.AuthorizationControls.MerchantCategoryCode.Allowed.builder()
                                    .code("code")
                                    .build()
                            )
                            .addBlocked(
                                Card.AuthorizationControls.MerchantCategoryCode.Blocked.builder()
                                    .code("5734")
                                    .build()
                            )
                            .build()
                    )
                    .merchantCountry(
                        Card.AuthorizationControls.MerchantCountry.builder()
                            .addAllowed(
                                Card.AuthorizationControls.MerchantCountry.Allowed.builder()
                                    .country("country")
                                    .build()
                            )
                            .addBlocked(
                                Card.AuthorizationControls.MerchantCountry.Blocked.builder()
                                    .country("country")
                                    .build()
                            )
                            .build()
                    )
                    .usage(
                        Card.AuthorizationControls.Usage.builder()
                            .category(Card.AuthorizationControls.Usage.Category.MULTI_USE)
                            .multiUse(
                                Card.AuthorizationControls.Usage.MultiUse.builder()
                                    .addSpendingLimit(
                                        Card.AuthorizationControls.Usage.MultiUse.SpendingLimit
                                            .builder()
                                            .interval(
                                                Card.AuthorizationControls.Usage.MultiUse
                                                    .SpendingLimit
                                                    .Interval
                                                    .PER_MONTH
                                            )
                                            .addMerchantCategoryCode(
                                                Card.AuthorizationControls.Usage.MultiUse
                                                    .SpendingLimit
                                                    .MerchantCategoryCode
                                                    .builder()
                                                    .code("code")
                                                    .build()
                                            )
                                            .settlementAmount(100000L)
                                            .build()
                                    )
                                    .build()
                            )
                            .singleUse(
                                Card.AuthorizationControls.Usage.SingleUse.builder()
                                    .settlementAmount(
                                        Card.AuthorizationControls.Usage.SingleUse.SettlementAmount
                                            .builder()
                                            .comparison(
                                                Card.AuthorizationControls.Usage.SingleUse
                                                    .SettlementAmount
                                                    .Comparison
                                                    .EQUALS
                                            )
                                            .value(0L)
                                            .build()
                                    )
                                    .build()
                            )
                            .build()
                    )
                    .build()
            )
        assertThat(card.billingAddress())
            .isEqualTo(
                Card.BillingAddress.builder()
                    .city("New York")
                    .line1("33 Liberty Street")
                    .line2(null)
                    .postalCode("10045")
                    .state("NY")
                    .build()
            )
        assertThat(card.bin()).isEqualTo("42424242")
        assertThat(card.createdAt()).isEqualTo(OffsetDateTime.parse("2020-01-31T23:59:59Z"))
        assertThat(card.description()).contains("Office Expenses")
        assertThat(card.digitalWallet())
            .contains(
                Card.DigitalWallet.builder()
                    .digitalCardProfileId("digital_card_profile_s3puplu90f04xhcwkiga")
                    .email("user@example.com")
                    .phone("+16505046304")
                    .build()
            )
        assertThat(card.entityId()).isEmpty
        assertThat(card.expirationMonth()).isEqualTo(11L)
        assertThat(card.expirationYear()).isEqualTo(2028L)
        assertThat(card.idempotencyKey()).isEmpty
        assertThat(card.last4()).isEqualTo("4242")
        assertThat(card.status()).isEqualTo(Card.Status.ACTIVE)
        assertThat(card.type()).isEqualTo(Card.Type.CARD)
    }

    @Test
    fun roundtrip() {
        val jsonMapper = jsonMapper()
        val card =
            Card.builder()
                .id("card_oubs0hwk5rn6knuecxg2")
                .accountId("account_in71c4amph0vgo2qllky")
                .authorizationControls(
                    Card.AuthorizationControls.builder()
                        .merchantAcceptorIdentifier(
                            Card.AuthorizationControls.MerchantAcceptorIdentifier.builder()
                                .addAllowed(
                                    Card.AuthorizationControls.MerchantAcceptorIdentifier.Allowed
                                        .builder()
                                        .identifier("identifier")
                                        .build()
                                )
                                .addBlocked(
                                    Card.AuthorizationControls.MerchantAcceptorIdentifier.Blocked
                                        .builder()
                                        .identifier("identifier")
                                        .build()
                                )
                                .build()
                        )
                        .merchantCategoryCode(
                            Card.AuthorizationControls.MerchantCategoryCode.builder()
                                .addAllowed(
                                    Card.AuthorizationControls.MerchantCategoryCode.Allowed
                                        .builder()
                                        .code("code")
                                        .build()
                                )
                                .addBlocked(
                                    Card.AuthorizationControls.MerchantCategoryCode.Blocked
                                        .builder()
                                        .code("5734")
                                        .build()
                                )
                                .build()
                        )
                        .merchantCountry(
                            Card.AuthorizationControls.MerchantCountry.builder()
                                .addAllowed(
                                    Card.AuthorizationControls.MerchantCountry.Allowed.builder()
                                        .country("country")
                                        .build()
                                )
                                .addBlocked(
                                    Card.AuthorizationControls.MerchantCountry.Blocked.builder()
                                        .country("country")
                                        .build()
                                )
                                .build()
                        )
                        .usage(
                            Card.AuthorizationControls.Usage.builder()
                                .category(Card.AuthorizationControls.Usage.Category.MULTI_USE)
                                .multiUse(
                                    Card.AuthorizationControls.Usage.MultiUse.builder()
                                        .addSpendingLimit(
                                            Card.AuthorizationControls.Usage.MultiUse.SpendingLimit
                                                .builder()
                                                .interval(
                                                    Card.AuthorizationControls.Usage.MultiUse
                                                        .SpendingLimit
                                                        .Interval
                                                        .PER_MONTH
                                                )
                                                .addMerchantCategoryCode(
                                                    Card.AuthorizationControls.Usage.MultiUse
                                                        .SpendingLimit
                                                        .MerchantCategoryCode
                                                        .builder()
                                                        .code("code")
                                                        .build()
                                                )
                                                .settlementAmount(100000L)
                                                .build()
                                        )
                                        .build()
                                )
                                .singleUse(
                                    Card.AuthorizationControls.Usage.SingleUse.builder()
                                        .settlementAmount(
                                            Card.AuthorizationControls.Usage.SingleUse
                                                .SettlementAmount
                                                .builder()
                                                .comparison(
                                                    Card.AuthorizationControls.Usage.SingleUse
                                                        .SettlementAmount
                                                        .Comparison
                                                        .EQUALS
                                                )
                                                .value(0L)
                                                .build()
                                        )
                                        .build()
                                )
                                .build()
                        )
                        .build()
                )
                .billingAddress(
                    Card.BillingAddress.builder()
                        .city("New York")
                        .line1("33 Liberty Street")
                        .line2(null)
                        .postalCode("10045")
                        .state("NY")
                        .build()
                )
                .bin("42424242")
                .createdAt(OffsetDateTime.parse("2020-01-31T23:59:59Z"))
                .description("Office Expenses")
                .digitalWallet(
                    Card.DigitalWallet.builder()
                        .digitalCardProfileId("digital_card_profile_s3puplu90f04xhcwkiga")
                        .email("user@example.com")
                        .phone("+16505046304")
                        .build()
                )
                .entityId(null)
                .expirationMonth(11L)
                .expirationYear(2028L)
                .idempotencyKey(null)
                .last4("4242")
                .status(Card.Status.ACTIVE)
                .type(Card.Type.CARD)
                .build()

        val roundtrippedCard =
            jsonMapper.readValue(jsonMapper.writeValueAsString(card), jacksonTypeRef<Card>())

        assertThat(roundtrippedCard).isEqualTo(card)
    }
}
