<?php

namespace App\Sms\Entity;

use App\Payment\Entity\Payment;
use App\Sms\Repository\SmsWalletTransactionRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: SmsWalletTransactionRepository::class)]
#[ORM\Table(name: 'sms_wallet_transactions')]
class SmsWalletTransaction
{
    public const TYPE_CREDIT = 'credit';
    public const TYPE_DEBIT  = 'debit';

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[ORM\Column(type: 'string', length: 36, unique: true)]
    private string $uuid;

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

    #[ORM\Column(type: 'string', length: 10)]
    private string $type;

    #[ORM\Column(name: 'amount_rials', type: 'integer')]
    private int $amountRials;

    #[ORM\Column(type: 'string', length: 255, nullable: true)]
    private ?string $description = null;

    #[ORM\ManyToOne(targetEntity: Payment::class)]
    #[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
    private ?Payment $payment = null;

    #[ORM\Column(name: 'created_at', type: 'integer')]
    private int $createdAt;

    public function __construct(SmsWallet $wallet, string $type, int $amountRials, ?string $description = null, ?Payment $payment = null)
    {
        $this->uuid        = Uuid::v4()->toRfc4122();
        $this->wallet      = $wallet;
        $this->type        = $type;
        $this->amountRials = $amountRials;
        $this->description = $description;
        $this->payment     = $payment;
        $this->createdAt   = time();
    }

    public function getId(): ?int         { return $this->id; }
    public function getUuid(): string     { return $this->uuid; }
    public function getType(): string     { return $this->type; }
    public function getAmountRials(): int { return $this->amountRials; }
    public function getDescription(): ?string { return $this->description; }
    public function getCreatedAt(): int   { return $this->createdAt; }

    public function toArray(): array
    {
        return [
            'uuid'         => $this->uuid,
            'type'         => $this->type,
            'amount_rials' => $this->amountRials,
            'description'  => $this->description,
            'created_at'   => $this->createdAt,
        ];
    }
}
