<?php
/**
 * Column registry for supported export entities.
 *
 * @package RawTypeOE
 */

namespace RawTypeOE\Free;

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Provides available columns per entity.
 */
class Column_Registry {

	/**
	 * Group label map for display purposes.
	 *
	 * @var array
	 */
	private static $group_labels = array(
		'order'         => 'Order',
		'financial'     => 'Financial',
		'billing'       => 'Billing',
		'shipping'      => 'Shipping',
		'product'       => 'Product',
		'pricing'       => 'Pricing',
		'inventory'     => 'Inventory',
		'taxonomy'      => 'Taxonomy',
		'dimensions'    => 'Dimensions',
		'media'         => 'Media',
		'customer'      => 'Customer',
		'metrics'       => 'Metrics',
		'custom_fields' => 'Custom fields',
	);

	/**
	 * Get columns by entity slug.
	 *
	 * @param string $entity Entity slug.
	 * @return array
	 */
	public function get_columns( $entity ) {
		$entity  = sanitize_key( $entity );
		$columns = array();

		if ( 'orders' === $entity ) {
			$columns = $this->get_orders_columns();
		} elseif ( 'products' === $entity ) {
			$columns = $this->get_products_columns();
		} elseif ( 'customers' === $entity ) {
			$columns = $this->get_customers_columns();
		} elseif ( 'cross-entity' === $entity ) {
			$columns = $this->get_cross_entity_columns();
		}

		/**
		 * Filter available columns for an entity.
		 *
		 * @param array  $columns Column definitions.
		 * @param string $entity  Entity slug.
		 */
		return apply_filters( "rawtype_oe_columns_{$entity}", $columns, $entity );
	}

	/**
	 * Get the human-readable label for a group key.
	 *
	 * @param string $group_key Group key.
	 * @return string
	 */
	public static function get_group_label( $group_key ) {
		return isset( self::$group_labels[ $group_key ] ) ? self::$group_labels[ $group_key ] : ucfirst( $group_key );
	}

	/**
	 * Orders entity columns.
	 *
	 * @return array
	 */
	private function get_orders_columns() {
		return array(
			$this->make_column( 'order_id', __( 'Order ID', 'rawtype-order-exporter-woocommerce' ), 'order', true ),
			$this->make_column( 'order_number', __( 'Order number', 'rawtype-order-exporter-woocommerce' ), 'order', true ),
			$this->make_column( 'status', __( 'Status', 'rawtype-order-exporter-woocommerce' ), 'order', true ),
			$this->make_column( 'date_created', __( 'Created date', 'rawtype-order-exporter-woocommerce' ), 'order', true ),
			$this->make_column( 'total', __( 'Total', 'rawtype-order-exporter-woocommerce' ), 'financial', true ),
			$this->make_column( 'currency', __( 'Currency', 'rawtype-order-exporter-woocommerce' ), 'financial' ),
			$this->make_column( 'payment_method', __( 'Payment method', 'rawtype-order-exporter-woocommerce' ), 'financial' ),
			$this->make_column( 'billing_first_name', __( 'Billing first name', 'rawtype-order-exporter-woocommerce' ), 'billing' ),
			$this->make_column( 'billing_last_name', __( 'Billing last name', 'rawtype-order-exporter-woocommerce' ), 'billing' ),
			$this->make_column( 'billing_email', __( 'Billing email', 'rawtype-order-exporter-woocommerce' ), 'billing', true ),
			$this->make_column( 'billing_phone', __( 'Billing phone', 'rawtype-order-exporter-woocommerce' ), 'billing' ),
			$this->make_column( 'billing_address_1', __( 'Billing address 1', 'rawtype-order-exporter-woocommerce' ), 'billing' ),
			$this->make_column( 'billing_city', __( 'Billing city', 'rawtype-order-exporter-woocommerce' ), 'billing' ),
			$this->make_column( 'billing_state', __( 'Billing state', 'rawtype-order-exporter-woocommerce' ), 'billing' ),
			$this->make_column( 'billing_postcode', __( 'Billing postcode', 'rawtype-order-exporter-woocommerce' ), 'billing' ),
			$this->make_column( 'billing_country', __( 'Billing country', 'rawtype-order-exporter-woocommerce' ), 'billing' ),
			$this->make_column( 'shipping_first_name', __( 'Shipping first name', 'rawtype-order-exporter-woocommerce' ), 'shipping' ),
			$this->make_column( 'shipping_last_name', __( 'Shipping last name', 'rawtype-order-exporter-woocommerce' ), 'shipping' ),
			$this->make_column( 'shipping_address_1', __( 'Shipping address 1', 'rawtype-order-exporter-woocommerce' ), 'shipping' ),
			$this->make_column( 'shipping_city', __( 'Shipping city', 'rawtype-order-exporter-woocommerce' ), 'shipping' ),
			$this->make_column( 'shipping_state', __( 'Shipping state', 'rawtype-order-exporter-woocommerce' ), 'shipping' ),
			$this->make_column( 'shipping_postcode', __( 'Shipping postcode', 'rawtype-order-exporter-woocommerce' ), 'shipping' ),
			$this->make_column( 'shipping_country', __( 'Shipping country', 'rawtype-order-exporter-woocommerce' ), 'shipping' ),
		);
	}

