<?php

namespace Mautic\PageBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver;
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
use Mautic\CoreBundle\Entity\FormEntity;

class Redirect extends FormEntity
{
    /**
     * @var string
     */
    private $id;

    /**
     * @var string
     */
    private $redirectId;

    private $url;

    /**
     * @var int
     */
    private $hits = 0;

    /**
     * @var int
     */
    private $uniqueHits = 0;

    /**
     * @var ArrayCollection<int, Trackable>
     */
    private $trackables;

    public function __construct()
    {
        $this->trackables = new ArrayCollection();
    }

    public static function loadMetadata(ORM\ClassMetadata $metadata): void
    {
        $builder = new ClassMetadataBuilder($metadata);

        $builder->setTable('page_redirects')
            ->setCustomRepositoryClass(RedirectRepository::class);

        $builder->addBigIntIdField();

        $builder->createField('redirectId', 'string')
            ->columnName('redirect_id')
            ->length(25)
            ->build();

        $builder->addField('url', 'text');

        $builder->addField('hits', 'integer');

        $builder->createField('uniqueHits', 'integer')
            ->columnName('unique_hits')
            ->build();

        $builder->createOneToMany('trackables', 'Trackable')
            ->mappedBy('redirect')
            ->fetchExtraLazy()
            ->build();
    }

    /**
     * Prepares the metadata for API usage.
     */
    public static function loadApiMetadata(ApiMetadataDriver $metadata): void
    {
        $metadata->setGroupPrefix('redirect')
            ->addListProperties(
                [
                    'id',
                    'redirectId',
                    'url',
                ]
            )
            ->addProperties(
                [
                    'hits',
                    'uniqueHits',
                ]
            )
            ->build();
    }

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

    /**
     * @return string
     */
    public function getRedirectId()
    {
        return $this->redirectId;
    }

    /**
     * @param string $redirectId
     */
    public function setRedirectId($redirectId = null): void
    {
        if (null === $redirectId) {
            $redirectId = substr(hash('sha1', uniqid(mt_rand())), 0, 25);
        }
        $this->redirectId = $redirectId;
    }

    public function getUrl(): string
    {
        return trim($this->url);
    }

    /**
     * @param string $url
     */
    public function setUrl($url): void
    {
        $this->url = trim($url);
    }

    /**
     * Set hits.
     *
     * @param int $hits
     */
    public function setHits($hits): Redirect
    {
        $this->hits = $hits;

        return $this;
    }

    /**
     * Get hits.
     *
     * @return int
     */
    public function getHits()
    {
        return $this->hits;
    }

    /**
     * Set uniqueHits.
     *
     * @param int $uniqueHits
     */
    public function setUniqueHits($uniqueHits): Redirect
    {
        $this->uniqueHits = $uniqueHits;

        return $this;
    }

    /**
     * Get uniqueHits.
     *
     * @return int
     */
    public function getUniqueHits()
    {
        return $this->uniqueHits;
    }

    /**
     * @return ArrayCollection
     */
    public function getTrackableList()
    {
        return $this->trackables;
    }

    /**
     * @param ArrayCollection $trackables
     *
     * @return Redirect
     */
    public function setTrackables($trackables)
    {
        $this->trackables = $trackables;

        return $this;
    }
}
