<?php

namespace App\Sms\Entity;

use Doctrine\ORM\Mapping as ORM;
use App\Sms\Repository\SmsLogRepository;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: SmsLogRepository::class)]
#[ORM\Table(name: 'sms_logs')]
#[ORM\Index(columns: ['mobile', 'created_at'], name: 'idx_sms_logs_mobile')]
class SmsLog
{
    public const TAG_GLOBAL              = 'global';
    public const TAG_OTP                 = 'otp';
    public const TAG_PAYMENT             = 'payment';
    public const TAG_CLINIC_INVITATION   = 'clinic_invitation';
    public const TAG_PRE_REGISTRATION    = 'pre_registration';
    public const TAG_NOTIFICATION_MOBILE = 'notification_mobile';
    public const TAG_USER_TEMPLATE       = 'user_template';
    public const TAG_WELCOME             = 'welcome';
    public const TAG_SECRETARY           = 'secretary';
    public const TAG_DOCTOR_APPOINTMENT  = 'doctor_appointment';

    public const TAGS = [
        self::TAG_GLOBAL,
        self::TAG_OTP,
        self::TAG_PAYMENT,
        self::TAG_CLINIC_INVITATION,
        self::TAG_PRE_REGISTRATION,
        self::TAG_NOTIFICATION_MOBILE,
        self::TAG_USER_TEMPLATE,
        self::TAG_WELCOME,
        self::TAG_SECRETARY,
        self::TAG_DOCTOR_APPOINTMENT,
    ];

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

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

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

    #[ORM\Column(type: 'text')]
    private string $message;

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

    #[ORM\Column(type: 'boolean')]
    private bool $success;

    #[ORM\Column(name: 'template_uuid', type: 'string', length: 36, nullable: true)]
    private ?string $templateUuid = null;

    // نام تمپلت VerifyLookup کاوه‌نگار که این پیامک با آن ارسال شده (مثلاً clinicpro-otp)؛ null اگر ارسال نشده.
    #[ORM\Column(name: 'template_code', type: 'string', length: 100, nullable: true)]
    private ?string $templateCode = null;

    #[ORM\Column(type: 'string', length: 30, options: ['default' => self::TAG_GLOBAL])]
    private string $tag = self::TAG_GLOBAL;

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

    public function __construct(string $mobile, string $message, string $provider, bool $success, string $tag = self::TAG_GLOBAL)
    {
        $this->uuid      = Uuid::v4()->toRfc4122();
        $this->mobile    = $mobile;
        $this->message   = $message;
        $this->provider  = $provider;
        $this->success   = $success;
        $this->tag       = $tag;
        $this->createdAt = time();
    }

    public function setTemplateUuid(?string $v): self { $this->templateUuid = $v; return $this; }
    public function setTemplateCode(?string $v): self { $this->templateCode = $v; return $this; }
    public function getTemplateCode(): ?string { return $this->templateCode; }
    public function getTag(): string { return $this->tag; }
    public function setTag(string $v): self { $this->tag = $v; return $this; }

    public function toArray(): array
    {
        return [
            'uuid'          => $this->uuid,
            'mobile'        => $this->mobile,
            'message'       => $this->message,
            'provider'      => $this->provider,
            'success'       => $this->success,
            'template_uuid' => $this->templateUuid,
            'template_code' => $this->templateCode,
            'tag'           => $this->tag,
            'created_at'    => $this->createdAt,
        ];
    }
}
