<?php

defined('ABSPATH') || exit;

if (!class_exists('ConnectUpdateChecker')) {
    // class ConnectUpdateChecker {
    //     public $plugin_slug;
    //     public $version;
    //     public $cache_key;

    //     public function __construct() {
    //         $this->plugin_slug = plugin_basename(__DIR__); // e.g., 121-connect/121-connect.php
    //         $this->version = '1.0.0'; // Current plugin version
    //         $this->cache_key = 'connect_custom_upd';

    //         add_filter('plugins_api', array($this, 'info'), 20, 3);
    //         add_filter('site_transient_update_plugins', array($this, 'update'));
    //         add_action('upgrader_process_complete', array($this, 'purge'), 10, 2);
    //     }

    //     public function request() {
    //         $remote = get_transient($this->cache_key);

    //         if (false === $remote) {
    //             // Make a request to the update server
    //             $remote = wp_remote_get(
    //                 'https://wp-update-service.121domains.co.uk/121connect/update.php',
    //                 array(
    //                     'timeout' => 10,
    //                     'headers' => array('Accept' => 'application/json')
    //                 )
    //             );

    //             if (
    //                 is_wp_error($remote) ||
    //                 200 !== wp_remote_retrieve_response_code($remote) ||
    //                 empty(wp_remote_retrieve_body($remote))
    //             ) {
    //                 return false; // Handle error appropriately
    //             }

    //             set_transient($this->cache_key, $remote, DAY_IN_SECONDS);
    //         }

    //         return json_decode(wp_remote_retrieve_body($remote));
    //     }

    //     public function info($res, $action, $args) {
    //         // Do nothing if you're not getting plugin information
    //         if ('plugin_information' !== $action) {
    //             return $res;
    //         }

    //         // Do nothing if it is not our plugin
    //         if ($this->plugin_slug !== $args->slug) {
    //             return $res;
    //         }

    //         // Get updates
    //         $remote = $this->request();

    //         if (!$remote) {
    //             return $res;
    //         }

    //         $res = new stdClass();

    //         $res->name = $remote->name;
    //         $res->slug = $remote->slug;
    //         $res->version = $remote->version;
    //         $res->tested = $remote->tested;
    //         $res->requires = $remote->requires;
    //         $res->author = $remote->author;
    //         $res->homepage = $remote->homepage;
    //         $res->download_link = $remote->download_url;

    //         // Add sections for additional information
    //         $res->sections = array(
    //             'description' => $remote->sections->description,
    //             'changelog' => $remote->sections->changelog
    //         );

    //         return $res;
    //     }

    //     public function update($transient) {
    //         if (empty($transient->checked)) {
    //             return $transient;
    //         }

    //         die("HERE");

    //         $remote = $this->request();

    //         if ($remote && version_compare($this->version, $remote->version, '<')) {
    //             $res = new stdClass();
    //             $res->slug = $this->plugin_slug;
    //             $res->plugin = plugin_basename(__FILE__); // e.g., 121-connect/121-connect.php
    //             $res->new_version = $remote->version;
    //             $res->tested = $remote->tested;
    //             $res->package = $remote->download_url;

    //             $transient->response[$res->plugin] = $res;
    //         }

    //         return $transient;
    //     }

    //     public function purge($upgrader, $options) {
    //         if ('update' === $options['action'] && 'plugin' === $options['type']) {
    //             // Clean the cache when a new plugin version is installed
    //             delete_transient($this->cache_key);
    //         }
    //     }
    // }

    // new ConnectUpdateChecker();
}
