<?php
/**
 * RadiusBooking API Route Registrar
 *
 * This class is responsible for registering API routes dynamically with the WordPress REST API.
 * It supports CRUD operations and allows for middleware to be applied globally or per route.
 *
 * @package RadiusTheme\RadiusBooking\Core\Api\Routes
 */

namespace RadiusTheme\RadiusBooking\Resources;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
use RadiusTheme\RadiusBooking\Abstracts\BaseResource;
use RadiusTheme\RadiusBooking\Models\Category;
use RadiusTheme\RadiusBooking\Repositories\ServiceRepository;
use RadiusTheme\RadiusBooking\Repositories\UserRepository;

/**
 * Api/Resources/ServiceResource.php
 * Service resource transformer
 *
 * @package RadiusTheme\RadiusBooking\Resources
 */
class ServiceResource extends BaseResource {
	/**
	 * Transform a Service model into an API-friendly array.
	 *
	 * @param object $service The Service model instance.
	 * @return array Transformed service data.
	 */
	public function transform( $service ): array {
		$categories = (array) ( new ServiceRepository() )->getCategoryServices( $service->category_id );

		return array(
			'id'                  => $service->id,
			'name'                => $service->name,
			'description'         => $service->description,
			'color'               => $service->color,
			'price'               => (float) $service->price,
			'status'              => $service->status,
			'category_id'         => $service->category_id,
			'min_capacity'        => $service->min_capacity,
			'max_capacity'        => $service->max_capacity,
			'duration'            => $service->duration,
			'time_before'         => (int) $service->time_before,
			'time_after'          => (int) $service->time_after,
			'bringing_anyone'     => (bool) $service->bringing_anyone,
			'priority'            => $service->priority,
			'picture_full_path'   => $service->picture_full_path,
			'picture_thumb_path'  => $service->picture_thumb_path,
			'position'            => (int) $service->position,
			'is_visible'          => (bool) $service->is_visible,
			'aggregated_price'    => (bool) $service->aggregated_price,
			'settings'            => maybe_unserialize( $service->settings ),
			'recurring_cycle'     => $service->recurring_cycle,
			'recurring_sub'       => $service->recurring_sub,
			'recurring_payment'   => (int) $service->recurring_payment,
			'deposit_payment'     => rtrb_pro_active() ? $service->deposit_payment : false,
			'deposit_type'        => rtrb_pro_active() ? $service->deposit_type : 'fixed',
			'deposit_per_person'  => rtrb_pro_active() ? (bool) $service->deposit_per_person : false,
			'deposit'             => rtrb_pro_active() ? (float) $service->deposit : 0,
			'full_payment'        => rtrb_pro_active() ? (bool) $service->full_payment : true,
			'mandatory_extra'     => (bool) $service->mandatory_extra,
			'min_selected_extras' => (int) $service->min_selected_extras,
			'custom_pricing'      => maybe_unserialize( $service->custom_pricing ),
			'max_extra_people'    => (int) $service->max_extra_people,
			'limit_per_customer'  => maybe_unserialize( $service->limit_per_customer ),
			'currency_symbol'     => get_rtrb_currency_symbol(),
			'currency'            => array(
				'currency_symbol' => get_rtrb_currency_symbol(),
				'currency'        => get_rtrb_currency(),
			),
			'assigned_employee_id' => $this->getServicesEmployee( $service->id ),
			'listing_id'          => $service->listing_id,
			'source_type'         => $service->source_type ?? 'manual',
			'listing_url'         => $service->listing_id ? get_permalink( $service->listing_id ) : null,
			'created_at'          => $service->created_at,
			'updated_at'          => $service->updated_at,
		);
	}

	private function getServicesEmployee( int $serviceId ): array {
		return ( new ServiceRepository() )->getServicesEmployeesIds( $serviceId );
	}
}
