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

    private ?int $id = null;

    private ?Deal $deal = null;

    private int $contactId = 0;

    private ?string $label = null;

    private ?int $createdBy = 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(DealContactAssociationRepository::class)
            ->addIndex(['contact_id'], 'dealflow_contact_assoc_contact_idx')
            ->addUniqueConstraint(['deal_id', 'contact_id'], 'dealflow_contact_assoc_unique');

        $builder->addId();

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

        $builder->createField('contactId', Types::INTEGER)
            ->columnName('contact_id')
            ->build();

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

        $builder->createField('createdBy', Types::INTEGER)
            ->columnName('created_by')
            ->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 getContactId(): int
    {
        return $this->contactId;
    }

    public function setContactId(int $contactId): self
    {
        $this->isChanged('contactId', $contactId);
        $this->contactId = $contactId;

        return $this;
    }

    public function getLabel(): ?string
    {
        return $this->label;
    }

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

        return $this;
    }

    public function getCreatedBy(): ?int
    {
        return $this->createdBy;
    }

    public function setCreatedBy(?int $createdBy): self
    {
        $this->createdBy = $createdBy;

        return $this;
    }

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