<?php

declare(strict_types=1);

namespace MauticPlugin\DealFlowBundle\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
use Mautic\CoreBundle\Entity\CommonEntity;

class DealAuditEvent extends CommonEntity
{
    public const TABLE_NAME = 'dealflow_deal_audit_events';

    private ?int $id = null;

    private ?Deal $deal = null;

    private string $eventType = '';

    private string $origin = 'ui';

    private ?string $actorType = null;

    private ?int $actorId = null;

    /**
     * @var array<string, mixed>
     */
    private array $payload = [];

    private \DateTimeInterface $createdAt;

    public function __construct()
    {
        $this->createdAt = new \DateTimeImmutable();
    }

    /** @param ORM\ClassMetadata<static> $metadata */
    public static function loadMetadata(ORM\ClassMetadata $metadata): void
    {
        $builder = new ClassMetadataBuilder($metadata);

        $builder->setTable(self::TABLE_NAME)
            ->setCustomRepositoryClass(DealAuditEventRepository::class)
            ->addIndex(['event_type'], 'dealflow_audit_event_type_idx');

        $builder->addId();

        $builder->createManyToOne('deal', Deal::class)
            ->inversedBy('auditEvents')
            ->addJoinColumn('deal_id', 'id', false, false, 'CASCADE')
            ->build();

        $builder->createField('eventType', Types::STRING)
            ->columnName('event_type')
            ->length(191)
            ->build();

        $builder->createField('origin', Types::STRING)
            ->length(32)
            ->build();

        $builder->createField('actorType', Types::STRING)
            ->columnName('actor_type')
            ->length(32)
            ->nullable()
            ->build();

        $builder->createField('actorId', Types::INTEGER)
            ->columnName('actor_id')
            ->nullable()
            ->build();

        $builder->createField('payload', Types::JSON)
            ->columnName('payload_json')
            ->build();

        $builder->createField('createdAt', 'datetime_immutable')
            ->columnName('created_at')
            ->build();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getDeal(): ?Deal
    {
        return $this->deal;
    }

    public function setDeal(Deal $deal): self
    {
        $this->deal = $deal;

        return $this;
    }

    public function getEventType(): string
    {
        return $this->eventType;
    }

    public function setEventType(string $eventType): self
    {
        $this->eventType = $eventType;

        return $this;
    }

    public function getOrigin(): string
    {
        return $this->origin;
    }

    public function setOrigin(string $origin): self
    {
        $this->origin = $origin;

        return $this;
    }

    public function getActorType(): ?string
    {
        return $this->actorType;
    }

    public function setActorType(?string $actorType): self
    {
        $this->actorType = $actorType;

        return $this;
    }

    public function getActorId(): ?int
    {
        return $this->actorId;
    }

    public function setActorId(?int $actorId): self
    {
        $this->actorId = $actorId;

        return $this;
    }

    /**
     * @return array<string, mixed>
     */
    public function getPayload(): array
    {
        return $this->payload;
    }

    /**
     * @param array<string, mixed> $payload
     */
    public function setPayload(array $payload): self
    {
        $this->payload = $payload;

        return $this;
    }

    public function getCreatedAt(): \DateTimeInterface
    {
        return $this->createdAt;
    }
}
