<?php

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use App\Repository\CoastalPointRepository;
use Symfony\Component\Serializer\Annotation\Groups;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: CoastalPointRepository::class)]
#[ORM\Index(columns: ['type'], name: 'idx_coastal_type')]
#[ORM\Index(columns: ['region'], name: 'idx_coastal_region')]
#[ApiResource(
    shortName: 'Spot',
    operations: [
        new GetCollection(),
        new Get(),
    ],
    normalizationContext: ['groups' => ['spot:read']],
    order: ['name' => 'ASC'],
)]
class CoastalPoint
{
    #[ORM\Id]
    #[ORM\Column(type: UuidType::NAME, unique: true)]
    #[ORM\GeneratedValue(strategy: 'CUSTOM')]
    #[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
    #[Groups(['spot:read'])]
    private ?Uuid $id = null;

    #[ORM\Column(length: 150)]
    #[Groups(['spot:read'])]
    private string $name;

    #[ORM\Column(type: 'geometry', options: ['geometry_type' => 'POINT', 'srid' => 4326])]
    private mixed $geometry = null;

    #[ORM\Column(length: 30)]
    #[Groups(['spot:read'])]
    private string $type;

    #[ORM\Column(length: 100)]
    #[Groups(['spot:read'])]
    private string $region;

    /** @var Collection<int, ImpactScore> */
    #[ORM\OneToMany(targetEntity: ImpactScore::class, mappedBy: 'coastalPoint')]
    private Collection $impactScores;

    /** @var Collection<int, UserFeedback> */
    #[ORM\OneToMany(targetEntity: UserFeedback::class, mappedBy: 'coastalPoint')]
    private Collection $feedbacks;

    public function __construct()
    {
        $this->name   = '';
        $this->type   = '';
        $this->region = '';
        $this->impactScores = new ArrayCollection();
        $this->feedbacks = new ArrayCollection();
    }

    public function getId(): ?Uuid { return $this->id; }
    public function getName(): string { return $this->name; }
    public function setName(string $name): static { $this->name = $name; return $this; }
    public function getGeometry(): mixed { return $this->geometry; }
    public function setGeometry(mixed $geometry): static { $this->geometry = $geometry; return $this; }

    #[Groups(['spot:read'])]
    public function getLongitude(): ?float
    {
        if ($this->geometry === null) return null;
        preg_match('/POINT\(([^ ]+) /', (string) $this->geometry, $m);
        return isset($m[1]) ? (float) $m[1] : null;
    }

    #[Groups(['spot:read'])]
    public function getLatitude(): ?float
    {
        if ($this->geometry === null) return null;
        preg_match('/POINT\([^ ]+ ([^)]+)\)/', (string) $this->geometry, $m);
        return isset($m[1]) ? (float) $m[1] : null;
    }
    public function getType(): string { return $this->type; }
    public function setType(string $type): static { $this->type = $type; return $this; }
    public function getRegion(): string { return $this->region; }
    public function setRegion(string $region): static { $this->region = $region; return $this; }

    /** @return Collection<int, ImpactScore> */
    public function getImpactScores(): Collection { return $this->impactScores; }

    /** @return Collection<int, UserFeedback> */
    public function getFeedbacks(): Collection { return $this->feedbacks; }
}
