<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Index(columns: ['customer_id', 'is_default'], name: 'idx_customer_payment_method_default')]
class CustomerPaymentMethod
{
    public const TYPE_SEPA = 'sepa_debit';
    public const TYPE_CARD = 'card';

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: Customer::class)]
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
    private Customer $customer;

    #[ORM\Column(length: 255)]
    private string $stripePaymentMethodId;

    #[ORM\Column(length: 20)]
    private string $type;

    #[ORM\Column(length: 4, nullable: true)]
    private ?string $last4 = null;

    #[ORM\Column(length: 50, nullable: true)]
    private ?string $brand = null;

    #[ORM\Column(length: 2, nullable: true)]
    private ?string $country = null;

    #[ORM\Column]
    private bool $isDefault = false;

    #[ORM\Column]
    private \DateTimeImmutable $createdAt;

    public function __construct(Customer $customer, string $stripePaymentMethodId, string $type)
    {
        $this->customer = $customer;
        $this->stripePaymentMethodId = $stripePaymentMethodId;
        $this->type = $type;
        $this->createdAt = new \DateTimeImmutable();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getCustomer(): Customer
    {
        return $this->customer;
    }

    public function getStripePaymentMethodId(): string
    {
        return $this->stripePaymentMethodId;
    }

    public function getType(): string
    {
        return $this->type;
    }

    public function getTypeLabel(): string
    {
        return match ($this->type) {
            self::TYPE_SEPA => 'Prelevement SEPA',
            self::TYPE_CARD => 'Carte bancaire',
            default => $this->type,
        };
    }

    public function getLast4(): ?string
    {
        return $this->last4;
    }

    public function setLast4(?string $last4): static
    {
        $this->last4 = $last4;

        return $this;
    }

    public function getBrand(): ?string
    {
        return $this->brand;
    }

    public function setBrand(?string $brand): static
    {
        $this->brand = $brand;

        return $this;
    }

    public function getCountry(): ?string
    {
        return $this->country;
    }

    public function setCountry(?string $country): static
    {
        $this->country = $country;

        return $this;
    }

    public function isDefault(): bool
    {
        return $this->isDefault;
    }

    public function setIsDefault(bool $isDefault): static
    {
        $this->isDefault = $isDefault;

        return $this;
    }

    public function getCreatedAt(): \DateTimeImmutable
    {
        return $this->createdAt;
    }

    public function getDisplayLabel(): string
    {
        $label = $this->getTypeLabel();
        if (null !== $this->last4) {
            $label .= ' **** '.$this->last4;
        }
        if (null !== $this->brand) {
            $label .= ' ('.$this->brand.')';
        }

        return $label;
    }
}
