<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Assigns structurally comparable page families before analysis.
 */
class Content_Miner_Family_Classifier {
	/**
	 * @param array<string,mixed> $item Content item.
	 * @return string
	 */
	public function classify( array $item ) {
		$path     = isset( $item['url'] ) ? (string) wp_parse_url( $item['url'], PHP_URL_PATH ) : '';
		$segments = array_values( array_filter( explode( '/', trim( $path, '/' ) ) ) );
		$first    = isset( $segments[0] ) ? strtolower( $segments[0] ) : '';
		$last     = $segments ? strtolower( end( $segments ) ) : '';

		if ( 'nationwide-shipping' === $first ) {
			return false !== strpos( $last, '-to-' ) ? 'nationwide-route' : 'city-page';
		}

		if ( 'carriers' === $first ) {
			if ( in_array( $last, array( 'tracking', 'claims', 'reviews' ), true ) ) {
				return 'carrier-' . rtrim( $last, 's' );
			}

			return 'carrier-profile';
		}

		if ( 'shipping' === $first ) {
			return 'shipping-guide';
		}

		if ( 'services' === $first ) {
			return 'service-page';
		}

		if ( in_array( $first, array( 'help', 'answers', 'what-is' ), true ) ) {
			return 'help-content';
		}

		if ( 'post' === $item['post_type'] ) {
			return 'editorial';
		}

		return 'other-page';
	}

	/**
	 * @return array<string,string>
	 */
	public function labels() {
		return array(
			'nationwide-route' => __( 'Nationwide routes', 'content-miner' ),
			'city-page'        => __( 'City pages', 'content-miner' ),
			'carrier-profile'  => __( 'Carrier profiles', 'content-miner' ),
			'carrier-tracking' => __( 'Carrier tracking', 'content-miner' ),
			'carrier-claim'    => __( 'Carrier claims', 'content-miner' ),
			'carrier-review'   => __( 'Carrier reviews', 'content-miner' ),
			'shipping-guide'   => __( 'Shipping guides', 'content-miner' ),
			'service-page'     => __( 'Service pages', 'content-miner' ),
			'help-content'     => __( 'Help and answers', 'content-miner' ),
			'editorial'        => __( 'Editorial articles', 'content-miner' ),
			'other-page'       => __( 'Other pages', 'content-miner' ),
		);
	}
}

