<?php

namespace Blazemedia\Affiliation\API;

use Blazemedia\Affiliation\Rendering\PageData;

class ArticlesByDate extends ApiGET {

    protected string $action     = 'articlesbydate';
    protected array  $parameters = ['date'];
    
    protected function transientKey() {
        
        return "articles_by_date_{$this->params->date}";
    }


    protected function getData(){

        $date = $this->params->date;

        $date= explode('-', $date);
        $year  = $date[0];
        $month = $date[1];
        $day   = $date[2];

        $query = [
            'post_type'   => ['notizie', 'guide'],
            'posts_per_page' => -1,
            'date_query' => [
                'year'  => $year,
                'month' => $month,
                'day'   => $day,
            ]
        ];


         $articles = array_map(function($post){

            $pagedata = new PageData($post);

            return $pagedata->getParams([
                'date'        => \get_the_date( 'Y-m-d' , $post->ID ),
                'attributes'  => \get_post_meta( $post->ID , 'bm_data_attributes' )
            ]);


         } , get_posts( $query )); 


        return json_encode( $articles );
    }

}