<?php
namespace R2_Offload\Admin;

use R2_Offload\DB;
use R2_Offload\Migration\Migrator;
use R2_Offload\Settings;
use R2_Offload\Storage\Providers;

defined( 'ABSPATH' ) || exit;

/**
 * "Providers & Pricing" admin page: registration/bucket guides for every
 * supported provider, a pros/cons comparison, and a live cost estimator seeded
 * from the current library size.
 */
final class Providers_Page {

	private Settings $settings;
	private Migrator $migrator;

	public function __construct( Settings $settings, Migrator $migrator ) {
		$this->settings = $settings;
		$this->migrator = $migrator;
	}

	/** @return array{bytes:int,attachments:int} */
	private function library_totals(): array {
		global $wpdb;
		$table = DB::items_table();
		$row   = $wpdb->get_row( "SELECT COUNT(*) AS n, COALESCE(SUM(bytes_total),0) AS b FROM {$table}" );
		return [
			'bytes'       => (int) ( $row->b ?? 0 ),
			'attachments' => (int) ( $row->n ?? 0 ),
		];
	}

	public function render(): void {
		$current = $this->settings->provider();
		$totals  = $this->library_totals();
		$gb      = $totals['bytes'] > 0 ? round( $totals['bytes'] / 1073741824, 2 ) : 0;
		// Rough object count: originals + typical thumbnails. User can override.
		$objects = max( $totals['attachments'], $totals['attachments'] * 5 );
		?>
		<div class="wrap" id="r2-offload-providers">
			<h1><?php esc_html_e( 'R2 Offload — Providers & Pricing', 'r2-offload' ); ?></h1>
			<p><?php
				printf(
					/* translators: %s provider label */
					esc_html__( 'Currently selected provider: %s. Change it on the Settings page.', 'r2-offload' ),
					'<strong>' . esc_html( Providers::label( $current ) ) . '</strong>'
				);
			?></p>

			<h2><?php esc_html_e( '1) Cost estimate for your library', 'r2-offload' ); ?></h2>
			<p class="description"><?php
				printf(
					/* translators: 1: size, 2: count */
					esc_html__( 'Seeded from your offload queue: %1$s across ~%2$s files. Adjust the numbers and your expected monthly downloads, then estimate. Figures use public list prices — they are estimates, not quotes.', 'r2-offload' ),
					'<strong>' . esc_html( size_format( $totals['bytes'], 2 ) ) . '</strong>',
					'<strong>' . esc_html( number_format_i18n( $objects ) ) . '</strong>'
				);
			?></p>
			<table class="form-table" role="presentation">
				<tr>
					<th><label for="r2-est-gb"><?php esc_html_e( 'Stored size (GB)', 'r2-offload' ); ?></label></th>
					<td><input type="number" id="r2-est-gb" min="0" step="0.01" value="<?php echo esc_attr( (string) $gb ); ?>"></td>
				</tr>
				<tr>
					<th><label for="r2-est-objects"><?php esc_html_e( 'Object (file) count', 'r2-offload' ); ?></label></th>
					<td><input type="number" id="r2-est-objects" min="0" step="1" value="<?php echo esc_attr( (string) $objects ); ?>"></td>
				</tr>
				<tr>
					<th><label for="r2-est-egress"><?php esc_html_e( 'Monthly downloads (GB)', 'r2-offload' ); ?></label></th>
					<td>
						<input type="number" id="r2-est-egress" min="0" step="0.01" value="" placeholder="<?php esc_attr_e( 'blank = served once/month', 'r2-offload' ); ?>">
						<p class="description"><?php esc_html_e( 'Roughly: monthly media pageviews × average media size. Egress is where providers differ most.', 'r2-offload' ); ?></p>
					</td>
				</tr>
			</table>
			<p><button class="button button-primary" data-r2-action="estimate"><?php esc_html_e( 'Estimate all providers', 'r2-offload' ); ?></button></p>

			<table class="widefat striped" id="r2-est-table" style="display:none">
				<thead><tr>
					<th><?php esc_html_e( 'Provider', 'r2-offload' ); ?></th>
					<th><?php esc_html_e( 'Monthly total', 'r2-offload' ); ?></th>
					<th><?php esc_html_e( 'Storage', 'r2-offload' ); ?></th>
					<th><?php esc_html_e( 'Egress', 'r2-offload' ); ?></th>
					<th><?php esc_html_e( 'Reads', 'r2-offload' ); ?></th>
					<th><?php esc_html_e( 'Base', 'r2-offload' ); ?></th>
					<th><?php esc_html_e( 'One-time upload', 'r2-offload' ); ?></th>
				</tr></thead>
				<tbody data-r2-est-body></tbody>
			</table>
			<div id="r2-est-notes"></div>

			<h2><?php esc_html_e( '2) Provider comparison', 'r2-offload' ); ?></h2>
			<table class="widefat striped r2-compare">
				<thead><tr>
					<th><?php esc_html_e( 'Provider', 'r2-offload' ); ?></th>
					<th><?php esc_html_e( 'Storage $/GB-mo', 'r2-offload' ); ?></th>
					<th><?php esc_html_e( 'Egress $/GB', 'r2-offload' ); ?></th>
					<th><?php esc_html_e( 'Pros', 'r2-offload' ); ?></th>
					<th><?php esc_html_e( 'Cons', 'r2-offload' ); ?></th>
				</tr></thead>
				<tbody>
				<?php foreach ( Providers::all() as $id => $p ) :
					$price = $p['pricing'];
					$egress = ! empty( $price['egress_free'] ) ? __( 'Free', 'r2-offload' ) : '$' . number_format( (float) $price['egress_gb'], 3 );
					?>
					<tr>
						<td><strong><?php echo esc_html( $p['label'] ); ?></strong><?php echo $id === $current ? ' ✓' : ''; ?></td>
						<td>$<?php echo esc_html( number_format( (float) $price['storage_gb_month'], 4 ) ); ?></td>
						<td><?php echo esc_html( $egress ); ?></td>
						<td><ul class="r2-bullets"><?php foreach ( $p['pros'] as $pro ) : ?><li><?php echo esc_html( $pro ); ?></li><?php endforeach; ?></ul></td>
						<td><ul class="r2-bullets"><?php foreach ( $p['cons'] as $con ) : ?><li><?php echo esc_html( $con ); ?></li><?php endforeach; ?></ul></td>
					</tr>
				<?php endforeach; ?>
				</tbody>
			</table>

			<h2><?php esc_html_e( '3) How to register a bucket', 'r2-offload' ); ?></h2>
			<?php foreach ( Providers::all() as $id => $p ) : ?>
				<details class="r2-setup-guide" <?php echo $id === $current ? 'open' : ''; ?>>
					<summary><strong><?php echo esc_html( $p['label'] ); ?></strong></summary>
					<ol class="r2-guide-steps">
						<?php foreach ( (array) $p['guide'] as $step ) : ?>
							<li><?php echo wp_kses_post( make_clickable( esc_html( $step ) ) ); ?></li>
						<?php endforeach; ?>
					</ol>
				</details>
			<?php endforeach; ?>
		</div>
		<?php
	}
}
