<?php

namespace App\Settlement\Entity;

use App\Auth\Entity\User;
use App\Payment\Entity\Payment;
use Doctrine\ORM\Mapping as ORM;
use App\Settlement\Repository\WalletTransactionRepository;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: WalletTransactionRepository::class)]
#[ORM\Table(name: 'wallet_transactions')]
#[ORM\Index(columns: ['user_id', 'created_at'], name: 'idx_wallet_user_date')]
#[ORM\Index(columns: ['user_id', 'type'], name: 'idx_wallet_user_type')]
class WalletTransaction
{
    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: User::class)]
    #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: false, onDelete: 'RESTRICT')]
    private User $user;

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

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

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

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

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

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

    public function __construct(User $user, int $amountRials, string $type, int $balanceAfter)
    {
        $this->uuid         = Uuid::v4()->toRfc4122();
        $this->user         = $user;
        $this->amountRials  = $amountRials;
        $this->type         = $type;
        $this->balanceAfter = $balanceAfter;
        $this->createdAt    = time();
    }

    public function getId(): ?int           { return $this->id; }
    public function getUuid(): string       { return $this->uuid; }
    public function getUser(): User         { return $this->user; }
    public function getPayment(): ?Payment  { return $this->payment; }
    public function getAmountRials(): int   { return $this->amountRials; }
    public function getType(): string       { return $this->type; }
    public function getDescription(): ?string { return $this->description; }
    public function getBalanceAfter(): int  { return $this->balanceAfter; }

    public function setPayment(?Payment $p): self      { $this->payment = $p; return $this; }
    public function setDescription(?string $d): self   { $this->description = $d; return $this; }

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