<?php
/**
 * Contact Information Management Service
 *
 * Manages multiple contact methods and ContactPoint schema.org markup.
 *
 * @package BW_Schema
 * @since 3.0.0-babel
 */

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

class BW_Schema_Contact {

	/**
	 * Contact type options
	 *
	 * @var array
	 */
	const CONTACT_TYPES = array(
		'customer_service' => 'Customer Service',
		'sales'            => 'Sales',
		'technical_support' => 'Technical Support',
		'reservations'     => 'Reservations',
		'billing'          => 'Billing',
		'emergency'        => 'Emergency',
	);

	/**
	 * Get contact field definitions for form builder
	 *
	 * @return array Field definitions for contact section
	 */
	public static function get_field_definitions() {
		$fields = array();

		// Add fields for up to 3 additional contact points
		for ( $i = 1; $i <= 3; $i++ ) {
			$fields[] = array(
				'section'   => 'contact_information',
				'name'      => 'contact_type_' . $i,
				'label'     => sprintf( __( 'Contact Type %d', 'bw-schema' ), $i ),
				'type'      => 'select',
				'options'   => array( '' => '-- Select Type --' ) + self::CONTACT_TYPES,
				'help'      => __( 'Type of contact (optional)', 'bw-schema' ),
				'sanitize'  => 'sanitize_text_field',
			);

			$fields[] = array(
				'section'   => 'contact_information',
				'name'      => 'contact_phone_' . $i,
				'label'     => sprintf( __( 'Phone Number %d', 'bw-schema' ), $i ),
				'type'      => 'tel',
				'help'      => __( 'e.g., "+1-555-0123"', 'bw-schema' ),
				'sanitize'  => 'sanitize_text_field',
				'show_if'   => array( 'contact_type_' . $i => array_keys( self::CONTACT_TYPES ) ),
			);
		}

		return $fields;
	}

	/**
	 * Format contact points for schema.org
	 *
	 * @param array $org_data Organization data
	 * @return array ContactPoint schemas
	 */
	public static function format_for_schema( $org_data ) {
		$contact_points = array();

		// Add primary contact
		if ( ! empty( $org_data['telephone'] ) ) {
			$contact_points[] = array(
				'@type'       => 'ContactPoint',
				'contactType' => 'Customer Service',
				'telephone'   => $org_data['telephone'],
			);
		}

		// Add additional contact points
		for ( $i = 1; $i <= 3; $i++ ) {
			$type_key = 'contact_type_' . $i;
			$phone_key = 'contact_phone_' . $i;

			if ( ! empty( $org_data[ $type_key ] ) && ! empty( $org_data[ $phone_key ] ) ) {
				$contact_points[] = array(
					'@type'       => 'ContactPoint',
					'contactType' => $org_data[ $type_key ],
					'telephone'   => $org_data[ $phone_key ],
				);
			}
		}

		return $contact_points;
	}

	/**
	 * Get contact type label
	 *
	 * @param string $type Contact type key
	 * @return string Contact type label
	 */
	public static function get_type_label( $type ) {
		return self::CONTACT_TYPES[ $type ] ?? $type;
	}
}
