<?php

declare(strict_types=1);

namespace SiteFort\Modules\Settings;

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

use SiteFort\Includes\Interfaces\SettingsProvider;
use SiteFort\Modules\Cloud\Service\CloudSettings;

/**
 * Provider that exposes Cloud/License settings (read-only).
 */
class CloudSettingsProvider implements SettingsProvider {

	public function get_settings_schema(): array {
		return [
			'license_key'        => [ 'type' => 'string', 'default' => '', 'readonly' => true ],
			'license_status'     => [ 'type' => 'string', 'default' => '', 'readonly' => true ],
			'license_plan'       => [ 'type' => 'string', 'default' => 'free', 'readonly' => true ],
			'license_expires_at' => [ 'type' => 'integer', 'default' => 0, 'readonly' => true ],
		];
	}

	public function get_settings(): array {
		// Use get_all() (not get_public()) to access site_token for the credential check.
		$cloud = CloudSettings::get_all();
		$has_creds = ! empty( $cloud['site_token'] ) && ! empty( $cloud['site_secret'] );
		$license_status = (string) ( $cloud['license_status'] ?? '' );
		// Don't surface 'active' when credentials are missing - prevents misleading UI state.
		if ( $license_status === 'active' && ! $has_creds ) {
			$license_status = '';
		}
		return [
			'license_key'        => CloudSettings::mask_license_key_for_display( (string) ( $cloud['license_key'] ?? '' ) ),
			'license_status'     => $license_status,
			'license_plan'       => (string) ( $cloud['license_plan'] ?? 'free' ),
			'license_expires_at' => (int) ( $cloud['license_expires_at'] ?? 0 ),
		];
	}

	public function save_settings( array $values ): bool {
		// Read-only - nothing to save. License changes go through the Cloud module.
		return true;
	}
}
