<?php

namespace Limb_Chatbot\Includes\AI_Providers\Open_Router\Endpoints\Model\Handlers;

use Limb_Chatbot\Includes\AI_Providers\Open_Router\Handlers\Response_Handler;
use Limb_Chatbot\Includes\Data_Objects\AI_Model;
use Limb_Chatbot\Includes\Exceptions\Exception;

/**
 * Class Model_Response_Handler
 *
 * Handles parsing of OpenRouter model API responses.
 *
 * @package Limb_Chatbot\Includes\AI_Providers\Open_Router\Endpoints\Model\Handlers
 * @since 1.0.14
 */
class Model_Response_Handler extends Response_Handler {

	/**
	 * Retrieves a single model from the response.
	 *
	 * @return AI_Model|null The model or null if not found.
	 * @throws Exception
	 * @since 1.0.14
	 */
	public function get_model(): ?AI_Model {
		$body = $this->get_body();

		if ( empty( $body->id ) ) {
			return null;
		}

		return AI_Model::find_by_name( $body->id );
	}

	/**
	 * Checks if model data was returned.
	 *
	 * @return bool True if model exists in response.
	 * @since 1.0.14
	 */
	public function has_model(): bool {
		$body = $this->get_body();
		return ! empty( $body->id );
	}
}
