<?php
/**
 * Manual document generation admin controller.
 *
 * @package EntryFoundry
 */

namespace EntryFoundry\Core\Controllers\Admin;

// Constructor and hook method types define this focused controller contract.
// phpcs:disable Squiz.Commenting, Generic.Commenting.DocComment

use EntryFoundry\Core\Models\Template\TemplateRepository;
use EntryFoundry\Core\Services\Generation\DocumentGenerationService;
use EntryFoundry\Core\Services\Security\SignedUrlService;
use RuntimeException;

/** Provides an authenticated template/entry PDF generation form. */
final class GenerationAdminController {
	/** @var DocumentGenerationService */ private $generation;
	/** @var SignedUrlService */ private $signer;

	public function __construct( DocumentGenerationService $generation, SignedUrlService $signer ) {
		$this->generation = $generation;
		$this->signer     = $signer;
	}

	public function register_menu(): void {
		add_submenu_page( 'edit.php?post_type=' . TemplateRepository::POST_TYPE, __( 'Generate / Test PDF', 'entryfoundry-pdf-builder-for-formidable-forms' ), __( 'Generate / Test PDF', 'entryfoundry-pdf-builder-for-formidable-forms' ), 'edit_posts', 'entryfoundry-generate', array( $this, 'render' ) );
	}

	public function render(): void {
		$template_id  = 0;
		$entry_id     = 0;
		$download_url = '';
		$error        = '';
		if ( isset( $_POST['entryfoundry_generate'] ) ) {
			check_admin_referer( 'entryfoundry_generate_pdf' );
			$template_id = isset( $_POST['template_id'] ) ? absint( $_POST['template_id'] ) : 0;
			$entry_id    = isset( $_POST['entry_id'] ) ? absint( $_POST['entry_id'] ) : 0;
			$template    = $template_id > 0 ? get_post( $template_id ) : null;
			if ( $template_id < 1 || ! $template || TemplateRepository::POST_TYPE !== $template->post_type ) {
				$error = __( 'Enter a valid EntryFoundry template ID.', 'entryfoundry-pdf-builder-for-formidable-forms' );
			} elseif ( $entry_id < 1 ) {
				$error = __( 'Enter a valid Formidable entry ID.', 'entryfoundry-pdf-builder-for-formidable-forms' );
			} elseif ( ! current_user_can( 'edit_post', $template_id ) ) {
				$error = __( 'You do not have permission to generate a PDF for this template.', 'entryfoundry-pdf-builder-for-formidable-forms' );
			} else {
				try {
					$this->generation->generate_pdf( $template_id, $entry_id );
					$download_url = $this->signer->create( admin_url( 'admin-post.php' ), $template_id, $entry_id, 'pdf', 'download' );
				} catch ( RuntimeException $exception ) {
					$error = __( 'The PDF could not be generated.', 'entryfoundry-pdf-builder-for-formidable-forms' );
				}
			}
		}
		echo '<div class="wrap"><h1>' . esc_html__( 'Generate / Test EntryFoundry PDF', 'entryfoundry-pdf-builder-for-formidable-forms' ) . '</h1>';
		echo '<p>' . esc_html__( 'This is a manual preview and troubleshooting tool. Published shortcodes, Formidable Views, and submission emails generate documents dynamically; they do not depend on this screen.', 'entryfoundry-pdf-builder-for-formidable-forms' ) . '</p>';
		if ( $error ) {
			echo '<div class="notice notice-error"><p>' . esc_html( $error ) . '</p></div>'; }
		if ( $download_url ) {
			echo '<div class="notice notice-success"><p><a href="' . esc_url( $download_url ) . '">' . esc_html__( 'Download generated PDF', 'entryfoundry-pdf-builder-for-formidable-forms' ) . '</a></p></div>'; }
		echo '<form method="post">';
		wp_nonce_field( 'entryfoundry_generate_pdf' );
		echo '<table class="form-table"><tr><th><label for="entryfoundry-template-id">' . esc_html__( 'Template ID', 'entryfoundry-pdf-builder-for-formidable-forms' ) . '</label></th><td><input required type="number" min="1" id="entryfoundry-template-id" name="template_id" value="' . esc_attr( (string) $template_id ) . '"></td></tr>';
		echo '<tr><th><label for="entryfoundry-entry-id">' . esc_html__( 'Entry ID', 'entryfoundry-pdf-builder-for-formidable-forms' ) . '</label></th><td><input required type="number" min="1" id="entryfoundry-entry-id" name="entry_id" value="' . esc_attr( (string) $entry_id ) . '"></td></tr></table>';
		submit_button( __( 'Generate PDF', 'entryfoundry-pdf-builder-for-formidable-forms' ), 'primary', 'entryfoundry_generate' );
		echo '</form></div>';
	}
}
