<?php
/**
 * WooCommerce Attributes REST Controller
 *
 * @category   WordPress_Plugin
 * @package    Common
 * @subpackage Rest_API
 * @author     miniOrange <info@miniorange.com>
 * @license    MIT/Expat
 * @link       https://plugins.miniorange.com/woocommerce-api-product-sync-with-woocommerce-rest-apis
 */

namespace MOWCPS\Common\Controllers\Rest_API;

use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
use MOWCPS\Common\Abstracts\Base_Rest_Controller;
use MOWCPS\Common\Handlers\WooCommerce\WooCommerce_Handler;
use MOWCPS\Common\Utils\Response_Builder;
use MOWCPS\Common\Constants\Constants;

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

/**
 * Class WooCommerce_Attributes_Rest_Controller
 *
 * Handles WooCommerce attributes REST API requests
 */
class WooCommerce_Attributes_Rest_Controller extends Base_Rest_Controller {

	/**
	 * Get WooCommerce attributes and taxonomies
	 *
	 * @param WP_REST_Request $request The request object.
	 * @return WP_REST_Response
	 */
	public function get_attributes( $request ) {
		try {
			// Core WooCommerce attributes.
			$core_attributes = array(
				'Product Name' => 'Product Name',
				'SKU'          => 'SKU',
				'Stock Status' => 'Stock Status',
				'Product Type' => 'Product Type',
				'Categories'   => 'Categories',
				'Tags'         => 'Tags',
				'Brands'       => 'Brands',
			);

			// Get WooCommerce attributes.
			$wc_attributes        = array();
			$attribute_taxonomies = wc_get_attribute_taxonomies();

			if ( ! empty( $attribute_taxonomies ) ) {
				foreach ( $attribute_taxonomies as $tax ) {
					$wc_attributes[ $tax->attribute_label ] = $tax->attribute_label;
				}
			}

			// Merge core attributes and WooCommerce attributes.
			$all_attributes = array_merge( $core_attributes, $wc_attributes );

			$attributes_array = array();
			foreach ( $all_attributes as $key => $value ) {
				$attributes_array[] = array(
					'label' => $value,
					'value' => $value,
				);
			}

			return Response_Builder::success( $attributes_array );

		} catch ( \Exception $e ) {
			return Response_Builder::error(
				'fetch_attributes_failed',
				'Failed to fetch WooCommerce attributes: ' . $e->getMessage(),
				array(),
				Constants::HTTP_INTERNAL_SERVER_ERROR
			);
		}
	}

	/**
	 * Get WooCommerce countries
	 *
	 * Retrieves all available countries from WooCommerce and returns them
	 * formatted for use in dropdown components.
	 *
	 * @param WP_REST_Request $request The request object.
	 * @return WP_REST_Response Response containing countries array or error message.
	 */
	public function get_countries( $request ) {
		try {
			// Check if WooCommerce is available.
			if ( ! class_exists( 'WooCommerce' ) ) {
				return Response_Builder::error(
					'woocommerce_not_installed',
					'WooCommerce is not installed',
					array(),
					Constants::HTTP_BAD_REQUEST
				);
			}

			// Get countries from handler.
			$countries_array = WooCommerce_Handler::get_countries();

			return Response_Builder::success( $countries_array );

		} catch ( \Exception $e ) {
			return Response_Builder::error(
				'fetch_countries_failed',
				'Failed to fetch countries: ' . $e->getMessage(),
				array(),
				Constants::HTTP_INTERNAL_SERVER_ERROR
			);
		}
	}

	/**
	 * Get WooCommerce states for a country
	 *
	 * Retrieves all available states/provinces for a given country code
	 * and returns them formatted for use in dropdown components.
	 *
	 * @param WP_REST_Request $request The request object. Must contain 'country' parameter.
	 * @return WP_REST_Response Response containing states array or error message.
	 */
	public function get_states( $request ) {
		try {
			// Check if WooCommerce is available.
			if ( ! class_exists( 'WooCommerce' ) ) {
				return Response_Builder::error(
					'woocommerce_not_installed',
					'WooCommerce is not installed',
					array(),
					Constants::HTTP_BAD_REQUEST
				);
			}

			// Validate country code parameter.
			$country_code = $request->get_param( 'country' );
			if ( empty( $country_code ) ) {
				return Response_Builder::error(
					'missing_country_code',
					'Country code is required',
					array(),
					Constants::HTTP_BAD_REQUEST
				);
			}

			// Get states from handler.
			$states_array = WooCommerce_Handler::get_states( $country_code );

			return Response_Builder::success( $states_array );

		} catch ( \Exception $e ) {
			return Response_Builder::error(
				'fetch_states_failed',
				'Failed to fetch states: ' . $e->getMessage(),
				array(),
				Constants::HTTP_INTERNAL_SERVER_ERROR
			);
		}
	}
}
