<?php

namespace SmartSyncS3\Models;
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
 * Class S3SyncSettings
 *
 * This class manages S3 synchronization settings. It provides methods to load, save, reset,
 * and update settings. The settings are stored as a single option in the WordPress database.
 * This class handles properties like synchronization state, processed files, direction,
 * total files, extensions, and running batches.
 * @package SmartSyncS3
 * @author Infinitie Technologies
 *
 * Credits:
 * - Developed by Infinitie Technologies
 * - Maintained by the Infintitie Development Team
 * - Inspired by best practices for WordPress plugin development
 *
 */
class SMARL_S3SyncSettings {
    /** @var array List of successfully synchronized files */
    public array $s3SyncSuccesses = [];

    /** @var array List of synchronization errors */
    public array $s3SyncErrors = [];

    /** @var string Current state of the synchronization process */
    public string $syncState = 'default';

    /** @var string Status of the S3 synchronization (e.g., idle, running) */
    public string $s3SyncStatus = 'idle';

    /** @var int Number of processed files */
    public int $s3SyncProcessedFiles = 0;

    /** @var string Direction of synchronization (e.g., upload, download) */
    public string $s3SyncDirection = '';

    /** @var int Total number of files for synchronization */
    public int $s3SyncTotalFiles = 0;

    /** @var array List of all files to be synchronized */
    public array $s3SyncAllFiles = [];

    /** @var array List of allowed file extensions */
    public array $extensions = [];

    /** @var string Option name used to store settings in the database */
    private string $optionName = 'smarl_sync_settings';


    public string $syncType = 'media';

    /**
     * Load settings from the WordPress database and populate the class properties.
     *
     * This method retrieves settings stored as an option and applies them to the class properties.
     */
    public function load(): void {
        $data = get_option($this->optionName, []);

        foreach ($data as $key => $value) {
            if (property_exists($this, $key)) {
                $this->$key = $value;
            }
        }
    }

    /**
     * Save all public properties to the WordPress database.
     *
     * This method converts the class properties into an associative array and saves them
     * as a single option in the database, excluding internal properties like $optionName.
     *
     * @developer Jay Parmar <infinitie.jay@gmail.com>
     */
    public function save(): void {
        $data = [];
        foreach (get_object_vars($this) as $key => $value) {
            if ($key !== 'optionName') {
                $data[$key] = $value;
            }
        }
        update_option($this->optionName, $data);
    }

    /**
     * Reset all settings to their default values and save them.
     *
     * This method creates a new instance of the class to get default values,
     * applies them to the current instance, and saves the settings to the database.
     *
     * @developer Jay Parmar <infinitie.jay@gmail.com>
     */
    public function reset(): void {
        $defaultInstance = new self();
        foreach (get_object_vars($defaultInstance) as $key => $value) {
            if (property_exists($this, $key)) {
                $this->$key = $value;
            }
        }
        $this->save();
    }

    /**
     * Create an instance of the class from an array of data.
     *
     * @param array $data Associative array of settings data.
     * @return self An instance of the class populated with the provided data.
     *
     * @developer Jay Parmar <infinitie.jay@gmail.com>
     */
    public static function fromArray(array $data): self {
        $instance = new self();
        foreach ($data as $key => $value) {
            if (property_exists($instance, $key)) {
                $instance->$key = $value;
            }
        }
        return $instance;
    }

    /**
     * Convert all properties of the class to an associative array.
     *
     * @return array An array representation of the class properties.
     *
     * @developer Jay Parmar <infinitie.jay@gmail.com>
     */
    public function toArray(): array {
        $data = [];
        foreach (get_object_vars($this) as $key => $value) {
            if ($key !== 'optionName') {
                $data[$key] = $value;
            }
        }
        return $data;
    }
}
