<?php
/**
 * Privacy Data Exporter
 *
 * Implements WordPress privacy tools for data export (GDPR Article 15).
 *
 * @package DonorCore\Privacy
 */

namespace DonorCore\Privacy;

defined( 'ABSPATH' ) || exit;

/**
 * Privacy Exporter Class
 */
class Privacy_Exporter {

	/**
	 * Initialize hooks.
	 */
	public function init() {
		add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_exporters' ) );
	}

	/**
	 * Register data exporters.
	 *
	 * @param array $exporters Existing exporters.
	 * @return array
	 */
	public function register_exporters( $exporters ) {
		$exporters['donor-core-donations'] = array(
			'exporter_friendly_name' => __( 'DonorCore Donations', 'donorcore-donations' ),
			'callback'               => array( $this, 'donations_exporter' ),
		);

		$exporters['donor-core-analytics'] = array(
			'exporter_friendly_name' => __( 'DonorCore Analytics', 'donorcore-donations' ),
			'callback'               => array( $this, 'analytics_exporter' ),
		);

		return $exporters;
	}

	/**
	 * Export donation logs for an email address.
	 *
	 * @param string $email_address Email address to export data for.
	 * @param int    $page          Page number.
	 * @return array Export data.
	 */
	public function donations_exporter( $email_address, $page = 1 ) {
		$donation_repo = new \DonorCore\Database\Donation_Log_Repository();
		$donations     = $donation_repo->get_by_donor_email( $email_address );

		$export_items = array();

		foreach ( $donations as $donation ) {
			$item_id = "donation-{$donation['id']}";

			// Get campaign name
			$campaign_name = __( 'General Donation', 'donorcore-donations' );
			if ( ! empty( $donation['campaign_id'] ) ) {
				$campaign = get_post( $donation['campaign_id'] );
				if ( $campaign ) {
					$campaign_name = $campaign->post_title;
				}
			}

			$data = array(
				array(
					'name'  => __( 'Donation ID', 'donorcore-donations' ),
					'value' => $donation['id'],
				),
				array(
					'name'  => __( 'Order ID', 'donorcore-donations' ),
					'value' => $donation['order_id'],
				),
				array(
					'name'  => __( 'Campaign', 'donorcore-donations' ),
					'value' => $campaign_name,
				),
				array(
					'name'  => __( 'Amount', 'donorcore-donations' ),
					'value' => $donation['amount'] . ' ' . $donation['currency'],
				),
				array(
					'name'  => __( 'Type', 'donorcore-donations' ),
					'value' => $donation['type'],
				),
				array(
					'name'  => __( 'Payment Gateway', 'donorcore-donations' ),
					'value' => $donation['gateway'],
				),
				array(
					'name'  => __( 'Date', 'donorcore-donations' ),
					'value' => $donation['created_at'],
				),
			);

			$export_items[] = array(
				'group_id'    => 'donor-core-donations',
				'group_label' => __( 'Donations', 'donorcore-donations' ),
				'item_id'     => $item_id,
				'data'        => $data,
			);
		}

		return array(
			'data' => $export_items,
			'done' => true,
		);
	}

	/**
	 * Export donor analytics for an email address.
	 *
	 * @param string $email_address Email address to export data for.
	 * @param int    $page          Page number.
	 * @return array Export data.
	 */
	public function analytics_exporter( $email_address, $page = 1 ) {
		$analytics_repo = new \DonorCore\Database\Donor_Analytics_Repository();
		$analytics      = $analytics_repo->get_by_email( $email_address );

		$export_items = array();

		if ( $analytics ) {
			$data = array(
				array(
					'name'  => __( 'Email Address', 'donorcore-donations' ),
					'value' => $analytics->donor_email,
				),
				array(
					'name'  => __( 'Total Lifetime Value', 'donorcore-donations' ),
					'value' => wc_price( $analytics->total_lifetime_value ),
				),
				array(
					'name'  => __( 'Total Donations', 'donorcore-donations' ),
					'value' => $analytics->donation_count,
				),
				array(
					'name'  => __( 'First Donation Date', 'donorcore-donations' ),
					'value' => $analytics->first_donation_date ? $analytics->first_donation_date : __( 'N/A', 'donorcore-donations' ),
				),
				array(
					'name'  => __( 'Last Donation Date', 'donorcore-donations' ),
					'value' => $analytics->last_donation_date ? $analytics->last_donation_date : __( 'N/A', 'donorcore-donations' ),
				),
			);

			$export_items[] = array(
				'group_id'    => 'donor-core-analytics',
				'group_label' => __( 'Donor Analytics', 'donorcore-donations' ),
				'item_id'     => 'donor-analytics-' . sanitize_title( $email_address ),
				'data'        => $data,
			);
		}

		return array(
			'data' => $export_items,
			'done' => true,
		);
	}
}
