<?php
/**
 * WebPage & Article Schema Management
 *
 * Manages WebPage and Article schema for pages and blog posts.
 *
 * @package BW_Schema
 * @since 3.0.0-babel
 */

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

class BW_Schema_WebPage {

	/**
	 * Get webpage/article field definitions for form builder
	 *
	 * @return array Field definitions for webpage section
	 */
	public static function get_field_definitions() {
		return array(
			array(
				'section'   => 'webpage_schema',
				'name'      => 'article_type',
				'label'     => __( 'Content Type', 'bw-schema' ),
				'type'      => 'select',
				'options'   => array(
					''              => __( '-- Select Type --', 'bw-schema' ),
					'article'       => __( 'Article (blog post, news)', 'bw-schema' ),
					'blogposting'   => __( 'Blog Post', 'bw-schema' ),
					'newsarticle'   => __( 'News Article', 'bw-schema' ),
					'creativework'  => __( 'Creative Work (guides, how-to)', 'bw-schema' ),
				),
				'help'      => __( 'Type of content on your site', 'bw-schema' ),
				'sanitize'  => 'sanitize_text_field',
			),
			array(
				'section'   => 'webpage_schema',
				'name'      => 'article_headline',
				'label'     => __( 'Headline/Title', 'bw-schema' ),
				'type'      => 'text',
				'help'      => __( 'Main headline for content (auto-filled from page title)', 'bw-schema' ),
				'sanitize'  => 'sanitize_text_field',
			),
			array(
				'section'   => 'webpage_schema',
				'name'      => 'article_description',
				'label'     => __( 'Description/Summary', 'bw-schema' ),
				'type'      => 'textarea',
				'help'      => __( 'Brief description of the content (auto-filled from meta description)', 'bw-schema' ),
				'sanitize'  => 'sanitize_textarea_field',
				'rows'      => 3,
			),
			array(
				'section'   => 'webpage_schema',
				'name'      => 'article_author',
				'label'     => __( 'Author', 'bw-schema' ),
				'type'      => 'text',
				'help'      => __( 'Author name (e.g., "John Smith")', 'bw-schema' ),
				'sanitize'  => 'sanitize_text_field',
			),
			array(
				'section'   => 'webpage_schema',
				'name'      => 'article_published_date',
				'label'     => __( 'Published Date', 'bw-schema' ),
				'type'      => 'date',
				'help'      => __( 'Date published (YYYY-MM-DD, auto-filled from post date)', 'bw-schema' ),
				'sanitize'  => 'sanitize_text_field',
			),
			array(
				'section'   => 'webpage_schema',
				'name'      => 'article_modified_date',
				'label'     => __( 'Modified Date', 'bw-schema' ),
				'type'      => 'date',
				'help'      => __( 'Last modified date (auto-filled from post modified date)', 'bw-schema' ),
				'sanitize'  => 'sanitize_text_field',
			),
			array(
				'section'   => 'webpage_schema',
				'name'      => 'article_keywords',
				'label'     => __( 'Keywords/Tags', 'bw-schema' ),
				'type'      => 'text',
				'help'      => __( 'Comma-separated keywords (e.g., "SEO, marketing, tips")', 'bw-schema' ),
				'sanitize'  => 'sanitize_text_field',
			),
			array(
				'section'   => 'webpage_schema',
				'name'      => 'article_image',
				'label'     => __( 'Featured Image URL', 'bw-schema' ),
				'type'      => 'url',
				'help'      => __( 'Featured image for the article (auto-filled from post thumbnail)', 'bw-schema' ),
				'sanitize'  => 'esc_url_raw',
			),
		);
	}

	/**
	 * Format webpage for schema.org Article/BlogPosting
	 *
	 * @param array $org_data Organization data
	 * @return array Article/BlogPosting schema or empty
	 */
	public static function format_for_schema( $org_data ) {
		if ( empty( $org_data['article_type'] ) || empty( $org_data['article_headline'] ) ) {
			return array();
		}

		// Map article type to schema.org type
		$type_map = array(
			'article'       => 'Article',
			'blogposting'   => 'BlogPosting',
			'newsarticle'   => 'NewsArticle',
			'creativework'  => 'CreativeWork',
		);

		$schema_type = $type_map[ $org_data['article_type'] ] ?? 'Article';

		$article = array(
			'@type'       => $schema_type,
			'headline'    => $org_data['article_headline'] ?? get_bloginfo( 'name' ),
			'description' => $org_data['article_description'] ?? '',
		);

		// Add author
		if ( ! empty( $org_data['article_author'] ) ) {
			$article['author'] = array(
				'@type' => 'Person',
				'name'  => $org_data['article_author'],
			);
		}

		// Add published date
		if ( ! empty( $org_data['article_published_date'] ) ) {
			$article['datePublished'] = $org_data['article_published_date'];
		}

		// Add modified date
		if ( ! empty( $org_data['article_modified_date'] ) ) {
			$article['dateModified'] = $org_data['article_modified_date'];
		}

		// Add keywords
		if ( ! empty( $org_data['article_keywords'] ) ) {
			$keywords = array_map( 'trim', explode( ',', $org_data['article_keywords'] ) );
			$article['keywords'] = implode( ', ', $keywords );
		}

		// Add featured image
		if ( ! empty( $org_data['article_image'] ) ) {
			$article['image'] = array(
				'@type' => 'ImageObject',
				'url'   => esc_url_raw( $org_data['article_image'] ),
			);
		}

		// Add publisher (the organization itself)
		$article['publisher'] = array(
			'@type' => 'Organization',
			'name'  => get_bloginfo( 'name' ),
			'logo'  => array(
				'@type' => 'ImageObject',
				'url'   => $org_data['logo_url'] ?? '',
			),
		);

		return $article;
	}

	/**
	 * Check if article schema is configured
	 *
	 * @param array $org_data Organization data
	 * @return bool True if article type is set
	 */
	public static function is_configured( $org_data ) {
		return ! empty( $org_data['article_type'] );
	}
}
