<?php
/**
 * Ten cot file import (tieng Viet khong dau) → key noi bo.
 *
 * @package DatvetaucaotocBooking
 */

declare(strict_types=1);

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

final class Datvetau_Booking_Trip_Import_Columns {

	/** @var array<string,string> */
	public const LABEL_TO_KEY = array(
		'ma_tuyen'          => 'route_slug',
		'ngay_chuyen'       => 'trip_date',
		'gio_di'            => 'departure_time',
		'gio_den'           => 'arrival_time',
		'ten_hang'          => 'operator_name',
		'hang_cho'          => 'seat_class',
		'gia_ve_nguoi_lon'  => 'price_adult_vnd',
		'gia_ve_tre_em'     => 'price_child_vnd',
		'gia_ve_cao_tuoi'   => 'price_senior_vnd',
		'so_ghe'            => 'capacity',
		'logo_hang'         => 'operator_logo_url',
		// Cu (CSV tieng Anh) — van ho tro.
		'route_slug'        => 'route_slug',
		'trip_date'         => 'trip_date',
		'departure_time'    => 'departure_time',
		'arrival_time'      => 'arrival_time',
		'operator_name'     => 'operator_name',
		'seat_class'        => 'seat_class',
		'price_adult_vnd'   => 'price_adult_vnd',
		'price_child_vnd'   => 'price_child_vnd',
		'price_senior_vnd'  => 'price_senior_vnd',
		'capacity'          => 'capacity',
		'operator_logo_url' => 'operator_logo_url',
	);

	/** @var list<string> */
	public const REQUIRED_KEYS = array(
		'route_slug',
		'trip_date',
		'departure_time',
		'arrival_time',
		'operator_name',
		'seat_class',
		'price_adult_vnd',
		'price_child_vnd',
		'price_senior_vnd',
		'capacity',
	);

	/** @var array<string,string> */
	public const KEY_TO_LABEL = array(
		'route_slug'        => 'ma_tuyen',
		'trip_date'         => 'ngay_chuyen',
		'departure_time'    => 'gio_di',
		'arrival_time'      => 'gio_den',
		'operator_name'     => 'ten_hang',
		'seat_class'        => 'hang_cho',
		'price_adult_vnd'   => 'gia_ve_nguoi_lon',
		'price_child_vnd'   => 'gia_ve_tre_em',
		'price_senior_vnd'  => 'gia_ve_cao_tuoi',
		'capacity'          => 'so_ghe',
		'operator_logo_url' => 'logo_hang',
	);

	/** @var list<string> Header dong 1 file mau (Excel/CSV). */
	public const SAMPLE_HEADER_LABELS = array(
		'ma_tuyen',
		'ngay_chuyen',
		'gio_di',
		'gio_den',
		'ten_hang',
		'hang_cho',
		'gia_ve_nguoi_lon',
		'gia_ve_tre_em',
		'gia_ve_cao_tuoi',
		'so_ghe',
	);

	public static function normalize_header_cell( string $label ): string {
		$label = strtolower( trim( $label ) );
		if ( function_exists( 'remove_accents' ) ) {
			$label = remove_accents( $label );
		}
		$label = preg_replace( '/[^a-z0-9_]+/', '_', $label ) ?? $label;
		$label = trim( $label, '_' );
		return $label;
	}

	/**
	 * @param list<string> $header_labels
	 * @return array{keys:list<string>,errors:list<string>}
	 */
	public static function map_header( array $header_labels ): array {
		$keys   = array();
		$errors = array();
		foreach ( $header_labels as $label ) {
			$norm = self::normalize_header_cell( $label );
			if ( '' === $norm ) {
				$keys[] = '';
				continue;
			}
			$key = self::LABEL_TO_KEY[ $norm ] ?? $norm;
			$keys[] = $key;
		}

		foreach ( self::REQUIRED_KEYS as $required ) {
			if ( ! in_array( $required, $keys, true ) ) {
				$label    = self::KEY_TO_LABEL[ $required ] ?? $required;
				$errors[] = sprintf(
					/* translators: %s: column header label */
					__( 'Thieu cot bat buoc: %s (xem file mau).', 'datvetaucaotoc-booking' ),
					$label
				);
			}
		}

		return array( 'keys' => $keys, 'errors' => $errors );
	}

	public static function required_label_vi( string $key ): string {
		return self::KEY_TO_LABEL[ $key ] ?? $key;
	}
}
