<?php

declare(strict_types=1);

namespace App\Core;

final class Validator
{
    private array $errors = [];

    public function required(string $field, mixed $value, string $label): self
    {
        if ($value === null || trim((string)$value) === '') {
            $this->errors[$field] = "Le champ {$label} est obligatoire.";
        }
        return $this;
    }

    public function email(string $field, mixed $value, string $label): self
    {
        if ($value !== null && trim((string)$value) !== '' && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
            $this->errors[$field] = "Le champ {$label} doit contenir une adresse e-mail valide.";
        }
        return $this;
    }

    public function addError(string $field, string $message): self
    {
        $this->errors[$field] = $message;
        return $this;
    }

    public function errors(): array
    {
        return $this->errors;
    }

    public function passes(): bool
    {
        return empty($this->errors);
    }
}
