<?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: 'trip')]
class Trip
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: Vessel::class, inversedBy: 'trips')]
    #[ORM\JoinColumn(nullable: false)]
    private Vessel $vessel;

    #[ORM\ManyToOne(targetEntity: Location::class)]
    #[ORM\JoinColumn(name: 'start_location_id', nullable: false)]
    private Location $startLocation;

    #[ORM\ManyToOne(targetEntity: Location::class)]
    #[ORM\JoinColumn(name: 'end_location_id', nullable: false)]
    private Location $endLocation;

    #[ORM\Column]
    private DateTimeImmutable $startDate;

    #[ORM\Column]
    private DateTimeImmutable $endDate;

    #[ORM\Column(type: "text", nullable: true)]
    protected ?string $note;

    #[ORM\Column(nullable: false)]
    private bool $completed = false;

    #[ORM\Column]
    private DateTimeImmutable $createdAt;

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

    #[ORM\OneToMany(targetEntity: UserTrip::class, mappedBy: 'trip')]
    private Collection $userTrips;

    public function __construct(
        Vessel $vessel,
        Location $startLocation,
        Location $endLocation,
        DateTimeImmutable $startDate,
        DateTimeImmutable $endDate
    ) {
        $this->vessel = $vessel;
        $this->startLocation = $startLocation;
        $this->endLocation = $endLocation;
        $this->startDate = $startDate;
        $this->endDate = $endDate;
        $this->completed = false;
        $this->createdAt = new DateTimeImmutable();
        $this->tripLocations = new ArrayCollection();
        $this->userTrips = new ArrayCollection();
    }

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

    public function getVessel(): Vessel
    {
        return $this->vessel;
    }

    public function setVessel(Vessel $vessel): self
    {
        $this->vessel = $vessel;
        return $this;
    }

    public function getCustomerReference(): ?string
    {
        return $this->customerReference;
    }

    public function setCustomerReference(?string $customerReference): void
    {
        $this->customerReference = $customerReference;
    }

    public function getStartLocation(): Location
    {
        return $this->startLocation;
    }

    public function setStartLocation(Location $startLocation): self
    {
        $this->startLocation = $startLocation;
        return $this;
    }

    public function getEndLocation(): Location
    {
        return $this->endLocation;
    }

    public function setEndLocation(Location $endLocation): self
    {
        $this->endLocation = $endLocation;
        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 getNote(): ?string
    {
        return $this->note;
    }

    public function setNote(?string $note): void
    {
        $this->note = $note;
    }

    public function isCompleted(): bool
    {
        return $this->completed;
    }

    public function setCompleted(): void
    {
        $completed = true;
        if (count($this->userTrips) <= 0) {
            $completed = false;
        } else {
            /** @var UserTrip $userTrip */
            foreach ($this->userTrips as $userTrip) {
                if (!$userTrip->isCompleted() || !$userTrip->isApproved()) {
                    $completed = false;
                    break;
                }
            }
        }
        $this->completed = $completed;
    }

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

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

    public function getUserTrips(): Collection
    {
        return $this->userTrips;
    }
}