<?php

namespace App\Sms\Repository;

use App\Sms\Entity\SmsWallet;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class SmsWalletRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, SmsWallet::class);
    }

    public function findByEntity(string $entityType, int $entityId): ?SmsWallet
    {
        return $this->findOneBy(['entityType' => $entityType, 'entityId' => $entityId]);
    }

    public function getBalance(string $entityType, int $entityId): int
    {
        $wallet = $this->findByEntity($entityType, $entityId);
        return $wallet?->getBalanceRials() ?? 0;
    }

    public function save(SmsWallet $wallet): void
    {
        $this->getEntityManager()->persist($wallet);
        $this->getEntityManager()->flush();
    }
}
