<?php

declare(strict_types=1);

namespace App\Entity;

use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: 'user_trip')]
class UserTrip
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: Trip::class, inversedBy: 'userTrips')]
    #[ORM\JoinColumn(nullable: false)]
    private Trip $trip;

    #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'userTrips')]
    #[ORM\JoinColumn(nullable: false)]
    private User $user;

    #[ORM\Column(length: 255)]
    private string $captainName;

    #[ORM\Column]
    private DateTimeImmutable $startDate;

    #[ORM\Column]
    private DateTimeImmutable $endDate;

    #[ORM\Column]
    private DateTimeImmutable $createdAt;

    #[ORM\OneToMany(targetEntity: UserTripLocation::class, mappedBy: 'userTrip')]
    private Collection $tripLocations;

    #[ORM\OneToMany(targetEntity: UserTripWorkLog::class, mappedBy: 'userTrip')]
    private Collection $workLogs;

    public function __construct(
        Trip $trip,
        User $user,
        string $captainName,
        DateTimeImmutable $startDate,
        DateTimeImmutable $endDate
    ) {
        $this->trip = $trip;
        $this->user = $user;
        $this->captainName = $captainName;
        $this->startDate = $startDate;
        $this->endDate = $endDate;
        $this->createdAt = new DateTimeImmutable();
        $this->tripLocations = new ArrayCollection();
        $this->workLogs = new ArrayCollection();
    }

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

    public function getTrip(): Trip
    {
        return $this->trip;
    }

    public function setTrip(Trip $trip): self
    {
        $this->trip = $trip;
        return $this;
    }

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

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

    public function getCaptainName(): string
    {
        return $this->captainName;
    }

    public function setCaptainName(string $captainName): self
    {
        $this->captainName = $captainName;
        return $this;
    }

    public function getStartDate(): DateTimeImmutable
    {
        return $this->startDate;
    }

    public function setStartDate(DateTimeImmutable $startDate): self
    {
        $this->startDate = $startDate;
        return $this;
    }

    public function getEndDate(): DateTimeImmutable
    {
        return $this->endDate;
    }

    public function setEndDate(DateTimeImmutable $endDate): self
    {
        $this->endDate = $endDate;
        return $this;
    }

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

    public function getTripLocations(): Collection
    {
        return $this->tripLocations;
    }

    public function addTripLocation(UserTripLocation $tripLocation): self
    {
        if (!$this->tripLocations->contains($tripLocation)) {
            $this->tripLocations->add($tripLocation);
            $tripLocation->setUserTrip($this);
        }
        return $this;
    }

    public function removeTripLocation(UserTripLocation $tripLocation): self
    {
        if ($this->tripLocations->removeElement($tripLocation)) {
            if ($tripLocation->getUserTrip() === $this) {
                $tripLocation->setUserTrip(null);
            }
        }
        return $this;
    }

    public function getWorkLogs(): Collection
    {
        return $this->workLogs;
    }

    public function addWorkLog(UserTripWorkLog $workLog): self
    {
        if (!$this->workLogs->contains($workLog)) {
            $this->workLogs->add($workLog);
            $workLog->setUserTrip($this);
        }
        return $this;
    }

    public function removeWorkLog(UserTripWorkLog $workLog): self
    {
        if ($this->workLogs->removeElement($workLog)) {
            if ($workLog->getUserTrip() === $this) {
                $workLog->setUserTrip(null);
            }
        }
        return $this;
    }
}