<?php
/**
 * Archive documentos
 */

$context = Timber::context();

$posts = Timber::get_posts([
    'post_type' => 'documento',
    'paged' => isset($params['paged']) ? $params['paged'] : 1,
    'posts_per_page' => 10,
    'lang' => '',
]);

$documentos = [];

foreach ($posts as $post) {
    $archivo = get_field('documento', $post->ID);
    
    $doc = [
        'id'    	=> $post->ID,
        'title' 	=> $post->title,
        'link'  	=> $post->link,
        'content' 	=> $post->content,
        'excerpt' 	=> $post->excerpt,
    ];
    
    if ($archivo && is_array($archivo)) {
        $doc['documento_url'] = $archivo['url'] ?? '';
        $doc['documento_filename'] = $archivo['filename'] ?? '';
        $doc['documento_filesize'] = size_format($archivo['filesize'] ?? 0);
        $doc['documento_extension'] = pathinfo($archivo['filename'] ?? '', PATHINFO_EXTENSION);
        $doc['thumbnail'] = get_the_post_thumbnail_url($post->ID, 'medium');
    }
    
    $documentos[] = $doc;
}

$context['documentos'] = $documentos;
$context['posts'] = $posts; 

error_log('✅ archive-documentos - Posts: ' . count($context['documentos']));

Timber::render('templates/page-documentos.twig', $context);
