<?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 DealNote extends CommonEntity
{
    public const TABLE_NAME = 'dealflow_deal_notes';

    private ?int $id = null;

    private ?Deal $deal = null;

    private string $body = '';

    private string $sourceType = 'manual';

    private ?string $sourceRef = null;

    private string $activityType = 'note';

    private ?int $authorUserId = null;

    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(DealNoteRepository::class);

        $builder->addId();

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

        $builder->createField('body', Types::TEXT)
            ->build();

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

        $builder->createField('sourceRef', Types::STRING)
            ->columnName('source_ref')
            ->length(191)
            ->nullable()
            ->build();

        $builder->createField('activityType', Types::STRING)
            ->columnName('activity_type')
            ->length(20)
            ->build();

        $builder->createField('authorUserId', Types::INTEGER)
            ->columnName('author_user_id')
            ->nullable()
            ->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 getBody(): string
    {
        return $this->body;
    }

    public function setBody(string $body): self
    {
        $this->isChanged('body', $body);
        $this->body = $body;

        return $this;
    }

    public function getSourceType(): string
    {
        return $this->sourceType;
    }

    public function setSourceType(string $sourceType): self
    {
        $this->isChanged('sourceType', $sourceType);
        $this->sourceType = $sourceType;

        return $this;
    }

    public function getSourceRef(): ?string
    {
        return $this->sourceRef;
    }

    public function setSourceRef(?string $sourceRef): self
    {
        $this->isChanged('sourceRef', $sourceRef);
        $this->sourceRef = $sourceRef;

        return $this;
    }

    public function getActivityType(): string
    {
        return $this->activityType;
    }

    public function setActivityType(string $activityType): self
    {
        $this->isChanged('activityType', $activityType);
        $this->activityType = $activityType;

        return $this;
    }

    public function getAuthorUserId(): ?int
    {
        return $this->authorUserId;
    }

    public function setAuthorUserId(?int $authorUserId): self
    {
        $this->isChanged('authorUserId', $authorUserId);
        $this->authorUserId = $authorUserId;

        return $this;
    }

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