<?php
/**
 * Redirect Manager
 *
 * High-level manager for creating, updating, deleting, and querying redirects.
 * Delegates database operations to RedirectDb and validation to RedirectValidator.
 *
 * @package SEOvaultSEO\RedirectManager
 */

namespace SEOvaultSEO\RedirectManager;

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

/**
 * Manager class for redirect CRUD operations.
 */
class RedirectManager {

	/**
	 * Validator instance.
	 *
	 * @var RedirectValidator
	 */
	protected $validator;

	/**
	 * Constructor.
	 */
	public function __construct() {
		$this->validator = new RedirectValidator();
	}

	/**
	 * Create a new redirect.
	 *
	 * @param Redirect $redirect Redirect to create.
	 * @return int|false Redirect ID on success, false on failure.
	 */
	public function create( Redirect $redirect ) {
		if ( ! $this->validator->validate_create( $redirect ) ) {
			return false;
		}

		$id = RedirectDb::insert( $redirect );

		if ( $id ) {
			do_action( 'seovaultseo_redirect_created', $redirect, $id );
		}

		return $id;
	}

	/**
	 * Update an existing redirect.
	 *
	 * @param int      $id       Redirect ID.
	 * @param Redirect $redirect New redirect data.
	 * @return bool True on success, false on failure.
	 */
	public function update( $id, Redirect $redirect ) {
		if ( ! $this->validator->validate_update( $redirect, $id ) ) {
			return false;
		}

		$success = RedirectDb::update( $id, $redirect );

		if ( $success ) {
			$redirect->set_id( $id );
			do_action( 'seovaultseo_redirect_updated', $redirect, $id );
		}

		return $success;
	}

	/**
	 * Delete a redirect by ID.
	 *
	 * @param int $id Redirect ID.
	 * @return bool True on success, false on failure.
	 */
	public function delete( $id ) {
		$redirect = RedirectDb::get( $id );

		$success = RedirectDb::delete( $id );

		if ( $success && $redirect ) {
			do_action( 'seovaultseo_redirect_deleted', $redirect, $id );
		}

		return $success;
	}

	/**
	 * Delete multiple redirects by IDs.
	 *
	 * @param array $ids Array of redirect IDs.
	 * @return int Number of rows deleted.
	 */
	public function delete_many( array $ids ) {
		return RedirectDb::delete_many( $ids );
	}

	/**
	 * Get a single redirect by ID.
	 *
	 * @param int $id Redirect ID.
	 * @return Redirect|null
	 */
	public function get( $id ) {
		return RedirectDb::get( $id );
	}

	/**
	 * Get all redirects, optionally filtered by format.
	 *
	 * @param string $format Optional format filter.
	 * @return Redirect[]
	 */
	public function get_all( $format = '' ) {
		return RedirectDb::get_all( $format );
	}

	/**
	 * Get the validator instance.
	 *
	 * @return RedirectValidator
	 */
	public function get_validator() {
		return $this->validator;
	}

	/**
	 * Get the last validation error message.
	 *
	 * @return string|null
	 */
	public function get_error() {
		return $this->validator->get_error();
	}

	/**
	 * Find a matching redirect for a given request URI.
	 *
	 * @param string $uri Request URI (relative path).
	 * @return Redirect|null
	 */
	public function find_match( $uri ) {
		return RedirectDb::find_match( $uri );
	}

	/**
	 * Export all redirects to CSV format.
	 *
	 * @return string CSV content.
	 */
	public function export_csv() {
		$exporter = new CsvExporter();
		return $exporter->export( $this->get_all() );
	}

	/**
	 * Import redirects from a CSV file.
	 *
	 * @param string $file_path Path to the CSV file.
	 * @return array{total: int, imported: int, skipped: int}
	 */
	public function import_csv( $file_path ) {
		$importer  = new CsvImporter();
		$redirects = $importer->load( $file_path );

		$total    = count( $redirects );
		$imported = 0;
		$skipped  = 0;

		foreach ( $redirects as $redirect ) {
			$result = $this->create( $redirect );

			if ( $result ) {
				++$imported;
			} else {
				++$skipped;
			}
		}

		return array(
			'total'    => $total,
			'imported' => $imported,
			'skipped'  => $skipped,
		);
	}
}
