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

    private ?int $id = null;

    private ?Deal $deal = null;

    private string $storagePath = '';

    private string $originalFilename = '';

    private string $mimeType = '';

    private int $sizeBytes = 0;

    private ?int $uploadedBy = 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(DealAttachmentRepository::class);

        $builder->addId();

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

        $builder->createField('storagePath', Types::STRING)
            ->columnName('storage_path')
            ->length(255)
            ->build();

        $builder->createField('originalFilename', Types::STRING)
            ->columnName('original_filename')
            ->length(255)
            ->build();

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

        $builder->createField('sizeBytes', Types::INTEGER)
            ->columnName('size_bytes')
            ->build();

        $builder->createField('uploadedBy', Types::INTEGER)
            ->columnName('uploaded_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 getStoragePath(): string
    {
        return $this->storagePath;
    }

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

        return $this;
    }

    public function getOriginalFilename(): string
    {
        return $this->originalFilename;
    }

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

        return $this;
    }

    public function getMimeType(): string
    {
        return $this->mimeType;
    }

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

        return $this;
    }

    public function getSizeBytes(): int
    {
        return $this->sizeBytes;
    }

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

        return $this;
    }

    public function getUploadedBy(): ?int
    {
        return $this->uploadedBy;
    }

    public function setUploadedBy(?int $uploadedBy): self
    {
        $this->uploadedBy = $uploadedBy;

        return $this;
    }

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