<?php
/**
 * Media Management Service
 *
 * Manages business images, logos, and media for schema.org markup.
 *
 * @package BW_Schema
 * @since 3.0.0-babel
 */

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

class BW_Schema_Media {

	/**
	 * Get media field definitions for form builder
	 *
	 * @return array Field definitions for media section
	 */
	public static function get_field_definitions() {
		return array(
			array(
				'section'   => 'media_images',
				'name'      => 'logo_url',
				'label'     => __( 'Logo URL', 'bw-schema' ),
				'type'      => 'url',
				'help'      => __( 'Full URL to your business logo (recommended: 600x60px for best results)', 'bw-schema' ),
				'sanitize'  => 'esc_url_raw',
			),
			array(
				'section'   => 'media_images',
				'name'      => 'featured_image_url',
				'label'     => __( 'Featured Image URL', 'bw-schema' ),
				'type'      => 'url',
				'help'      => __( 'Main image for your business (recommended: 1200x630px)', 'bw-schema' ),
				'sanitize'  => 'esc_url_raw',
			),
			array(
				'section'   => 'media_images',
				'name'      => 'gallery_images',
				'label'     => __( 'Gallery Images (URLs)', 'bw-schema' ),
				'type'      => 'textarea',
				'help'      => __( 'Enter one URL per line for additional images', 'bw-schema' ),
				'sanitize'  => 'sanitize_textarea_field',
				'rows'      => 5,
			),
		);
	}

	/**
	 * Parse gallery image URLs from textarea
	 *
	 * @param string $gallery_text Newline-separated URLs
	 * @return array Array of image URLs
	 */
	public static function parse_gallery_images( $gallery_text ) {
		if ( empty( $gallery_text ) ) {
			return array();
		}

		$urls = array_map( 'trim', explode( "\n", $gallery_text ) );
		$urls = array_filter( $urls ); // Remove empty lines
		$urls = array_map( 'esc_url_raw', $urls ); // Sanitize URLs

		return array_values( $urls );
	}

	/**
	 * Format image for schema.org
	 *
	 * @param string $url Image URL
	 * @return array Image schema or empty
	 */
	public static function format_image( $url ) {
		if ( empty( $url ) ) {
			return array();
		}

		return array(
			'@type' => 'ImageObject',
			'url'   => esc_url_raw( $url ),
		);
	}

	/**
	 * Format multiple images for schema.org
	 *
	 * @param array $org_data Organization data
	 * @return array Array of image schemas
	 */
	public static function format_images_for_schema( $org_data ) {
		$images = array();

		// Logo
		if ( ! empty( $org_data['logo_url'] ) ) {
			$images[] = self::format_image( $org_data['logo_url'] );
		}

		// Featured image
		if ( ! empty( $org_data['featured_image_url'] ) ) {
			$images[] = self::format_image( $org_data['featured_image_url'] );
		}

		// Gallery images
		if ( ! empty( $org_data['gallery_images'] ) ) {
			$gallery = self::parse_gallery_images( $org_data['gallery_images'] );
			foreach ( $gallery as $url ) {
				$images[] = self::format_image( $url );
			}
		}

		return array_filter( $images );
	}

	/**
	 * Get logo URL
	 *
	 * @param array $org_data Organization data
	 * @return string Logo URL or empty
	 */
	public static function get_logo_url( $org_data ) {
		return $org_data['logo_url'] ?? '';
	}

	/**
	 * Get featured image URL
	 *
	 * @param array $org_data Organization data
	 * @return string Featured image URL or empty
	 */
	public static function get_featured_image_url( $org_data ) {
		return $org_data['featured_image_url'] ?? '';
	}

	/**
	 * Get image count
	 *
	 * @param array $org_data Organization data
	 * @return int Number of images
	 */
	public static function count_images( $org_data ) {
		return count( self::format_images_for_schema( $org_data ) );
	}
}
