/**
 * @file assets/js/adminjs/admin-coming-soon.js
 * Coming Soon checkbox and Preorder Link row automation for the Book Editor; FREE-tier visual-only mode.
 * @package MyBookShowroom
 * @since   1.0.0
 */

( function ( $, undefined ) {
	'use strict';
	var __ = wp.i18n.__;
	var escHtml = MBS_AdminUtils.escHtml;

	var MBS_ComingSoon = {

		cfg: window.MBS_ComingSoonData || { isPro: false },

		init: function () {
			this.bindDate();
			this.bindCheckbox();
			this.bindPreorderUrl();
			this.evaluate(); // enforce correct state for already-saved data on page load
		},

		// ── Date helpers ────────────────────────────────────────────────────────

		isFuture: function ( dateStr ) {
			if ( ! dateStr ) { return false; }
			// Compare calendar days (ignore time) using local midnight.
			var d     = new Date( dateStr + 'T00:00:00' );
			var today = new Date( new Date().toDateString() );
			return d > today;
		},

		isPast: function ( dateStr ) {
			if ( ! dateStr ) { return false; }
			var d     = new Date( dateStr + 'T00:00:00' );
			var today = new Date( new Date().toDateString() );
			return d < today;
		},

		// ── Core state evaluator ────────────────────────────────────────────────

		/**
		 * Read the current date and update the Coming Soon checkbox state and
		 * the Preorder Link row visibility accordingly.
		 */
		evaluate: function () {
			var dateStr = $( '#mbs_publication_date' ).val();
			var $cb     = $( '#mbs_coming_soon' );

			if ( this.isPast( dateStr ) ) {
				// Past date: force unchecked. For PRO also disable so the user
				// can't check a "Coming Soon" that's already happened.
				$cb.prop( 'checked', false );
				if ( this.cfg.isPro ) {
					$cb.prop( 'disabled', true );
					this.setLabel( __( 'Publication date is in the past — Coming Soon is not applicable.', 'my-book-showroom' ), 0.5 );
				}

			} else if ( this.isFuture( dateStr ) ) {
				// Future date: auto-check (both tiers).
				$cb.prop( 'checked', true );
				if ( this.cfg.isPro ) {
					$cb.prop( 'disabled', false );
					this.setLabel( '', 1 );
				}
				// FREE: checkbox stays disabled (PHP renders it that way); we
				// only set .checked so the user sees the PRO feature indicator.

			} else {
				// No date: restore normal state for PRO, leave FREE as-is.
				if ( this.cfg.isPro ) {
					$cb.prop( 'disabled', false );
					this.setLabel( '', 1 );
				}
			}

			this.updatePreorderRow();
		},

		// ── Preorder Link row ────────────────────────────────────────────────────

		updatePreorderRow: function () {
			var checked = $( '#mbs_coming_soon' ).prop( 'checked' );
			if ( this.cfg.isPro && checked ) {
				$( '#mbs_preorder_row' ).slideDown( 150 );
			} else {
				$( '#mbs_preorder_row' ).slideUp( 150 );
			}
		},

		// ── Preorder URL validation ──────────────────────────────────────────────

		bindPreorderUrl: function () {
			$( '#mbs_preorder_link' ).on( 'blur', function () {
				var val     = $( this ).val().trim();
				var $notice = $( '#mbs-preorder-url-notice' );

				if ( ! val ) {
					$notice.remove();
					return;
				}

				var valid = false;
				try {
					var u = new URL( val );
					valid = ( u.protocol === 'https:' || u.protocol === 'http:' );
				} catch ( e ) {
					valid = false;
				}

				if ( ! valid ) {
					if ( ! $notice.length ) {
						$( this ).after(
							'<p id="mbs-preorder-url-notice" class="description" ' +
							'style="color:#d63638;margin-top:4px;">' +
							escHtml( __( 'Please enter a valid URL (e.g. https://example.com/preorder).', 'my-book-showroom' ) ) +
							'</p>'
						);
					}
				} else {
					$notice.remove();
				}
			} );
		},

		// ── Event bindings ───────────────────────────────────────────────────────

		bindDate: function () {
			$( '#mbs_publication_date' ).on( 'change', function () {
				MBS_ComingSoon.evaluate();
			} );
		},

		bindCheckbox: function () {
			$( '#mbs_coming_soon' ).on( 'change', function () {
				MBS_ComingSoon.updatePreorderRow();
			} );
		},

		// ── Label helper ─────────────────────────────────────────────────────────

		setLabel: function ( title, opacity ) {
			var $label = $( 'label[for="mbs_coming_soon"]' );
			if ( title ) {
				$label.attr( 'title', title );
			} else {
				$label.removeAttr( 'title' );
			}
			$label.css( 'opacity', opacity );
		}
	};

	$( document ).ready( function () {
		MBS_ComingSoon.init();
	} );

} )( jQuery );
