<?php
/**
 * Hours of Operation Service
 *
 * Manages business hours for schema.org markup.
 *
 * @package BW_Schema
 * @since 3.0.0-babel
 */

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

class BW_Schema_Hours {

	/**
	 * Days of week
	 *
	 * @var array
	 */
	const DAYS = array(
		'monday'    => 'Monday',
		'tuesday'   => 'Tuesday',
		'wednesday' => 'Wednesday',
		'thursday'  => 'Thursday',
		'friday'    => 'Friday',
		'saturday'  => 'Saturday',
		'sunday'    => 'Sunday',
	);

	/**
	 * Get hours field definitions for form builder
	 *
	 * @return array Field definitions
	 */
	public static function get_field_definitions() {
		$fields = array(
			array(
				'section'   => 'hours_operation',
				'name'      => 'hours_description',
				'label'     => __( 'Hours Description', 'bw-schema' ),
				'type'      => 'textarea',
				'help'      => __( 'Brief description of hours (e.g., "Open 9 AM - 5 PM Weekdays, 10 AM - 3 PM Saturdays").', 'bw-schema' ),
				'sanitize'  => 'sanitize_textarea_field',
				'rows'      => 3,
			),
		);

		// Add a field for each day
		foreach ( self::DAYS as $day_key => $day_label ) {
			$fields[] = array(
				'section'   => 'hours_operation',
				'name'      => 'hours_' . $day_key . '_open',
				'label'     => __( $day_label . ' - Opens at', 'bw-schema' ),
				'type'      => 'time',
				'help'      => __( 'Opening time (24-hour format, e.g., 09:00)', 'bw-schema' ),
				'sanitize'  => 'sanitize_text_field',
			);

			$fields[] = array(
				'section'   => 'hours_operation',
				'name'      => 'hours_' . $day_key . '_close',
				'label'     => __( $day_label . ' - Closes at', 'bw-schema' ),
				'type'      => 'time',
				'help'      => __( 'Closing time (24-hour format, e.g., 17:00)', 'bw-schema' ),
				'sanitize'  => 'sanitize_text_field',
			);

			$fields[] = array(
				'section'   => 'hours_operation',
				'name'      => 'hours_' . $day_key . '_closed',
				'label'     => __( $day_label . ' - Closed', 'bw-schema' ),
				'type'      => 'checkbox',
				'help'      => __( 'Check if closed all day', 'bw-schema' ),
				'sanitize'  => 'sanitize_text_field',
			);
		}

		return $fields;
	}

	/**
	 * Format hours for schema.org openingHoursSpecification
	 *
	 * @param array $org_data Organization data
	 * @return array OpeningHoursSpecification array for schema.org
	 */
	public static function format_for_schema( $org_data ) {
		$hours_spec = array();

		foreach ( self::DAYS as $day_key => $day_label ) {
			$open_key = 'hours_' . $day_key . '_open';
			$close_key = 'hours_' . $day_key . '_close';
			$closed_key = 'hours_' . $day_key . '_closed';

			// Skip if closed
			if ( ! empty( $org_data[ $closed_key ] ) ) {
				continue;
			}

			// Skip if no opening time
			if ( empty( $org_data[ $open_key ] ) || empty( $org_data[ $close_key ] ) ) {
				continue;
			}

			$day_abbr = strtoupper( substr( $day_key, 0, 2 ) );

			$hours_spec[] = array(
				'@type'             => 'OpeningHoursSpecification',
				'dayOfWeek'         => $day_abbr,
				'opens'             => $org_data[ $open_key ],
				'closes'            => $org_data[ $close_key ],
			);
		}

		return $hours_spec;
	}

	/**
	 * Get schema.org day abbreviation
	 *
	 * @param string $day_key Day key (monday, tuesday, etc.)
	 * @return string Abbreviated day (Mo, Tu, We, Th, Fr, Sa, Su)
	 */
	public static function get_day_abbr( $day_key ) {
		$abbr_map = array(
			'monday'    => 'Mo',
			'tuesday'   => 'Tu',
			'wednesday' => 'We',
			'thursday'  => 'Th',
			'friday'    => 'Fr',
			'saturday'  => 'Sa',
			'sunday'    => 'Su',
		);

		return $abbr_map[ $day_key ] ?? '';
	}
}
