<?php
/**
 * This file is part of the Wisewand WordPress plugin.
 *
 * @package Wisewand
 * @since 1.0.0
 * @license GPL-2.0-or-later
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */
namespace Wisewand\Admin;

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Wisewand\Utils\Options;
use Wisewand\Logging\Logger;
use Wisewand\Utils\Wisewand_Payload_Builder;

class Editorial_Calendar {

	private $options;
	private $logger;
	private $payload_builder;
	private $entry_manager;
	private $ajax_handler;

	public function __construct( Options $options, Logger $logger ) {
		$this->options = $options;
		$this->logger  = $logger;

		$this->payload_builder = new Wisewand_Payload_Builder( $options, $logger );

		if ( ! class_exists( 'Wisewand\\Calendar\\Calendar_Entry_Manager' ) ) {
			require_once WISEWAND_PLUGIN_DIR . 'includes/Calendar/Calendar_Entry_Manager.php';
		}

		if ( ! class_exists( 'Wisewand\\Repositories\\Calendar_Repository' ) ) {
			require_once WISEWAND_PLUGIN_DIR . 'includes/Repositories/Calendar_Repository.php';
		}

		$calendar_repository = new \Wisewand\Repositories\Calendar_Repository();
		$this->entry_manager = new \Wisewand\Calendar\Calendar_Entry_Manager( $options, $logger, $this->payload_builder, $calendar_repository );

		$this->ajax_handler = new Calendar_AJAX_Handler( $options, $logger, $this->entry_manager, $this->payload_builder );
	}

