<?php

namespace App\Sms\Entity;

use App\Sms\Repository\SmsSettingsRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: SmsSettingsRepository::class)]
#[ORM\Table(name: 'sms_settings')]
#[ORM\UniqueConstraint(name: 'uniq_sms_settings_entity', columns: ['entity_type', 'entity_id'])]
class SmsSettings
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

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

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

    #[ORM\Column(name: 'reminder_enabled', type: 'boolean')]
    private bool $reminderEnabled = false;

    #[ORM\Column(name: 'reminder_hours_before', type: 'smallint')]
    private int $reminderHoursBefore = 2;

    #[ORM\Column(name: 'post_visit_enabled', type: 'boolean')]
    private bool $postVisitEnabled = false;

    #[ORM\Column(name: 'post_visit_text', type: 'text', nullable: true)]
    private ?string $postVisitText = null;

    #[ORM\Column(name: 'post_visit_text_pending', type: 'text', nullable: true)]
    private ?string $postVisitTextPending = null;

    #[ORM\Column(name: 'post_visit_text_status', type: 'string', length: 20)]
    private string $postVisitTextStatus = 'none';

    #[ORM\Column(name: 'post_visit_text_reject_reason', type: 'text', nullable: true)]
    private ?string $postVisitTextRejectReason = null;

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

    public const TEXT_STATUS_NONE     = 'none';
    public const TEXT_STATUS_PENDING  = 'pending';
    public const TEXT_STATUS_APPROVED = 'approved';
    public const TEXT_STATUS_REJECTED = 'rejected';

    public function __construct(string $entityType, int $entityId)
    {
        $this->entityType = $entityType;
        $this->entityId   = $entityId;
        $this->updatedAt  = time();
    }

    public function getId(): ?int           { return $this->id; }
    public function getEntityType(): string { return $this->entityType; }
    public function getEntityId(): int      { return $this->entityId; }
    public function isReminderEnabled(): bool  { return $this->reminderEnabled; }
    public function getReminderHoursBefore(): int { return $this->reminderHoursBefore; }
    public function isPostVisitEnabled(): bool { return $this->postVisitEnabled; }
    public function getPostVisitText(): ?string { return $this->postVisitText; }

    public function getPostVisitTextPending(): ?string     { return $this->postVisitTextPending; }
    public function getPostVisitTextStatus(): string       { return $this->postVisitTextStatus; }
    public function getPostVisitTextRejectReason(): ?string { return $this->postVisitTextRejectReason; }

    public function setReminderEnabled(bool $v): self       { $this->reminderEnabled = $v; $this->updatedAt = time(); return $this; }
    public function setReminderHoursBefore(int $v): self    { $this->reminderHoursBefore = $v; $this->updatedAt = time(); return $this; }
    public function setPostVisitEnabled(bool $v): self      { $this->postVisitEnabled = $v; $this->updatedAt = time(); return $this; }
    public function setPostVisitText(?string $v): self      { $this->postVisitText = $v; $this->updatedAt = time(); return $this; }

    public function submitPostVisitText(string $text): self
    {
        $this->postVisitTextPending      = $text;
        $this->postVisitTextStatus       = self::TEXT_STATUS_PENDING;
        $this->postVisitTextRejectReason = null;
        $this->updatedAt                 = time();
        return $this;
    }

    public function approvePostVisitText(): self
    {
        if ($this->postVisitTextPending !== null) {
            $this->postVisitText = $this->postVisitTextPending;
        }
        $this->postVisitTextPending = null;
        $this->postVisitTextStatus  = self::TEXT_STATUS_APPROVED;
        $this->updatedAt            = time();
        return $this;
    }

    public function rejectPostVisitText(string $reason): self
    {
        $this->postVisitTextStatus       = self::TEXT_STATUS_REJECTED;
        $this->postVisitTextRejectReason = $reason;
        $this->updatedAt                 = time();
        return $this;
    }

    public function toArray(): array
    {
        return [
            'id'                            => $this->id,
            'entity_type'                   => $this->entityType,
            'entity_id'                     => $this->entityId,
            'reminder_enabled'              => $this->reminderEnabled,
            'reminder_hours_before'         => $this->reminderHoursBefore,
            'post_visit_enabled'            => $this->postVisitEnabled,
            'post_visit_text'               => $this->postVisitText,
            'post_visit_text_pending'       => $this->postVisitTextPending,
            'post_visit_text_status'        => $this->postVisitTextStatus,
            'post_visit_text_reject_reason' => $this->postVisitTextRejectReason,
            'updated_at'                    => $this->updatedAt,
        ];
    }
}
