<?php

// If this file is called directly, abort.
if (!defined('WPINC')) {
	die;
}

class DocumentsController {
	public function getDocuments() {
		$args = [
			'post_type' => 'attachment',
			'post_mime_type' => [
				'application/pdf',
				'application/msword',
				'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
				'application/vnd.ms-excel',
				'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
				'application/vnd.ms-powerpoint',
				'application/vnd.openxmlformats-officedocument.presentationml.presentation',
				'text/plain',
				'text/csv',
				'application/rtf',
			],
			'post_status' => 'inherit',
			'posts_per_page' => -1,
		];

		$attachments = get_posts($args);
		$data = [];

		if (empty($attachments)) {
			return $data;
		}

		foreach ($attachments as $attachment) {
			$author_id = $attachment->post_author;
			$user = get_userdata($author_id);
			$author_name = $user ? ($user->display_name ?: $user->user_nicename) : "";
			$author_url = $author_id ? get_author_posts_url($author_id) : "";

			$data[] = [
				'id' => (int) $attachment->ID,
				'name' => $attachment->post_title ?? "",
				'url' => wp_get_attachment_url($attachment->ID),
				'author' => [
					'name' => $author_name,
					'url' => $author_url
				],
				'tags' => "",
				'post_date' => $attachment->post_date,
				'meta_description' => ""
			];
		}

		return $data;
	}
}