	/**
	 * Products entity columns.
	 *
	 * @return array
	 */
	private function get_products_columns() {
		return array(
			$this->make_column( 'product_id', __( 'Product ID', 'rawtype-order-exporter-woocommerce' ), 'product', true ),
			$this->make_column( 'name', __( 'Name', 'rawtype-order-exporter-woocommerce' ), 'product', true ),
			$this->make_column( 'slug', __( 'Slug', 'rawtype-order-exporter-woocommerce' ), 'product' ),
			$this->make_column( 'type', __( 'Type', 'rawtype-order-exporter-woocommerce' ), 'product' ),
			$this->make_column( 'status', __( 'Status', 'rawtype-order-exporter-woocommerce' ), 'product', true ),
			$this->make_column( 'sku', __( 'SKU', 'rawtype-order-exporter-woocommerce' ), 'product', true ),
			$this->make_column( 'price', __( 'Price', 'rawtype-order-exporter-woocommerce' ), 'pricing', true ),
			$this->make_column( 'regular_price', __( 'Regular price', 'rawtype-order-exporter-woocommerce' ), 'pricing' ),
			$this->make_column( 'sale_price', __( 'Sale price', 'rawtype-order-exporter-woocommerce' ), 'pricing' ),
			$this->make_column( 'stock_quantity', __( 'Stock quantity', 'rawtype-order-exporter-woocommerce' ), 'inventory', true ),
			$this->make_column( 'stock_status', __( 'Stock status', 'rawtype-order-exporter-woocommerce' ), 'inventory' ),
			$this->make_column( 'categories', __( 'Categories', 'rawtype-order-exporter-woocommerce' ), 'taxonomy' ),
			$this->make_column( 'tags', __( 'Tags', 'rawtype-order-exporter-woocommerce' ), 'taxonomy' ),
			$this->make_column( 'weight', __( 'Weight', 'rawtype-order-exporter-woocommerce' ), 'dimensions' ),
			$this->make_column( 'length', __( 'Length', 'rawtype-order-exporter-woocommerce' ), 'dimensions' ),
			$this->make_column( 'width', __( 'Width', 'rawtype-order-exporter-woocommerce' ), 'dimensions' ),
			$this->make_column( 'height', __( 'Height', 'rawtype-order-exporter-woocommerce' ), 'dimensions' ),
			$this->make_column( 'image_url', __( 'Image URL', 'rawtype-order-exporter-woocommerce' ), 'media' ),
		);
	}

