<?php

namespace App\Entity;

use App\Repository\StockVehicleRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * Une ligne de la « photo du stock » importée dans Paramètres (export DMS).
 * La table est entièrement remplacée à chaque import. La concession est référencée
 * par son ID externe (ID_CONCESSION), résolu en nom à l'affichage.
 */
#[ORM\Entity(repositoryClass: StockVehicleRepository::class)]
#[ORM\Table(name: 'stock_vehicles')]
#[ORM\Index(name: 'idx_stock_concession_id', columns: ['concession_id'])]
class StockVehicle
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(type: 'date_immutable', nullable: true)]
    private ?\DateTimeImmutable $dateStock;

    /** ID_CONCESSION du DMS. */
    #[ORM\Column(length: 64)]
    private string $concessionId;

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

    /** Immatriculation. */
    #[ORM\Column(length: 50)]
    private string $plate;

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

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

    #[ORM\Column(type: 'datetime_immutable')]
    private \DateTimeImmutable $importedAt;

    public function __construct(
        ?\DateTimeImmutable $dateStock,
        string $concessionId,
        string $type,
        string $plate,
        string $vin,
        string $localisation,
        \DateTimeImmutable $importedAt,
    ) {
        $this->dateStock = $dateStock;
        $this->concessionId = mb_substr($concessionId, 0, 64);
        $this->type = mb_substr($type, 0, 100);
        $this->plate = mb_substr($plate, 0, 50);
        $this->vin = mb_substr($vin, 0, 50);
        $this->localisation = mb_substr($localisation, 0, 255);
        $this->importedAt = $importedAt;
    }

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

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

    public function getConcessionId(): string
    {
        return $this->concessionId;
    }

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

    public function getPlate(): string
    {
        return $this->plate;
    }

    public function getVin(): string
    {
        return $this->vin;
    }

    public function getLocalisation(): string
    {
        return $this->localisation;
    }

    public function getImportedAt(): \DateTimeImmutable
    {
        return $this->importedAt;
    }

    /**
     * Forme tableau attendue par InventoryComparator et les templates/exports.
     *
     * @return array{dateStock: ?\DateTimeImmutable, concessionId: string, type: string, plate: string, vin: string, localisation: string}
     */
    public function toRow(): array
    {
        return [
            'dateStock' => $this->dateStock,
            'concessionId' => $this->concessionId,
            'type' => $this->type,
            'plate' => $this->plate,
            'vin' => $this->vin,
            'localisation' => $this->localisation,
        ];
    }
}
