<?php

/**
 * Contentosapp Studio Studio — Unified workspace page.
 *
 * Wraps Content Hub and New Production as pill tabs.
 * Settings moved to its own menu page (costudio-settings).
 * Uses server-side tab rendering (one view included per request).
 *
 * @package Contentosapp_Studio
 * @since   1.1.0
 */

defined('ABSPATH') || exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals -- View template variables are scoped to this included admin file.

// $studio_active_tab / $studio_plan_id are read (capability-gated, sanitized)
// in the page_studio() menu callback before this template is included.
$valid_tabs = ['hub', 'producao'];
$active_tab_raw = $studio_active_tab ?? '';
$active_tab = in_array( $active_tab_raw, $valid_tabs, true )
	? $active_tab_raw
	: 'hub';

// Backwards compat: redirect old ?tab=settings to the new standalone page.
if ( 'settings' === $active_tab_raw ) {
	wp_safe_redirect( admin_url( 'admin.php?page=costudio-settings' ) );
	exit;
}

$service_plan = costudio_license() ? costudio_license()->get_plan() : Costudio_Standalone_License::PLAN_FREE;
?>

<div class="wrap fos-standalone-wrap">

	<!-- ── PAGE HEADER ───────────────────────────────────────────────────── -->
	<?php
	$costudio_header_title = __( 'Studio', 'contentosapp-studio' );
	$costudio_header_subtitle = __( 'Your content hub — productions, drafts, published articles', 'contentosapp-studio' );
	$costudio_header_right = '<span class="cts-badge cts-badge--neutral">'
		. esc_html( sprintf(
			/* translators: %s: ContentOS Auto service plan name. */
			__( 'Auto: %s', 'contentosapp-studio' ),
			ucfirst( $service_plan )
		) ) . '</span>';
	include __DIR__ . '/../partials/partial-page-header.php';
	?>

	<!-- ── STUDIO TAB NAV ────────────────────────────────────────────────── -->
	<nav class="fos-studio-tabs" role="tablist" aria-label="<?php esc_attr_e('Studio sections', 'contentosapp-studio'); ?>">
		<button type="button" class="fos-studio-tab-btn<?php echo esc_attr( 'hub' === $active_tab ? ' is-active' : '' ); ?>"
			data-tab="hub" role="tab"
			aria-selected="<?php echo esc_attr( 'hub' === $active_tab ? 'true' : 'false' ); ?>">
			<span class="fos-studio-tab-btn__icon"></span>
			<?php esc_html_e('Content Hub', 'contentosapp-studio'); ?>
		</button>
		<button type="button" class="fos-studio-tab-btn<?php echo esc_attr( 'producao' === $active_tab ? ' is-active' : '' ); ?>"
			data-tab="producao" role="tab"
			aria-selected="<?php echo esc_attr( 'producao' === $active_tab ? 'true' : 'false' ); ?>">
			<span class="fos-studio-tab-btn__icon"></span>
			<?php esc_html_e('New Production', 'contentosapp-studio'); ?>
		</button>

		<?php
		// Determine initial mode.
		$current_user_id = get_current_user_id();
		$np_mode = get_user_meta( $current_user_id, 'costudio_np_mode', true );
		if ( empty( $np_mode ) ) {
			$prod_count = Costudio_Production::count_total();
			$np_mode = $prod_count <= 3 ? 'simple' : 'advanced';
		}
		$is_simple = $np_mode === 'simple';
		?>
		<div id="fos-np-mode-toggle-wrap" class="fos-np-mode-toggle-wrap" style="margin-left:auto; padding-right:18px; <?php echo esc_attr( 'producao' !== $active_tab ? 'display: none;' : '' ); ?>">
			<div style="display:flex; align-items:center; gap:8px;">
				<button type="button" class="fos-np-mode-label" data-np-mode="simple" style="font-size:13px; font-weight:600; color:var(--fos-text-sub); cursor:pointer; background:none; border:0; padding:0;"><?php esc_html_e( 'Simple Mode', 'contentosapp-studio' ); ?></button>
				<label class="fos-toggle" title="<?php esc_attr_e( 'Toggle Simple/Advanced mode', 'contentosapp-studio' ); ?>">
					<input type="checkbox" id="fos-np-mode-toggle" <?php checked( ! $is_simple ); ?>>
					<span class="fos-toggle__track"></span>
					<span class="fos-toggle__thumb"></span>
				</label>
				<button type="button" class="fos-np-mode-label" data-np-mode="advanced" style="font-size:13px; font-weight:600; color:var(--fos-text-sub); cursor:pointer; background:none; border:0; padding:0;"><?php esc_html_e( 'Advanced Mode', 'contentosapp-studio' ); ?></button>
			</div>
		</div>
	</nav>

	<!-- ── TAB CONTENT ───────────────────────────────────────────────────── -->
	<div class="fos-studio-content">
		<div id="fos-studio-panel-hub" class="fos-studio-panel <?php echo esc_attr( 'hub' === $active_tab ? 'is-active' : '' ); ?>">
			<?php include COSTUDIO_DIR . 'admin/views/page-central.php'; ?>
		</div>
		<div id="fos-studio-panel-producao" class="fos-studio-panel <?php echo esc_attr( 'producao' === $active_tab ? 'is-active' : '' ); ?>">
			<?php include COSTUDIO_DIR . 'admin/views/page-nova-producao.php'; ?>
		</div>
	</div>

	<?php
	// Sprint B.4 — quota-wall modals for the production submit flow.
	//   - modal-confirm-extra: Caso A — confirm using 1 extra credit when monthly is full.
	//   - modal-buy-addon:     Caso B — buy more extras when both monthly + addon are 0.
	// Both render hidden by default; the production JS triggers them on
	// quota errors. Including them here (Studio level, not inside the
	// nova-producao panel) keeps them addressable regardless of which
	// tab is active and avoids re-rendering on tab switches.
	include COSTUDIO_DIR . 'admin/views/partials/modal-confirm-extra.php';
	include COSTUDIO_DIR . 'admin/views/partials/modal-buy-addon.php';
	?>

</div>
