<?php
namespace Sifency\Admin;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Elementor_Native_Exporter
 * Export Elementor-built posts/pages as native Elementor JSON
 */
class Elementor_Native_Exporter {

	public function __construct() {
		add_filter( 'post_row_actions', array( $this, 'add_export_action' ), 10, 2 );
		add_filter( 'page_row_actions', array( $this, 'add_export_action' ), 10, 2 );
		add_action( 'admin_init', array( $this, 'handle_export_request' ) );
	}

	/**
	 * Check if post is editable & built with Elementor
	 */
	protected function is_elementor_post( $post_id ) {
		return (
			current_user_can( 'edit_post', $post_id ) &&
			get_post_meta( $post_id, '_elementor_edit_mode', true ) === 'builder'
		);
	}

	/**
	 * Generate export URL (Reusable anywhere)
	 */
	public function get_export_url( $post_id ) {
		if ( ! $this->is_elementor_post( $post_id ) ) {
			return false;
		}

		return wp_nonce_url(
			add_query_arg(
				array(
					'action'  => 'sifency_elementor_native_export',
					'post_id' => $post_id,
				),
				admin_url( 'admin.php' )
			),
			'sifency_el_export_' . $post_id
		);
	}

	/**
	 * Add export link to post/page row actions
	 */
	public function add_export_action( $actions, $post ) {
		$url = $this->get_export_url( $post->ID );

		if ( ! $url ) {
			return $actions;
		}

		$actions['sifency_el_export'] = sprintf(
			'<a href="%s" style="color:#93003c;font-weight:bold;">%s</a>',
			esc_url( $url ),
			esc_html__( 'Export Elementor JSON', 'sifency-addons' )
		);

		return $actions;
	}

	/**
	 * Process export request
	 */
	public function handle_export_request() {

		if (
			! isset( $_GET['action'] ) ||
			$_GET['action'] !== 'sifency_elementor_native_export'
		) {
			return;
		}

		$post_id = isset( $_GET['post_id'] ) ? absint( $_GET['post_id'] ) : 0;

		check_admin_referer( 'sifency_el_export_' . $post_id );

		if ( ! $this->is_elementor_post( $post_id ) ) {
			wp_die( esc_html__( 'Permission denied or invalid Elementor post.', 'sifency-addons' ) );
		}

		$raw_data = get_post_meta( $post_id, '_elementor_data', true );
		$content  = json_decode( $raw_data, true );

		if ( empty( $content ) ) {
			wp_die( esc_html__( 'No valid Elementor data found.', 'sifency-addons' ) );
		}

		$page_settings = get_post_meta( $post_id, '_elementor_page_settings', true );
		if ( empty( $page_settings ) ) {
			$page_settings = array();
		}

		$export_data = array(
			'content'       => $content,
			'page_settings' => $page_settings,
			'version'       => defined( 'ELEMENTOR_VERSION' ) ? ELEMENTOR_VERSION : '3.0.0',
			'type'          => 'page',
			'title'         => get_the_title( $post_id ),
			'source'        => 'sifency-exporter',
		);

		$filename = sprintf(
			'elementor-template-%d-%s.json',
			$post_id,
			gmdate( 'Y-m-d' )
		);

		nocache_headers();
		header( 'Content-Type: application/json; charset=utf-8' );
		header( 'Content-Disposition: attachment; filename=' . $filename );

		echo wp_json_encode( $export_data );
		exit;
	}
}
