<?php
defined('ABSPATH') || exit;

add_action('wp_dashboard_setup', 'iv1_register_dashboard_widget');

function iv1_register_dashboard_widget(): void {
    if (!current_user_can('manage_options')) {
        return;
    }

    wp_add_dashboard_widget(
        'iv1_backup_dashboard',
        __('IV1 Backup Status', 'iv1-wp-tools'),
        'iv1_render_dashboard_widget'
    );
}

function iv1_render_dashboard_widget(): void {
    $schedule_opts = get_option(IV1WPT_SCHEDULE_OPTION, iv1wpt_default_schedule());
    $schedules_available = iv1wpt_get_available_schedules();

    $next_files = wp_next_scheduled('iv1wpt_cron_files');
    $next_db    = wp_next_scheduled('iv1wpt_cron_db');

    $next_files_str = $next_files ? wp_date('d.m.Y H:i', $next_files) : '—';
    $next_db_str    = $next_db ? wp_date('d.m.Y H:i', $next_db) : '—';

    $last_file_backup = '—';
    $last_db_backup   = '—';
    $total_size       = 0;

    if (is_dir(IV1WPT_BACKUP_DIR)) {
        $files = glob(trailingslashit(IV1WPT_BACKUP_DIR) . '*');

        if (!empty($files)) {
            foreach ($files as $file) {
                if (!is_file($file)) {
                    continue;
                }

                $basename = basename($file);
                $size     = filesize($file);

                if ($size !== false) {
                    $total_size += $size;
                }

                if (str_starts_with($basename, 'files-')) {
                    $last_file_backup = wp_date('d.m.Y H:i', filemtime($file));
                }

                if (str_starts_with($basename, 'db-')) {
                    $last_db_backup = wp_date('d.m.Y H:i', filemtime($file));
                }
            }
        }
    }

    ?>
	<div class="iv1-dashboard-backup-widget">
		<table class="widefat striped" style="margin:0;">
			<tbody>
				<tr>
					<td><?php esc_html_e('Next file backup', 'iv1-wp-tools'); ?></td>
					<td>
						<strong>
							<?php
							echo esc_html(
								($schedule_opts['files_schedule'] ?? 'off') === 'off'
									? __('deactivated', 'iv1-wp-tools')
									: $next_files_str . ' (' . ($schedules_available[$schedule_opts['files_schedule']] ?? '—') . ')'
							);
							?>
						</strong>
					</td>
				</tr>

				<tr>
					<td><?php esc_html_e('Next DB backup', 'iv1-wp-tools'); ?></td>
					<td>
						<strong>
							<?php
							echo esc_html(
								($schedule_opts['db_schedule'] ?? 'off') === 'off'
									? __('deactivated', 'iv1-wp-tools')
									: $next_db_str . ' (' . ($schedules_available[$schedule_opts['db_schedule']] ?? '—') . ')'
							);
							?>
						</strong>
					</td>
				</tr>

				<tr>
					<td><?php esc_html_e('Last file backup', 'iv1-wp-tools'); ?></td>
					<td><?php echo esc_html($last_file_backup); ?></td>
				</tr>

				<tr>
					<td><?php esc_html_e('Last DB backup', 'iv1-wp-tools'); ?></td>
					<td><?php echo esc_html($last_db_backup); ?></td>
				</tr>

				<tr>
					<td><?php esc_html_e('Backup storage used', 'iv1-wp-tools'); ?></td>
					<td><?php echo esc_html(size_format($total_size, 2)); ?></td>
				</tr>
			</tbody>
		</table>

		<p style="margin-top:12px;">
			<a href="<?php echo esc_url(admin_url('admin.php?page=iv1-wp-tools')); ?>">
				<?php esc_html_e('Open backup details', 'iv1-wp-tools'); ?>
			</a>
		</p>
	</div>
    <?php
}