/**
 * Database Management JavaScript
 * 
 * Handles all interactions for the AskAny Database Management page
 */

jQuery(document).ready(function($) {
	
	// Pro status and restrictions
	window.askanyDbMgmt = window.askanyDbMgmt || {};
	window.askanyDbMgmt.isPro = window.askanyDbMgmt.isPro || false;
	
	// Check if user has PRO access for database operations
	function checkProAccess(actionName) {
		if (!window.askanyDbMgmt.isPro) {
			showProModal(actionName);
			return false;
		}
		return true;
	}
	
	// Show PRO upgrade modal
	function showProModal(feature) {
		if (typeof AskanyPro !== 'undefined') {
			AskanyPro.openModal({
				title: askanyDbMgmt.strings.upgradeToProTitle || 'Upgrade to Pro',
				description: askanyDbMgmt.strings.upgradeToProDesc || 'Unlock advanced database management features',
				features: [
					askanyDbMgmt.strings.proFeature1 || 'Advanced database operations',
					askanyDbMgmt.strings.proFeature2 || 'Data export & import',
					askanyDbMgmt.strings.proFeature3 || 'Custom date filtering',
					askanyDbMgmt.strings.proFeature4 || 'Bulk data cleanup',
					askanyDbMgmt.strings.proFeature5 || 'Priority support'
				]
			});
		} else {
			// Fallback if AskanyPro modal is not available
			alert(askanyDbMgmt.strings.proRequired || 'This feature requires AskAny Pro. Please upgrade to access advanced database management.');
		}
	}
	
	// Handle PRO modal open button clicks
	$(document).on('click', '.askany-open-pro-modal', function(e) {
		e.preventDefault();
		showProModal('database-management');
	});
	
	// Set default dates if not already set
	function setDefaultDates() {
		var today = new Date();
		var thirtyDaysAgo = new Date();
		thirtyDaysAgo.setDate(today.getDate() - 30);
		
		var todayStr = today.toISOString().split('T')[0];
		var thirtyDaysAgoStr = thirtyDaysAgo.toISOString().split('T')[0];
		
		if (!$('#date-from').val()) {
			$('#date-from').val(thirtyDaysAgoStr);
		}
		if (!$('#date-to').val()) {
			$('#date-to').val(todayStr);
		}
	}
	
	// Initialize default dates
	setDefaultDates();
	
	// Modal functionality
	function showModal(title, message, requireConfirm = false, callback = null) {
		$('#modal-title').text(title);
		$('#modal-message').html(message);
		
		if (requireConfirm) {
			$('.confirmation-input').show();
			$('#confirm-text').val('').focus();
			$('#modal-confirm').prop('disabled', true);
		} else {
			$('.confirmation-input').hide();
			$('#modal-confirm').prop('disabled', false);
		}
		
		$('#askany-confirm-modal').show();
		
		// Store callback for later use
		$('#askany-confirm-modal').data('callback', callback);
	}
	
	function hideModal() {
		$('#askany-confirm-modal').hide();
		$('#confirm-text').val('');
	}
	
	// Confirmation text validation
	$('#confirm-text').on('input', function() {
		const isValid = $(this).val().toUpperCase() === 'CONFIRM';
		$('#modal-confirm').prop('disabled', !isValid);
	});
	
	// Modal event handlers
	$('.modal-close, #modal-cancel').on('click', hideModal);
	
	$('#modal-confirm').on('click', function() {
		const callback = $('#askany-confirm-modal').data('callback');
		if (callback && typeof callback === 'function') {
			callback();
		}
		hideModal();
	});
	
	// Show results
	function showResults(message, isSuccess = true) {
		$('#operation-results')
			.removeClass('success error')
			.addClass(isSuccess ? 'success' : 'error')
			.html(message);
		$('#results-section').show();
		
		// Scroll to results
		$('html, body').animate({
			scrollTop: $('#results-section').offset().top - 100
		}, 500);
	}
	
	// Quick cleanup actions
	$('#cleanup-10-days').on('click', function() {
		if (!checkProAccess('cleanup-10-days')) return;
		
		showModal(
			askanyDbMgmt.strings.clean10Days,
			askanyDbMgmt.strings.clean10DaysConfirm,
			true,
			function() {
				performCleanup(10);
			}
		);
	});
	
	
});