<?php

namespace Limb_Chatbot\Includes\Factories;

use Limb_Chatbot\Includes\Exceptions\Error_Codes;
use Limb_Chatbot\Includes\Exceptions\Exception;
use Limb_Chatbot\Includes\Interfaces\Chat_Export_Formatter_Interface;
use Limb_Chatbot\Includes\Services\Export\Formatters\Csv_Chat_Export_Formatter;
use Limb_Chatbot\Includes\Services\Export\Formatters\Json_Chat_Export_Formatter;
use Limb_Chatbot\Includes\Services\Export\Formatters\Pdf_Chat_Export_Formatter;

/**
 * Class Chat_Export_Formatter_Factory
 *
 * Creates the appropriate chat export formatter strategy based on the requested format.
 *
 * @package Limb_Chatbot\Includes\Factories
 * @since 1.0.0
 */
class Chat_Export_Formatter_Factory {

	/**
	 * Allowed export formats.
	 *
	 * @var string[]
	 */
	const ALLOWED_FORMATS = [ 'json', 'csv', 'pdf' ];

	/**
	 * Create a formatter instance for the given format type.
	 *
	 * @param string $format The export format ('json', 'csv', or 'pdf').
	 *
	 * @return Chat_Export_Formatter_Interface The formatter strategy instance.
	 * @throws Exception When the requested format is not supported.
	 */
	public function make( string $format ): Chat_Export_Formatter_Interface {
		$format = strtolower( sanitize_text_field( $format ) );

		if ( $format === 'json' ) {
			return new Json_Chat_Export_Formatter();
		} elseif ( $format === 'csv' ) {
			return new Csv_Chat_Export_Formatter();
		} elseif ( $format === 'pdf' ) {
			return new Pdf_Chat_Export_Formatter();
		}

		throw new Exception(
			Error_Codes::NOT_SUPPORTED,
			/* translators: %s is the unsupported format name */
			sprintf( __( 'Export format "%s" is not supported.', 'limb-chatbot' ), $format )
		);
	}

	/**
	 * Check whether a given format string is supported.
	 *
	 * @param string $format Format identifier.
	 *
	 * @return bool True if supported, false otherwise.
	 */
	public static function is_supported( string $format ): bool {
		return in_array( strtolower( $format ), self::ALLOWED_FORMATS, true );
	}
}

