<?php

/*
 * This file is part of the Serenity package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Serenity\CoreBundle\Slug\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Serenity\CoreBundle\Shared\Aggregate\AggregateRoot;
use Serenity\CoreBundle\Shared\Aggregate\AggregateRootInterface;
use Serenity\CoreBundle\Shared\Common\IdentifierTrait;
use Serenity\CoreBundle\Shared\Common\TimestampTrait;
use Serenity\CoreBundle\Shared\Feature\SerenitySaasInterface;
use Serenity\CoreBundle\Shared\Feature\SerenitySaasTrait;

#[ORM\Entity]
#[ORM\Table(name: 'serenity_core_slug_history')]
#[ORM\UniqueConstraint(name: 'uniq_slug_history', columns: ['saasidentifier', 'entityclass', 'oldslug'])]
#[ORM\Index(name: 'idx_slug_history_lookup', columns: ['saasidentifier', 'entityclass', 'oldslug'])]
#[ORM\HasLifecycleCallbacks]
class SlugHistory implements AggregateRootInterface, SerenitySaasInterface
{
    use AggregateRoot;
    use IdentifierTrait {
        IdentifierTrait::__construct as private identifierConstruct;
    }
    use SerenitySaasTrait;
    use TimestampTrait;

    #[ORM\Column(length: 255)]
    public private(set) string $entityClass;

    #[ORM\Column(length: 255)]
    public private(set) string $oldSlug;

    #[ORM\Column(type: 'string', length: 40)]
    public private(set) string $entityId;

    #[ORM\Column(type: Types::SMALLINT, options: ['default' => 301])]
    public private(set) int $code = 301;

    private function __construct()
    {
    }

    public static function record(string $oldSlug, string $entityClass, string $entityId, int $code = 301): self
    {
        $self = new self();
        $self->oldSlug = $oldSlug;
        $self->entityClass = $entityClass;
        $self->entityId = $entityId;
        $self->code = $code;

        return $self;
    }
}
