<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Attribute as Vich;

#[ORM\Entity]
#[Vich\Uploadable]
class Contrat
{
    public const STATE_DRAFT = 'draft';
    public const STATE_SEND = 'send';
    public const STATE_SIGNED = 'signed';
    public const STATE_CANCELLED = 'cancelled';

    public const TYPE_MIGRATION_SITECONSEIL = 'migration_siteconseil';

    public const TYPE_LABELS = [
        self::TYPE_MIGRATION_SITECONSEIL => 'Contrat de Service - Migration SARL SITECONSEIL',
    ];

    public const SERVICE_CATALOG = [
        'esite_vitrine' => ['label' => 'E-Site Vitrine', 'price' => '100.00', 'unit' => '/mois'],
        'esite_ecommerce' => ['label' => 'E-Site E-Commerce', 'price' => '150.00', 'unit' => '/mois'],
        'esite_hors_cms' => ['label' => 'Site hors CMS Esy-Web (hebergement seul)', 'price' => '100.00', 'unit' => '/mois'],
        'email_3go' => ['label' => 'E-Mail 3 Go', 'price' => '1.00', 'unit' => '/mois'],
        'email_50go' => ['label' => 'E-Mail 50 Go', 'price' => '5.00', 'unit' => '/mois'],
        'ndd_gestion' => ['label' => 'Nom de domaine - Gestion', 'price' => '30.00', 'unit' => '/an'],
        'ndd_renouvellement' => ['label' => 'Nom de domaine - Renouvellement', 'price' => '20.00', 'unit' => '/an'],
        'eprotect' => ['label' => 'E-Protect Pro', 'price' => '60.00', 'unit' => '/trimestre'],
        'ecalendar' => ['label' => 'E-Calendar', 'price' => '30.00', 'unit' => '/mois'],
        'echat' => ['label' => 'E-Chat', 'price' => '15.00', 'unit' => '/mois'],
        'emailer' => ['label' => 'E-Mailer', 'price' => '30.00', 'unit' => '/mois'],
    ];

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

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

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

    #[ORM\Column(length: 50)]
    private string $type;

    /** @var list<array{service: string, quantity: int, priceHt: string}> */
    #[ORM\Column(type: 'json')]
    private array $services = [];

    #[ORM\Column(length: 20, options: ['default' => 'draft'])]
    private string $state = self::STATE_DRAFT;

    #[ORM\Column(nullable: true)]
    private ?string $submissionId = null;

    #[ORM\Column(nullable: true)]
    private ?int $submitterCompanyId = null;

    #[ORM\Column(nullable: true)]
    private ?int $submitterCustomerId = null;

    // ── PDF Unsigned ──
    #[ORM\Column(length: 255, nullable: true)]
    private ?string $pdfUnsigned = null;

    #[Vich\UploadableField(mapping: 'contrat_pdf', fileNameProperty: 'pdfUnsigned')]
    private ?File $pdfUnsignedFile = null;

    // ── PDF Signed ──
    #[ORM\Column(length: 255, nullable: true)]
    private ?string $pdfSigned = null;

    #[Vich\UploadableField(mapping: 'contrat_signed_pdf', fileNameProperty: 'pdfSigned')]
    private ?File $pdfSignedFile = null;

    // ── PDF Audit ──
    #[ORM\Column(length: 255, nullable: true)]
    private ?string $pdfAudit = null;

    #[Vich\UploadableField(mapping: 'contrat_audit_pdf', fileNameProperty: 'pdfAudit')]
    private ?File $pdfAuditFile = null;

    #[ORM\ManyToOne(targetEntity: Customer::class)]
    #[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
    private ?Customer $customer = null;

    #[ORM\Column]
    private \DateTimeImmutable $createdAt;

    #[ORM\Column(nullable: true)]
    private ?\DateTimeImmutable $signedAt = null;

    #[ORM\Column(nullable: true)]
    private ?\DateTimeImmutable $updatedAt = null;

    public function __construct(string $email, string $raisonSociale, string $type)
    {
        $this->email = $email;
        $this->raisonSociale = $raisonSociale;
        $this->type = $type;
        $this->createdAt = new \DateTimeImmutable();
    }

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

    public function getReference(): string
    {
        return 'CTR_'.str_pad((string) ($this->id ?? 0), 5, '0', \STR_PAD_LEFT);
    }

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

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

