<?php
    if ( ! defined( 'ABSPATH' ) ) exit;

    class cloudS3StorageImageFunction {

        static function getImageAttach () {
            $attachments = get_posts(array(
                'post_type' => 'attachment',
                'posts_per_page' => -1,
                'post_status' => 'any'
            ));
            
            return $attachments;
        }

        static function getFileAttachFullVersion() {   
            $file_list = [];

            $cached_file_list = get_transient('cloud_s3_storage_cached_file_attach_list');
        
            if ($cached_file_list !== false) {
                return $cached_file_list;
            }

            $file_attach_list = self::getImageAttach();

            foreach ($file_attach_list as $attachment) {
                $mime_type = get_post_mime_type($attachment->ID);
                
                $s3_status = CloudS3StorageDB::get_image_status($attachment->ID);
           
                $attachment_file_path = get_attached_file($attachment->ID);
                $original_file = basename($attachment_file_path);
                $cloud_s3_storageAttach = cloudS3StorageAttach::getRelativePathByAttach($attachment);

                $file_list[$original_file] = [
                    'image_id' => $attachment->ID,
                    'url' => self::sanitize_key_static(str_replace(wp_upload_dir()['basedir'], '', $attachment_file_path)),
                    'complete_path' => $attachment_file_path,
                    'checker' => [
                        'enable' => $cloud_s3_storageAttach['checked_en'],
                        'disable' => $cloud_s3_storageAttach['checked_ds']
                    ]
                ];
        
                if (strpos($mime_type, 'image/') === 0) {
                    $metadata = wp_get_attachment_metadata($attachment->ID);
                    if ($metadata && array_key_exists('sizes', $metadata)) {
                        $base_path = dirname($attachment_file_path) . '/';
                        foreach ($metadata['sizes'] as $file) {
                            $full_path = $base_path . $file['file'];
                            $relative_path = str_replace(wp_upload_dir()['basedir'], '', $full_path);
        
                            $file_list[$file['file']] = [
                                'image_id' => $attachment->ID,
                                'url' => self::sanitize_key_static($relative_path),
                                'complete_path' => $full_path,
                                'checker' => [
                                    'enable' => $cloud_s3_storageAttach['checked_en'],
                                    'disable' => $cloud_s3_storageAttach['checked_ds']
                                ]
                            ];
                        }
                    }
                }
            }
            set_transient('cloud_s3_storage_cached_file_attach_list', $file_list, 3600);
       
            return $file_list;
        }       
        
        static function getIdImageAttachFullVersion() {  
            $cached_id_list = get_transient('cloud_s3_storage_cached_id_list');
        
            if ($cached_id_list !== false) {
                return $cached_id_list;
            }
        
            $id_list = [];
        
            foreach (self::getImageAttach() as $attachment) {
                $s3_status = CloudS3StorageDB::get_image_status($attachment->ID);
                
                if ($s3_status !== 'transferred') {
                    $id_list[] = $attachment->ID;
                }
            }
        
            set_transient('cloud_s3_storage_cached_id_list', $id_list, 3600);
        
            return $id_list;
        }

        private static function sanitize_key_static($key) {
            return trim($key, '/');
        }
    }