	/**
	 * Customers entity columns.
	 *
	 * @return array
	 */
	private function get_customers_columns() {
		return array(
			$this->make_column( 'customer_id', __( 'Customer ID', 'rawtype-order-exporter-woocommerce' ), 'customer', true ),
			$this->make_column( 'username', __( 'Username', 'rawtype-order-exporter-woocommerce' ), 'customer' ),
			$this->make_column( 'email', __( 'Email', 'rawtype-order-exporter-woocommerce' ), 'customer', true ),
			$this->make_column( 'first_name', __( 'First name', 'rawtype-order-exporter-woocommerce' ), 'customer', true ),
			$this->make_column( 'last_name', __( 'Last name', 'rawtype-order-exporter-woocommerce' ), 'customer', true ),
			$this->make_column( 'date_registered', __( 'Date registered', 'rawtype-order-exporter-woocommerce' ), 'customer', true ),
			$this->make_column( 'billing_first_name', __( 'Billing first name', 'rawtype-order-exporter-woocommerce' ), 'billing' ),
			$this->make_column( 'billing_last_name', __( 'Billing last name', 'rawtype-order-exporter-woocommerce' ), 'billing' ),
			$this->make_column( 'billing_email', __( 'Billing email', 'rawtype-order-exporter-woocommerce' ), 'billing' ),
			$this->make_column( 'billing_phone', __( 'Billing phone', 'rawtype-order-exporter-woocommerce' ), 'billing' ),
			$this->make_column( 'billing_address_1', __( 'Billing address 1', 'rawtype-order-exporter-woocommerce' ), 'billing' ),
			$this->make_column( 'billing_city', __( 'Billing city', 'rawtype-order-exporter-woocommerce' ), 'billing' ),
			$this->make_column( 'billing_state', __( 'Billing state', 'rawtype-order-exporter-woocommerce' ), 'billing' ),
			$this->make_column( 'billing_postcode', __( 'Billing postcode', 'rawtype-order-exporter-woocommerce' ), 'billing' ),
			$this->make_column( 'billing_country', __( 'Billing country', 'rawtype-order-exporter-woocommerce' ), 'billing' ),
			$this->make_column( 'shipping_first_name', __( 'Shipping first name', 'rawtype-order-exporter-woocommerce' ), 'shipping' ),
			$this->make_column( 'shipping_last_name', __( 'Shipping last name', 'rawtype-order-exporter-woocommerce' ), 'shipping' ),
			$this->make_column( 'shipping_address_1', __( 'Shipping address 1', 'rawtype-order-exporter-woocommerce' ), 'shipping' ),
			$this->make_column( 'shipping_city', __( 'Shipping city', 'rawtype-order-exporter-woocommerce' ), 'shipping' ),
			$this->make_column( 'shipping_state', __( 'Shipping state', 'rawtype-order-exporter-woocommerce' ), 'shipping' ),
			$this->make_column( 'shipping_postcode', __( 'Shipping postcode', 'rawtype-order-exporter-woocommerce' ), 'shipping' ),
			$this->make_column( 'shipping_country', __( 'Shipping country', 'rawtype-order-exporter-woocommerce' ), 'shipping' ),
			$this->make_column( 'orders_count', __( 'Orders count', 'rawtype-order-exporter-woocommerce' ), 'metrics', true ),
			$this->make_column( 'total_spent', __( 'Total spent', 'rawtype-order-exporter-woocommerce' ), 'metrics', true ),
		);
	}

	/**
	 * Build a normalized column definition.
	 *
	 * @param string $key     Column key.
	 * @param string $label   Human-readable label.
	 * @param string $group   Group key.
	 * @param bool   $is_default Whether selected by default.
	 * @return array
	 */
	private function make_column( $key, $label, $group, $is_default = false ) {
		return array(
			'key'     => sanitize_key( $key ),
			'label'   => sanitize_text_field( $label ),
			'group'   => sanitize_key( $group ),
			'default' => (bool) $is_default,
		);
	}

