<?php

declare(strict_types=1);

namespace App\Module\Directory\Domain\Entity;

use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use App\Module\Directory\Domain\Enum\ProspectStatus;
use App\Module\Directory\Infrastructure\ApiPlatform\State\ConvertProspectProcessor;
use App\Module\Directory\Infrastructure\Doctrine\DoctrineProspectRepository;
use App\Shared\Domain\Attribute\Auditable;
use App\Shared\Domain\Contract\BlamableInterface;
use App\Shared\Domain\Contract\ClientInterface;
use App\Shared\Domain\Contract\TimestampableInterface;
use App\Shared\Domain\Trait\TimestampableBlamableTrait;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;

#[Auditable]
#[ApiResource(
    operations: [
        new GetCollection(paginationEnabled: false, security: "is_granted('directory.prospects.view')"),
        new Get(security: "is_granted('directory.prospects.view')"),
        new Post(security: "is_granted('directory.prospects.manage')"),
        new Patch(security: "is_granted('directory.prospects.manage')"),
        new Delete(security: "is_granted('directory.prospects.manage')"),
        new Post(
            uriTemplate: '/prospects/{id}/convert',
            security: "is_granted('directory.prospects.manage')",
            processor: ConvertProspectProcessor::class,
        ),
    ],
    normalizationContext: ['groups' => ['prospect:read']],
    denormalizationContext: ['groups' => ['prospect:write']],
    order: ['company' => 'ASC'],
)]
#[ApiFilter(SearchFilter::class, properties: ['status' => 'exact'])]
#[ORM\Entity(repositoryClass: DoctrineProspectRepository::class)]
#[ORM\Table(name: 'prospect')]
class Prospect implements TimestampableInterface, BlamableInterface
{
    use TimestampableBlamableTrait;

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    #[Groups(['prospect:read'])]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    #[Groups(['prospect:read', 'prospect:write'])]
    private ?string $company = null;

    #[ORM\Column(length: 255, nullable: true)]
    #[Groups(['prospect:read', 'prospect:write'])]
    private ?string $email = null;

    #[ORM\Column(length: 50, nullable: true)]
    #[Groups(['prospect:read', 'prospect:write'])]
    private ?string $phone = null;

    #[ORM\Column(length: 255, nullable: true)]
    #[Groups(['prospect:read', 'prospect:write'])]
    private ?string $website = null;

    #[ORM\Column(type: Types::STRING, length: 32, enumType: ProspectStatus::class)]
    #[Groups(['prospect:read', 'prospect:write'])]
    private ProspectStatus $status = ProspectStatus::New;

    #[ORM\Column(length: 255, nullable: true)]
    #[Groups(['prospect:read', 'prospect:write'])]
    private ?string $source = null;

    #[ORM\Column(type: Types::TEXT, nullable: true)]
    #[Groups(['prospect:read', 'prospect:write'])]
    private ?string $notes = null;

    #[ORM\ManyToOne(targetEntity: ClientInterface::class)]
    #[ORM\JoinColumn(name: 'converted_client_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    #[Groups(['prospect:read'])]
    private ?ClientInterface $convertedClient = null;

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

    public function getCompany(): ?string
    {
        return $this->company;
    }

    public function setCompany(string $company): static
    {
        $this->company = $company;

        return $this;
    }

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

    public function setEmail(?string $email): static
    {
        $this->email = $email;

        return $this;
    }

    public function getPhone(): ?string
    {
        return $this->phone;
    }

    public function setPhone(?string $phone): static
    {
        $this->phone = $phone;

        return $this;
    }

    public function getWebsite(): ?string
    {
        return $this->website;
    }

    public function setWebsite(?string $website): static
    {
        $this->website = $website;

        return $this;
    }

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

    public function setStatus(ProspectStatus $status): static
    {
        $this->status = $status;

        return $this;
    }

    public function getSource(): ?string
    {
        return $this->source;
    }

    public function setSource(?string $source): static
    {
        $this->source = $source;

        return $this;
    }

    public function getNotes(): ?string
    {
        return $this->notes;
    }

    public function setNotes(?string $notes): static
    {
        $this->notes = $notes;

        return $this;
    }

    public function getConvertedClient(): ?ClientInterface
    {
        return $this->convertedClient;
    }

    public function setConvertedClient(?ClientInterface $convertedClient): static
    {
        $this->convertedClient = $convertedClient;

        return $this;
    }
}
