<?php

declare(strict_types=1);

namespace App\Entity;

/**
 * User实体类，对应数据库中的users表。
 */
class User
{
    /**
     * 用户唯一标识符。
     * @var int|null
     */
    private ?int $id = null;

    /**
     * 用户名。
     * @var string
     */
    private string $name;

    /**
     * 电子邮箱地址。
     * @var string
     */
    private string $email;

    /**
     * 账户状态。
     * @var string
     */
    private string $status = 'active';

    /**
     * 记录创建时间。
     * @var \DateTimeImmutable
     */
    private \DateTimeImmutable $createdAt;

    /**
     * 最后更新时间。
     * @var \DateTimeImmutable
     */
    private \DateTimeImmutable $updatedAt;

    /**
     * 构造函数。
     *
     * @param string $name
     * @param string $email
     */
    public function __construct(string $name, string $email)
    {
        $this->name = $name;
        $this->email = $email;
        $this->createdAt = new \DateTimeImmutable();
        $this->updatedAt = new \DateTimeImmutable();
    }

    // region Getter 和 Setter 方法

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

    public function setId(int $id): void
    {
        $this->id = $id;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): void
    {
        $this->name = $name;
        $this->updatedAt = new \DateTimeImmutable();
    }

    public function getEmail(): string
    {
        return $this->email;
    }

    public function setEmail(string $email): void
    {
        $this->email = $email;
        $this->updatedAt = new \DateTimeImmutable();
    }

    public function getStatus(): string
    {
        return $this->status;
    }

    public function setStatus(string $status): void
    {
        $this->status = $status;
        $this->updatedAt = new \DateTimeImmutable();
    }

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

    public function getUpdatedAt(): \DateTimeImmutable
    {
        return $this->updatedAt;
    }

    // endregion

    /**
     * 检查用户是否为活跃状态。
     *
     * @return bool
     */
    public function isActive(): bool
    {
        return $this->status === 'active';
    }

    /**
     * 将实体转换为数组形式，常用于序列化或API返回。
     *
     * @return array
     */
    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'status' => $this->status,
            'createdAt' => $this->createdAt->format('Y-m-d H:i:s'),
            'updatedAt' => $this->updatedAt->format('Y-m-d H:i:s')
        ];
    }
}
