<?php
/**
 * Organization Schema Type
 *
 * @package SEOvaultSEO\Schema\Types
 */

namespace SEOvaultSEO\Schema\Types;

use SEOvaultSEO\Schema\SchemaBase;
use SEOvaultSEO\Core\Options;

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

class Organization extends SchemaBase {

	public function schema_data() {
		$data = array(
			'title'   => 'Organization',
			'type'    => 'Organization',
			'show_on' => array(
				'rules'        => array( 'basic-global' ),
				'specific'     => array(),
				'specificText' => array(),
			),
			'fields'  => $this->parse_fields( $this->get() ),
		);

		// Inject social profiles into sameAs if not manually set.
		if ( isset( $data['fields']['sameAs'] ) && empty( $data['fields']['sameAs'] ) ) {
			$profiles = self::collect_social_profiles();
			if ( ! empty( $profiles ) ) {
				$data['fields']['sameAs'] = $profiles;
			}
		}

		return $data;
	}

	/**
	 * Collect all social profile URLs from Options for sameAs schema property.
	 *
	 * @return array Array of sanitized, deduplicated URL strings.
	 */
	private static function collect_social_profiles() {
		$profiles = array_merge(
			array_values( Options::get_social_profiles() ),
			array_filter(
				array(
					Options::get_facebook_page_url(),
					Options::get_twitter_profile_username(),
				)
			)
		);
		$profiles = array_filter( array_map( 'esc_url_raw', $profiles ) );
		return array_values( array_unique( $profiles ) );
	}

	public function get() {
		return apply_filters(
			'seovaultseo_schema_type_organization',
			array(
				array(
					'id'       => '@id',
					'type'     => 'Hidden',
					'std'      => '%site.url%#organization',
					'required' => true,
					'default'  => true,
					'show'     => false,
				),
				array(
					'id'      => 'schema_name',
					'label'   => __( 'Schema Title', 'seovault-ai' ),
					'show'    => true,
					'default' => true,
					'type'    => 'Title',
					'std'     => 'Organization',
				),
				array(
					'id'       => 'name',
					'label'    => __( 'Name', 'seovault-ai' ),
					'required' => true,
					'std'      => '%site.title%',
					'type'     => 'Text',
					'default'  => true,
					'show'     => true,
				),
				array(
					'id'       => 'url',
					'label'    => __( 'URL', 'seovault-ai' ),
					'required' => true,
					'std'      => '%site.url%',
					'type'     => 'Text',
					'default'  => true,
					'show'     => true,
				),
				array(
					'id'      => 'logo',
					'label'   => __( 'Logo', 'seovault-ai' ),
					'std'     => '%site.logo%',
					'type'    => 'Text',
					'default' => true,
					'show'    => true,
				),
				array(
					'id'      => 'description',
					'label'   => __( 'Description', 'seovault-ai' ),
					'default' => true,
					'show'    => true,
					'std'     => '%site.description%',
					'type'    => 'Text',
				),
				array(
					'id'      => 'sameAs',
					'label'   => __( 'Social Profiles', 'seovault-ai' ),
					'type'    => 'Text',
					'default' => true,
					'show'    => true,
					'std'     => '',
				),
			)
		);
	}
}
