<?php

namespace RadiusTheme\RadiusBooking\ExportImport;

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

use RadiusTheme\RadiusBooking\Core\Container\Container;

/**
 * Class ExportBuilder
 *
 * Fluent builder for export operations.
 *
 * Usage:
 *   $data = Export::module('appointment')->export();
 *   $data = Export::module('appointment')->export(0, 100);
 *   $data = Export::modules(['category', 'service'])->export();
 *
 * @package RadiusTheme\RadiusBooking\ExportImport
 */
class ExportBuilder {

	/**
	 * Module names to export.
	 *
	 * @var array
	 */
	private array $modules = array();

	/**
	 * Set a single module.
	 *
	 * @param string $module Module name.
	 *
	 * @return self
	 */
	public function module( string $module ): self {
		$this->modules = array( $module );
		return $this;
	}

	/**
	 * Set multiple modules.
	 *
	 * @param array $modules Module names.
	 *
	 * @return self
	 */
	public function modules( array $modules ): self {
		$this->modules = $modules;
		return $this;
	}

	/**
	 * Execute the export.
	 *
	 * @param int $offset Starting offset (single module only).
	 * @param int $limit  Batch limit (single module only).
	 *
	 * @return array Exported data.
	 */
	public function export( int $offset = 0, int $limit = 0 ): array {
		$service = Container::resolve( ExportImportService::class );

		if ( count( $this->modules ) === 1 && $limit > 0 ) {
			return $service->exportBatch( $this->modules[0], $offset, $limit );
		}

		if ( count( $this->modules ) === 1 ) {
			return $service->exportAll( $this->modules[0] );
		}

		return $service->exportModules( $this->modules );
	}
}
