<?php
/**
 * Import/Export Functions
 *
 * Data type configurations for import/export operations
 *
 * @package WC_Affiliate
 * @since 3.0.0
 */

/**
 * Get available affiliate data types for export/import operations.
 *
 * @return array Array of data type configurations.
 */
function wca_get_affiliate_data_types(): array {
	return apply_filters(
		'wc_affiliate_data_types',
		array(
			'affiliates'   => array(
				'name'        => __( 'Affiliates', 'wc-affiliate' ),
				'description' => __( 'Affiliate user data and settings', 'wc-affiliate' ),
				'model'       => 'Affiliate',
			),
			'visits'       => array(
				'name'        => __( 'Visits', 'wc-affiliate' ),
				'description' => __( 'Affiliate visit tracking data', 'wc-affiliate' ),
				'model'       => 'Visit',
			),
			'referrals'    => array(
				'name'        => __( 'Referrals', 'wc-affiliate' ),
				'description' => __( 'Referral and commission data', 'wc-affiliate' ),
				'model'       => 'Referral',
			),
			'payables'     => array(
				'name'        => __( 'Payables', 'wc-affiliate' ),
				'description' => __( 'Approved and paid referral data', 'wc-affiliate' ),
				'model'       => 'Referral',
			),
			'transactions' => array(
				'name'        => __( 'Transactions', 'wc-affiliate' ),
				'description' => __( 'Payout transaction data', 'wc-affiliate' ),
				'model'       => 'Transaction',
			),
			'settings'     => array(
				'name'        => __( 'Settings', 'wc-affiliate' ),
				'description' => __( 'Plugin configuration and settings', 'wc-affiliate' ),
				'model'       => 'Settings',
			),
			'complete'     => array(
				'name'        => __( 'Complete Data', 'wc-affiliate' ),
				'description' => __( 'All affiliate program data', 'wc-affiliate' ),
				'model'       => 'All',
			),
		)
	);
}

/**
 * Get available export types for affiliate data.
 *
 * @return array Array of export type configurations.
 */
function wca_get_export_types(): array {
	return wca_get_affiliate_data_types();
}

/**
 * Get available import types for affiliate data.
 *
 * @return array Array of import type configurations with validation fields.
 */
function wca_get_import_types(): array {
	$base_types = wca_get_affiliate_data_types();

	// Add import-specific fields.
	$import_types = array();

	foreach ( $base_types as $key => $type ) {
		$import_types[ $key ] = $type;

		// Add required and optional fields based on type.
		switch ( $key ) {
			case 'affiliates':
				$import_types[ $key ]['required_fields'] = array( 'user_email', 'status' );
				$import_types[ $key ]['optional_fields'] = array(
					'user_login',
					'display_name',
					'commission_type',
					'commission_amount',
					'payment_method',
				);
				break;

			case 'visits':
				$import_types[ $key ]['required_fields'] = array( 'affiliate_id', 'page_url', 'visited_at' );
				$import_types[ $key ]['optional_fields'] = array( 'referrer_url', 'campaign', 'ip_address' );
				break;

			case 'referrals':
				$import_types[ $key ]['required_fields'] = array(
					'affiliate_id',
					'order_id',
					'commission',
					'order_total',
				);
				$import_types[ $key ]['optional_fields'] = array(
					'type',
					'visit_id',
					'products',
					'payment_status',
					'transaction_id',
				);
				break;

			case 'payables':
				$import_types[ $key ]['required_fields'] = array(
					'affiliate_id',
					'order_id',
					'commission',
					'order_total',
				);
				$import_types[ $key ]['optional_fields'] = array( 'type', 'visit_id', 'products', 'transaction_id' );
				break;

			case 'transactions':
				$import_types[ $key ]['required_fields'] = array( 'affiliate_id', 'amount', 'payment_method' );
				$import_types[ $key ]['optional_fields'] = array( 'txn_id', 'status', 'requested_at', 'processed_at' );
				break;

			case 'settings':
				$import_types[ $key ]['required_fields'] = array();
				$import_types[ $key ]['optional_fields'] = array( 'basic', 'advanced', 'email', 'payout', 'messages' );
				break;

			case 'complete':
				// Complete import doesn't have specific field requirements.
				$import_types[ $key ]['required_fields'] = array();
				$import_types[ $key ]['optional_fields'] = array();
				break;
		}
	}

	return apply_filters( 'wc_affiliate_import_types', $import_types );
}
