<?php
/**
 * Template: Legal Suite (native overview)
 *
 * Landing overview of the legal setup: company-data status, generated documents
 * with versions, and quick links to the Legal Wizard, Documents and Compliance
 * pages. Read-only; all editing happens on the linked pages.
 *
 * @var array $data get-legal-wizard-data response: { legalEntity:{...}|null, documents:[{type,version}], ... }
 * @package Biscotti_CMP
 */

defined( 'ABSPATH' ) || exit;

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template is included inside Biscotti_Admin::render_dynamic_page(); these are function-scoped locals, not globals.

$page_title = __( 'Legal Suite', 'biscotti-cmp' );
include __DIR__ . '/partials/page-header.php';

if ( null === $data ) :
	include __DIR__ . '/partials/loading-state.php';
	return;
endif;

$e         = ( isset( $data['legalEntity'] ) && is_array( $data['legalEntity'] ) ) ? $data['legalEntity'] : array();
$documents = ( isset( $data['documents'] ) && is_array( $data['documents'] ) ) ? $data['documents'] : array();
$has_entity = ! empty( $e ) && ! empty( $e['companyName'] );

$doc_label = function ( $type ) {
	if ( function_exists( 'biscotti_doc_type_label' ) ) {
		return biscotti_doc_type_label( strtoupper( (string) $type ) );
	}
	return strtoupper( (string) $type );
};
?>

<div class="biscotti-page biscotti-page-legal-suite">

	<!-- Company status -->
	<div class="biscotti-card">
		<h3>🏢 <?php esc_html_e( 'Company Data', 'biscotti-cmp' ); ?></h3>
		<?php if ( $has_entity ) : ?>
			<p>
				<span class="biscotti-pill biscotti-pill-green">✓ <?php esc_html_e( 'Configured', 'biscotti-cmp' ); ?></span>
				<strong style="margin-left:8px;"><?php echo esc_html( $e['companyName'] ); ?></strong>
				<?php if ( ! empty( $e['city'] ) ) : ?>
					<span class="description"><?php echo esc_html( ' — ' . ( $e['city'] ) ); ?></span>
				<?php endif; ?>
			</p>
		<?php else : ?>
			<p><span class="biscotti-pill biscotti-pill-amber">⚠️ <?php esc_html_e( 'Not configured', 'biscotti-cmp' ); ?></span>
			<?php esc_html_e( 'Complete your company data to generate legal documents.', 'biscotti-cmp' ); ?></p>
		<?php endif; ?>
		<p><a href="<?php echo esc_url( admin_url( 'admin.php?page=biscotti-legal-wizard' ) ); ?>" class="button button-primary"><?php esc_html_e( 'Open Legal Wizard', 'biscotti-cmp' ); ?> &rarr;</a></p>
	</div>

	<!-- Documents -->
	<div class="biscotti-card">
		<h3>📄 <?php esc_html_e( 'Legal Documents', 'biscotti-cmp' ); ?></h3>
		<?php if ( empty( $documents ) ) : ?>
			<p class="biscotti-empty-state"><?php esc_html_e( 'No documents generated yet.', 'biscotti-cmp' ); ?></p>
		<?php else : ?>
			<table class="widefat biscotti-data-table">
				<thead><tr>
					<th><?php esc_html_e( 'Document', 'biscotti-cmp' ); ?></th>
					<th><?php esc_html_e( 'Version', 'biscotti-cmp' ); ?></th>
				</tr></thead>
				<tbody>
					<?php foreach ( $documents as $d ) : ?>
						<tr>
							<td><strong><?php echo esc_html( $doc_label( $d['type'] ?? '' ) ); ?></strong></td>
							<td>v<?php echo esc_html( isset( $d['version'] ) ? absint( $d['version'] ) : 1 ); ?></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		<?php endif; ?>
		<p>
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=biscotti-documents' ) ); ?>" class="button"><?php esc_html_e( 'Manage Documents', 'biscotti-cmp' ); ?> &rarr;</a>
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=biscotti-compliance' ) ); ?>" class="button"><?php esc_html_e( 'View Compliance Report', 'biscotti-cmp' ); ?> &rarr;</a>
		</p>
	</div>

</div>
