<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups; // Add this use statement
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
#[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(["chat:channel:list", "chat_message"])] // Add group
private ?int $id = null;
#[ORM\Column(length: 180, unique: true)]
private ?string $email = null;
#[ORM\Column]
private array $roles = [];
/**
* @var string The hashed password
*/
#[ORM\Column]
private ?string $password = null;
#[ORM\Column(length: 255)]
#[Groups(["chat:channel:list", "chat_message"])] // Add group
private ?string $firstName = null;
#[ORM\Column(length: 255)]
#[Groups(["chat:channel:list", "chat_message"])] // Add group
private ?string $lastName = null;
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\ManyToOne(inversedBy: 'users')]
private ?Company $company = null;
#[ORM\OneToOne(mappedBy: 'user', cascade: ['persist', 'remove'])]
#[Groups(["chat_message"])] // Add serialization group to expose profile
private ?UserProfile $profile = null;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: UserTeam::class, orphanRemoval: true)]
private Collection $userTeams;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Submission::class)]
private Collection $submissions;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: ChatMessage::class)]
private Collection $chatMessages;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Notification::class)]
private Collection $notifications;
public function __toString(): string
{
return $this->getFullName();
}
public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
$this->userTeams = new ArrayCollection();
$this->submissions = new ArrayCollection();
$this->chatMessages = new ArrayCollection();
$this->notifications = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): static
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): static
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): static
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): static
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): static
{
$this->lastName = $lastName;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
#[Groups(["chat:channel:list", "chat_message"])] // Add group to the getter for fullName
public function getFullName(): string
{
return $this->firstName . ' ' . $this->lastName;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): static
{
$this->company = $company;
return $this;
}
public function getProfile(): ?UserProfile
{
return $this->profile;
}
public function setProfile(UserProfile $profile): static
{
// set the owning side of the relation if necessary
if ($profile->getUser() !== $this) {
$profile->setUser($this);
}
$this->profile = $profile;
return $this;
}
/**
* @return Collection<int, UserTeam>
*/
public function getUserTeams(): Collection
{
return $this->userTeams;
}
public function addUserTeam(UserTeam $userTeam): static
{
if (!$this->userTeams->contains($userTeam)) {
$this->userTeams->add($userTeam);
$userTeam->setUser($this);
}
return $this;
}
public function removeUserTeam(UserTeam $userTeam): static
{
if ($this->userTeams->removeElement($userTeam)) {
// set the owning side to null (unless already changed)
if ($userTeam->getUser() === $this) {
$userTeam->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Submission>
*/
public function getSubmissions(): Collection
{
return $this->submissions;
}
public function addSubmission(Submission $submission): static
{
if (!$this->submissions->contains($submission)) {
$this->submissions->add($submission);
$submission->setUser($this);
}
return $this;
}
public function removeSubmission(Submission $submission): static
{
if ($this->submissions->removeElement($submission)) {
// set the owning side to null (unless already changed)
if ($submission->getUser() === $this) {
$submission->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, ChatMessage>
*/
public function getChatMessages(): Collection
{
return $this->chatMessages;
}
public function addChatMessage(ChatMessage $chatMessage): static
{
if (!$this->chatMessages->contains($chatMessage)) {
$this->chatMessages->add($chatMessage);
$chatMessage->setUser($this);
}
return $this;
}
public function removeChatMessage(ChatMessage $chatMessage): static
{
if ($this->chatMessages->removeElement($chatMessage)) {
// set the owning side to null (unless already changed)
if ($chatMessage->getUser() === $this) {
$chatMessage->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Notification>
*/
public function getNotifications(): Collection
{
return $this->notifications;
}
public function addNotification(Notification $notification): static
{
if (!$this->notifications->contains($notification)) {
$this->notifications->add($notification);
$notification->setUser($this);
}
return $this;
}
public function removeNotification(Notification $notification): static
{
if ($this->notifications->removeElement($notification)) {
// set the owning side to null (unless already changed)
if ($notification->getUser() === $this) {
$notification->setUser(null);
}
}
return $this;
}
/**
* Get all teams the user is a member of
*
* @return Collection<int, Team>
*/
public function getTeams(): Collection
{
$teams = new ArrayCollection();
foreach ($this->userTeams as $userTeam) {
$teams->add($userTeam->getTeam());
}
return $teams;
}
/**
* Check if user is a member of a specific team
*/
public function isMemberOfTeam(Team $team): bool
{
foreach ($this->userTeams as $userTeam) {
if ($userTeam->getTeam() === $team) {
return true;
}
}
return false;
}
/**
* Check if user is a leader of a specific team
*/
public function isLeaderOfTeam(Team $team): bool
{
foreach ($this->userTeams as $userTeam) {
if ($userTeam->getTeam() === $team && $userTeam->getRole() === 'leader') {
return true;
}
}
return false;
}
/**
* Get the user's primary team (first team found)
*/
public function getUserTeam(): ?UserTeam
{
if ($this->userTeams->isEmpty()) {
return null;
}
return $this->userTeams->first();
}
/**
* Get the user's primary team entity
*/
public function getTeam(): ?Team
{
$userTeam = $this->getUserTeam();
return $userTeam ? $userTeam->getTeam() : null;
}
/**
* Get total steps for a specific season
*/
public function getTotalStepsForSeason(Season $season): int
{
$totalSteps = 0;
foreach ($this->submissions as $submission) {
// Only count approved submissions for this season
if ($submission->getSeason() === $season &&
$submission->getStatus() === 'approved' &&
$submission->getActivityData() !== null) {
$activityData = $submission->getActivityData();
if (isset($activityData['steps'])) {
$totalSteps += (int) $activityData['steps'];
}
}
}
return $totalSteps;
}
/**
* Get lifetime total steps across all seasons
*/
public function getLifetimeTotalSteps(): int
{
$totalSteps = 0;
foreach ($this->submissions as $submission) {
// Only count approved submissions
if ($submission->getStatus() === 'approved' &&
$submission->getActivityData() !== null) {
$activityData = $submission->getActivityData();
if (isset($activityData['steps'])) {
$totalSteps += (int) $activityData['steps'];
}
}
}
return $totalSteps;
}
}