<?php

namespace WordpressModels\ORM\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;

/**
 * Entity mapping for the WordPress usermeta table.
 *
 * This entity uses {@see AbstractMeta} as a base class for generic meta table mapping.
 *
 * The {@see self::$id} property is overridden to use the `umeta_id` column name.
 */
#[
    ORM\Table('usermeta'),
    ORM\Index(['meta_key'], name: 'meta_key'),
    ORM\Entity,
    ORM\Cache(usage: 'NONSTRICT_READ_WRITE', region: 'wp_usermeta'),
]
class UserMeta extends AbstractMeta
{

    /**
     * Override the default column name for.
     *
     * @var int|null
     */
    #[ORM\Column(name: 'umeta_id', type: Types::BIGINT, options: ['unsigned' => true])]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'IDENTITY')]
    #[Groups(['read_meta'])]
    protected ?int $id = null;

    #[
        ORM\ManyToOne(targetEntity: User::class, inversedBy: 'meta'),
        ORM\JoinColumn('user_id', referencedColumnName: 'ID')
    ]
    #[Groups(['read_meta_details'])]
    private User $user;

    public function getUser(): User
    {
        return $this->user;
    }

    public function setUser(User $user): UserMeta
    {
        $this->user = $user;
        return $this;
    }

    public static function getType(): string
    {
        return 'user';
    }

}