        return $this;
    }

    public function getRaisonSociale(): string
    {
        return $this->raisonSociale;
    }

    public function setRaisonSociale(string $raisonSociale): static
    {
        $this->raisonSociale = $raisonSociale;

        return $this;
    }

    public function getType(): string
    {
        return $this->type;
    }

    public function getTypeLabel(): string
    {
        return self::TYPE_LABELS[$this->type] ?? $this->type;
    }

    public function setType(string $type): static
    {
        $this->type = $type;

        return $this;
    }

    public function getState(): string
    {
        return $this->state;
    }

    public function setState(string $state): static
    {
        $this->state = $state;

        return $this;
    }

    /** @return list<array{service: string, quantity: int, priceHt: string}> */
    public function getServices(): array
    {
        return $this->services;
    }

    /** @param list<array{service: string, quantity: int, priceHt: string}> $services */
    public function setServices(array $services): static
    {
        $this->services = $services;

        return $this;
    }

    public function getTotalHt(): float
    {
        $total = 0.0;
        foreach ($this->services as $s) {
            $total += (float) $s['priceHt'] * $s['quantity'];
        }

        return $total;
    }

    public function getSubmissionId(): ?string
    {
        return $this->submissionId;
    }

    public function setSubmissionId(?string $submissionId): static
    {
        $this->submissionId = $submissionId;

        return $this;
    }

    public function getSubmitterCompanyId(): ?int
    {
        return $this->submitterCompanyId;
    }

    public function setSubmitterCompanyId(?int $submitterCompanyId): static
    {
        $this->submitterCompanyId = $submitterCompanyId;

        return $this;
    }

    public function getSubmitterCustomerId(): ?int
    {
        return $this->submitterCustomerId;
    }

    public function setSubmitterCustomerId(?int $submitterCustomerId): static
    {
        $this->submitterCustomerId = $submitterCustomerId;

        return $this;
    }

    public function getPdfUnsigned(): ?string
    {
        return $this->pdfUnsigned;
    }

    public function setPdfUnsigned(?string $pdfUnsigned): static
    {
        $this->pdfUnsigned = $pdfUnsigned;

        return $this;
    }

    public function getPdfUnsignedFile(): ?File
    {
        return $this->pdfUnsignedFile;
    }

    public function setPdfUnsignedFile(?File $file): static
    {
        $this->pdfUnsignedFile = $file;
        if (null !== $file) {
            $this->updatedAt = new \DateTimeImmutable();
        }

        return $this;
    }

    public function getPdfSigned(): ?string
    {
        return $this->pdfSigned;
    }

    public function setPdfSigned(?string $pdfSigned): static
    {
        $this->pdfSigned = $pdfSigned;

        return $this;
    }

    public function getPdfSignedFile(): ?File
    {
        return $this->pdfSignedFile;
    }

    public function setPdfSignedFile(?File $file): static
    {
        $this->pdfSignedFile = $file;
        if (null !== $file) {
            $this->updatedAt = new \DateTimeImmutable();
        }

        return $this;
    }

    public function getPdfAudit(): ?string
    {
        return $this->pdfAudit;
    }

    public function setPdfAudit(?string $pdfAudit): static
    {
        $this->pdfAudit = $pdfAudit;

        return $this;
    }

    public function getPdfAuditFile(): ?File
    {
        return $this->pdfAuditFile;
    }

    public function setPdfAuditFile(?File $file): static
    {
        $this->pdfAuditFile = $file;
        if (null !== $file) {
            $this->updatedAt = new \DateTimeImmutable();
        }

        return $this;
    }

    public function getCustomer(): ?Customer
    {
        return $this->customer;
    }

    public function setCustomer(?Customer $customer): static
    {
        $this->customer = $customer;

        return $this;
    }

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

    public function getSignedAt(): ?\DateTimeImmutable
    {
        return $this->signedAt;
    }

    public function setSignedAt(?\DateTimeImmutable $signedAt): static
    {
        $this->signedAt = $signedAt;

        return $this;
    }

    public function getUpdatedAt(): ?\DateTimeImmutable
    {
        return $this->updatedAt;
    }

    public function setUpdatedAt(?\DateTimeImmutable $updatedAt): static
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }
}
