<?php
/**
 * Class ShipmentTemplateListColumns
 *
 * @package DhlPolska\ShipmentTemplates
 */

namespace DhlPolska\ShipmentTemplates;

use DhlPolskaVendor\Octolize\DhlPolska\API\WebApi\Struct\Services\AgeverService;
use DhlPolskaVendor\Octolize\DhlPolska\API\WebApi\Struct\Services\NadSobotaService;
use DhlPolskaVendor\Octolize\DhlPolska\API\WebApi\Struct\Services\NstService;
use DhlPolskaVendor\Octolize\DhlPolska\API\WebApi\Struct\Services\PdiService;
use DhlPolskaVendor\Octolize\DhlPolska\API\WebApi\Struct\Services\PodService;
use DhlPolskaVendor\Octolize\DhlPolska\API\WebApi\Struct\Services\RodService;
use DhlPolskaVendor\Octolize\DhlPolska\API\WebApi\Struct\Services\Service1722;
use DhlPolskaVendor\Octolize\DhlPolska\API\WebApi\Struct\Services\SobotaService;
use DhlPolska\Shipment\ShipmentData;
use DhlPolska\Shipment\ShipmentTypes;
use DhlPolska\WooCommerceSettings\Templates\TemplatesRepository;
use DhlPolskaVendor\WPDesk\PluginBuilder\Plugin\Hookable;

/**
 * Customize shipment templates list columns.
 */
class ShipmentTemplateListColumns implements Hookable {
	private const COLUMN_TYPE       = 'dhl_polska_template_type';
	private const COLUMN_WEIGHT     = 'dhl_polska_template_weight';
	private const COLUMN_DIMENSIONS = 'dhl_polska_template_dimensions';
	private const COLUMN_SERVICES   = 'dhl_polska_template_services';
	private const SERVICE_CODE_MAP  = [
		ShipmentData::TEMPLATE_SERVICE_1722        => Service1722::TYPE,
		ShipmentData::TEMPLATE_SERVICE_SOBOTA      => SobotaService::TYPE,
		ShipmentData::TEMPLATE_SERVICE_NAD_SOBOTA  => NadSobotaService::TYPE,
		ShipmentData::TEMPLATE_SERVICE_INSURANCE   => 'UBEZP',
		ShipmentData::TEMPLATE_SERVICE_COD         => 'COD',
		ShipmentData::TEMPLATE_SERVICE_PDI         => PdiService::TYPE,
		ShipmentData::TEMPLATE_SERVICE_ROD         => RodService::TYPE,
		ShipmentData::TEMPLATE_SERVICE_POD         => PodService::TYPE,
		ShipmentData::TEMPLATE_SERVICE_AGEVER      => AgeverService::TYPE,
		ShipmentData::TEMPLATE_SERVICE_NST         => NstService::TYPE,
		ShipmentData::TEMPLATE_SERVICE_EURO_RETURN => 'EURO RETURN',
	];

	private TemplatesRepository $templates_repository;

	private ShipmentTypes $shipment_types;

	public function __construct( TemplatesRepository $templates_repository, ShipmentTypes $shipment_types ) {
		$this->templates_repository = $templates_repository;
		$this->shipment_types       = $shipment_types;
	}

	/**
	 * Hooks.
	 */
	public function hooks() {
		add_filter( 'manage_' . ShipmentTemplatePostType::POST_TYPE . '_posts_columns', [ $this, 'add_columns' ] );
		add_action( 'manage_' . ShipmentTemplatePostType::POST_TYPE . '_posts_custom_column', [ $this, 'render_column' ], 10, 2 );
	}

	/**
	 * @param array<string, string> $columns .
	 *
	 * @return array<string, string>
	 */
	public function add_columns( array $columns ): array {
		$updated = [];

		foreach ( $columns as $key => $label ) {
			$updated[ $key ] = $label;

			if ( 'title' === $key ) {
				$updated[ self::COLUMN_TYPE ]       = __( 'Typ', 'dhl-polska' );
				$updated[ self::COLUMN_WEIGHT ]     = __( 'Domyślna waga', 'dhl-polska' );
				$updated[ self::COLUMN_DIMENSIONS ] = __( 'Wymiary', 'dhl-polska' );
				$updated[ self::COLUMN_SERVICES ]   = __( 'Usługi', 'dhl-polska' );
			}
		}

		if ( ! isset( $updated[ self::COLUMN_TYPE ] ) ) {
			$updated[ self::COLUMN_TYPE ]       = __( 'Typ', 'dhl-polska' );
			$updated[ self::COLUMN_WEIGHT ]     = __( 'Domyślna waga', 'dhl-polska' );
			$updated[ self::COLUMN_DIMENSIONS ] = __( 'Wymiary', 'dhl-polska' );
			$updated[ self::COLUMN_SERVICES ]   = __( 'Usługi', 'dhl-polska' );
		}

		return $updated;
	}

	/**
	 * @param string $column .
	 * @param int    $post_id .
	 *
	 * @return void
	 */
	public function render_column( string $column, int $post_id ): void {
		$template       = $this->templates_repository->get_template_meta( $post_id );
		$weight_unit    = get_option( 'woocommerce_weight_unit', 'kg' );
		$dimension_unit = get_option( 'woocommerce_dimension_unit', 'cm' );
		$weight         = wc_format_decimal( (string) ( $template['weight'] ?? '' ) );
		$type_labels    = $this->shipment_types->get_type_labels();
		$type_label     = $type_labels[ (string) ( $template['type'] ?? '' ) ] ?? (string) ( $template['type'] ?? '' );
		$dimensions     = sprintf(
			'%s x %s x %s',
			(string) ( $template['dimensions']['length'] ?? '' ),
			(string) ( $template['dimensions']['width'] ?? '' ),
			(string) ( $template['dimensions']['height'] ?? '' )
		);
		$services       = is_array( $template['services'] ?? null ) ? $template['services'] : [];
		$service_codes  = [];

		foreach ( self::SERVICE_CODE_MAP as $service_key => $service_code ) {
			if ( empty( $services[ $service_key ] ) ) {
				continue;
			}

			$service_codes[] = $service_code;
		}

		$services_label = empty( $service_codes ) ? '-' : implode( ', ', $service_codes );

		$output_map = [
			self::COLUMN_TYPE       => $type_label,
			self::COLUMN_WEIGHT     => trim( $weight . ' ' . $weight_unit ),
			self::COLUMN_DIMENSIONS => trim( $dimensions . ' ' . $dimension_unit ),
			self::COLUMN_SERVICES   => $services_label,
		];

		if ( ! isset( $output_map[ $column ] ) ) {
			return;
		}

		echo wp_kses_post( esc_html( $output_map[ $column ] ) );
	}
}
