<?php
// +-------------------------------------------------+
// © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: RecordFilterCreatedAfterDate.php,v 1.1.4.2 2026/04/30 09:26:09 rtigero Exp $

namespace Pmb\DSI\Models\Filter\Entities\Record\RecordFilterCreatedAfterDate;

use Pmb\DSI\Models\Filter\Entities\Record\RecordFilter;

class RecordFilterCreatedAfterDate extends RecordFilter
{
    public static $fields = [
        "after_date" => [
            "type" => "date",
            "required" => true
        ]
    ];

    public function __construct(array $data, int $entityId = 0)
    {
        parent::__construct($data, $entityId);
    }

    public function filter(): array
    {
        $filteredData = [];

        $after_date = new \Datetime($this->fieldsValues->after_date);
        //Si on n'a pas de dernière diffusion on renvoie tout
        if (! isset($this->fieldsValues->after_date) || is_null($after_date)) {
            return $this->data;
        }

        foreach ($this->data as $id => $item) {
            $notice = new \notice($id);

            $date = \DateTime::createFromFormat("d/m/Y H:i:s", $notice->create_date);

            if (!$date) {
                // Ignore les entrées avec des dates invalides
                continue;
            }

            if ($date->format("U") > $after_date->format("U")) {
                $filteredData[$id] = $item;
            }
        }

        return $filteredData;
    }
}