	/**
	 * Cross-entity columns grouped by customer/order/product.
	 *
	 * @return array
	 */
	private function get_cross_entity_columns() {
		return array(
			array(
				'key'          => 'customer_name',
				'label'        => __( 'Customer name', 'rawtype-order-exporter-woocommerce' ),
				'group'        => 'customer',
				'default'      => true,
				'entity'       => 'customers',
				'entity_label' => __( 'Customer', 'rawtype-order-exporter-woocommerce' ),
			),
			array(
				'key'          => 'customer_email',
				'label'        => __( 'Customer email', 'rawtype-order-exporter-woocommerce' ),
				'group'        => 'customer',
				'default'      => true,
				'entity'       => 'customers',
				'entity_label' => __( 'Customer', 'rawtype-order-exporter-woocommerce' ),
			),
			array(
				'key'          => 'customer_total_spent',
				'label'        => __( 'Customer total spent', 'rawtype-order-exporter-woocommerce' ),
				'group'        => 'customer',
				'default'      => false,
				'entity'       => 'customers',
				'entity_label' => __( 'Customer', 'rawtype-order-exporter-woocommerce' ),
			),
			array(
				'key'          => 'customer_orders_count',
				'label'        => __( 'Customer orders count', 'rawtype-order-exporter-woocommerce' ),
				'group'        => 'customer',
				'default'      => false,
				'entity'       => 'customers',
				'entity_label' => __( 'Customer', 'rawtype-order-exporter-woocommerce' ),
			),
			array(
				'key'          => 'order_id',
				'label'        => __( 'Order ID', 'rawtype-order-exporter-woocommerce' ),
				'group'        => 'order',
				'default'      => true,
				'entity'       => 'orders',
				'entity_label' => __( 'Order', 'rawtype-order-exporter-woocommerce' ),
			),
			array(
				'key'          => 'order_status',
				'label'        => __( 'Order status', 'rawtype-order-exporter-woocommerce' ),
				'group'        => 'order',
				'default'      => true,
				'entity'       => 'orders',
				'entity_label' => __( 'Order', 'rawtype-order-exporter-woocommerce' ),
			),
			array(
				'key'          => 'order_total',
				'label'        => __( 'Order total', 'rawtype-order-exporter-woocommerce' ),
				'group'        => 'order',
				'default'      => true,
				'entity'       => 'orders',
				'entity_label' => __( 'Order', 'rawtype-order-exporter-woocommerce' ),
			),
			array(
				'key'          => 'order_date',
				'label'        => __( 'Order date', 'rawtype-order-exporter-woocommerce' ),
				'group'        => 'order',
				'default'      => true,
				'entity'       => 'orders',
				'entity_label' => __( 'Order', 'rawtype-order-exporter-woocommerce' ),
			),
			array(
				'key'          => 'product_name',
				'label'        => __( 'Product name', 'rawtype-order-exporter-woocommerce' ),
				'group'        => 'product',
				'default'      => true,
				'entity'       => 'products',
				'entity_label' => __( 'Product', 'rawtype-order-exporter-woocommerce' ),
			),
			array(
				'key'          => 'product_sku',
				'label'        => __( 'Product SKU', 'rawtype-order-exporter-woocommerce' ),
				'group'        => 'product',
				'default'      => true,
				'entity'       => 'products',
				'entity_label' => __( 'Product', 'rawtype-order-exporter-woocommerce' ),
			),
			array(
				'key'          => 'product_category',
				'label'        => __( 'Product category', 'rawtype-order-exporter-woocommerce' ),
				'group'        => 'product',
				'default'      => false,
				'entity'       => 'products',
				'entity_label' => __( 'Product', 'rawtype-order-exporter-woocommerce' ),
			),
			array(
				'key'          => 'product_price',
				'label'        => __( 'Product price', 'rawtype-order-exporter-woocommerce' ),
				'group'        => 'product',
				'default'      => false,
				'entity'       => 'products',
				'entity_label' => __( 'Product', 'rawtype-order-exporter-woocommerce' ),
			),
		);
	}
}