	public function render_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'wisewand-ai-content-writer-seo-content-generator' ) );
		}

		$stats = $this->entry_manager->get_statistics();

		echo '<div class="wrap editorial-calendar-page">';

		$this->render_header( $stats );
		$this->render_calendar_container();
		$this->render_side_panel();

		echo '</div>';
	}

	private function render_header( $stats ) {
		?>
		<div class="calendar-page-header">
			<div>
				<h1>📅 <?php esc_html_e( 'Editorial Calendar', 'wisewand-ai-content-writer-seo-content-generator' ); ?></h1>
				<p><?php esc_html_e( 'Manage your automated content publication roadmap', 'wisewand-ai-content-writer-seo-content-generator' ); ?></p>
			</div>
			<div class="header-stats">
				<div class="stat-item">
					<div class="stat-value"><?php echo esc_html( $stats['total_scheduled'] ?? 0 ); ?></div>
					<div class="stat-label"><?php esc_html_e( 'Total Scheduled', 'wisewand-ai-content-writer-seo-content-generator' ); ?></div>
				</div>
				<div class="stat-item">
					<div class="stat-value"><?php echo esc_html( $stats['upcoming_7_days'] ?? 0 ); ?></div>
					<div class="stat-label"><?php esc_html_e( 'Next 7 Days', 'wisewand-ai-content-writer-seo-content-generator' ); ?></div>
				</div>
				<div class="stat-item">
					<div class="stat-value"><?php echo esc_html( $stats['by_wisewand_status']['ready'] ?? 0 ); ?></div>
					<div class="stat-label"><?php esc_html_e( 'Ready to Publish', 'wisewand-ai-content-writer-seo-content-generator' ); ?></div>
				</div>
			</div>
		</div>
		<?php
	}

	private function render_calendar_container() {
		?>
		<div class="calendar-main">
			<div class="calendar-toolbar">
				<div class="toolbar-left">
					<button type="button" id="clear-calendar-editorial" class="btn-clear-calendar" style="background: #EF4444; color: white;">
						🗑️ <?php esc_html_e( 'Clear All Scheduled', 'wisewand-ai-content-writer-seo-content-generator' ); ?>
					</button>
					<select id="source-filter" class="source-filter-dropdown" style="margin-left: 12px; padding: 8px 12px; border: 1px solid #d1d5db; border-radius: 6px; font-size: 14px; background: white; cursor: pointer;">
						<option value="all"><?php esc_html_e( '📋 All Sources', 'wisewand-ai-content-writer-seo-content-generator' ); ?></option>
						<option value="manual"><?php esc_html_e( '✏️ Manual', 'wisewand-ai-content-writer-seo-content-generator' ); ?></option>
						<option value="discovery"><?php esc_html_e( '🔍 Discovery', 'wisewand-ai-content-writer-seo-content-generator' ); ?></option>
						<option value="rss"><?php esc_html_e( '📡 RSS Feeds', 'wisewand-ai-content-writer-seo-content-generator' ); ?></option>
					</select>
				</div>
				<div class="calendar-views">
					<button type="button" class="view-btn active" data-view="dayGridMonth">
						<?php esc_html_e( 'Month', 'wisewand-ai-content-writer-seo-content-generator' ); ?>
					</button>
					<button type="button" class="view-btn" data-view="timeGridWeek">
						<?php esc_html_e( 'Week', 'wisewand-ai-content-writer-seo-content-generator' ); ?>
					</button>
					<button type="button" class="view-btn" data-view="listMonth">
						<?php esc_html_e( 'List', 'wisewand-ai-content-writer-seo-content-generator' ); ?>
					</button>
				</div>
			</div>

			<div id="editorial-calendar"></div>

			<div class="calendar-legend">
				<span class="legend-item"><span class="legend-color" style="background: #3b82f6;"></span> <?php esc_html_e( 'Scheduled', 'wisewand-ai-content-writer-seo-content-generator' ); ?></span>
				<span class="legend-item"><span class="legend-color" style="background: #f59e0b;"></span> <?php esc_html_e( 'Generating', 'wisewand-ai-content-writer-seo-content-generator' ); ?></span>
				<span class="legend-item"><span class="legend-color" style="background: #8b5cf6;"></span> <?php esc_html_e( 'Ready', 'wisewand-ai-content-writer-seo-content-generator' ); ?></span>
				<span class="legend-item"><span class="legend-color" style="background: #10b981;"></span> <?php esc_html_e( 'Imported', 'wisewand-ai-content-writer-seo-content-generator' ); ?></span>
				<span class="legend-item"><span class="legend-color" style="background: #ef4444;"></span> <?php esc_html_e( 'Failed', 'wisewand-ai-content-writer-seo-content-generator' ); ?></span>
			</div>
		</div>
		<?php
	}

	private function render_side_panel() {
		?>
		<div id="entry-side-panel" class="side-panel" style="display: none;">
			<div class="panel-overlay"></div>
			<div class="panel-content">
				<div class="panel-header">
					<h2 id="panel-title">📝 <?php esc_html_e( 'Edit Publication', 'wisewand-ai-content-writer-seo-content-generator' ); ?></h2>
					<button type="button" class="close-panel">×</button>
				</div>
				<div class="panel-body" id="panel-body">
					<div class="loading-spinner">Loading...</div>
				</div>
			</div>
		</div>
		<?php
	}

	public function enqueue_assets() {
		// IMPORTANT: WordPress.org requires bundling external libraries locally
		// FullCalendar files are located in assets/vendor/fullcalendar/
		wp_enqueue_style(
			'wisewand-fullcalendar',
			WISEWAND_PLUGIN_URL . 'assets/vendor/fullcalendar/fullcalendar.min.css',
			array(),
			'6.1.19'
		);

		wp_enqueue_script(
			'wisewand-fullcalendar',
			WISEWAND_PLUGIN_URL . 'assets/vendor/fullcalendar/fullcalendar.min.js',
			array( 'jquery' ),
			'6.1.19',
			true
		);

		wp_enqueue_style(
			'wisewand-editorial-calendar-admin',
			WISEWAND_PLUGIN_URL . 'assets/css/admin/wisewand-editorial-calendar-admin.css',
			array(),
			WISEWAND_PLUGIN_VERSION
		);

		wp_enqueue_script(
			'wisewand-editorial-calendar-admin',
			WISEWAND_PLUGIN_URL . 'assets/js/admin/wisewand-editorial-calendar-admin.js',
			array( 'jquery', 'wisewand-fullcalendar' ),
			WISEWAND_PLUGIN_VERSION,
			true
		);

		wp_localize_script(
			'wisewand-editorial-calendar-admin',
			'wisewandCalendar',
			array(
				'ajaxUrl'  => admin_url( 'admin-ajax.php' ),
				'nonce'    => wp_create_nonce( 'wisewand_calendar' ),
				'seoNonce' => wp_create_nonce( 'wisewand_seo_real' ),
			)
		);
	}

	// ── AJAX delegations ── Calendar_AJAX_Handler ──────────────────────

	public function ajax_get_calendar_events() {
		$this->ajax_handler->ajax_get_calendar_events();
	}

	public function ajax_update_entry_gmdate() {
		$this->ajax_handler->ajax_update_entry_gmdate();
	}

	public function ajax_get_entry_details() {
		$this->ajax_handler->ajax_get_entry_details();
	}

	public function ajax_save_entry() {
		$this->ajax_handler->ajax_save_entry();
	}

	public function ajax_delete_entry() {
		$this->ajax_handler->ajax_delete_entry();
	}

	public function ajax_generate_instructions_template() {
		$this->ajax_handler->ajax_generate_instructions_template();
	}

	public function ajax_generate_instructions_ai() {
		$this->ajax_handler->ajax_generate_instructions_ai();
	}

	public function ajax_generate_now() {
		$this->ajax_handler->ajax_generate_now();
	}

	public function ajax_import_now() {
		$this->ajax_handler->ajax_import_now();
	}

	public function ajax_get_projects() {
		$this->ajax_handler->ajax_get_projects();
	}

	public function ajax_get_project_details() {
		$this->ajax_handler->ajax_get_project_details();
	}

	public function ajax_get_personas() {
		$this->ajax_handler->ajax_get_personas();
	}

	public function ajax_trigger_manual_cron() {
		$this->ajax_handler->ajax_trigger_manual_cron();
	}
}
