<?php

/**
 * JCOGS Image Pro - Add-on Setup File
 * ====================================
 * 
 * @category   ExpressionEngine Add-on
 * @package    JCOGS Image Pro
 * @author     JCOGS Design <contact@jcogs.net>
 * @copyright  Copyright (c) 2021 - 2025 JCOGS Design
 * @license    https://jcogs.net/add-ons/license/jcogs_img_pro
 * @version    2.0.6
 * @link       https://JCOGS.net/
 * @since      2.0.0
 */

// Load Composer autoloader
require_once PATH_THIRD . "jcogs_img_pro/vendor/autoload.php";

$addonJson = json_decode(file_get_contents(__DIR__ . '/addon.json'));

// Version constant
defined("JCOGS_IMG_PRO_VERSION") || define('JCOGS_IMG_PRO_VERSION', $addonJson->version);

// Class constant
defined("JCOGS_IMG_PRO_CLASS") || define('JCOGS_IMG_PRO_CLASS', $addonJson->class);

// Add-on name
defined("JCOGS_IMG_PRO_NAME") || define('JCOGS_IMG_PRO_NAME', $addonJson->name);

if (!function_exists('jcogs_img_pro_prune_windows_heic_binaries_on_non_windows')) {
    /**
     * Removes Windows-only HEIC converter artifacts on non-Windows hosts.
     *
     * This provides a runtime safety net in case vendor files are restored by
     * composer outside add-on install/upgrade events.
     */
    function jcogs_img_pro_prune_windows_heic_binaries_on_non_windows(): void
    {
        if (!defined('PATH_THIRD')) {
            return;
        }

        $is_windows = false;

        if (defined('PHP_OS_FAMILY')) {
            $is_windows = (PHP_OS_FAMILY === 'Windows');
        } else {
            $is_windows = (stripos(PHP_OS, 'WIN') === 0);
        }

        if ($is_windows) {
            return;
        }

        $files_to_remove = [
            PATH_THIRD . 'jcogs_img_pro/vendor/maestroerror/php-heic-to-jpg/bin/heicToJpg.exe',
            PATH_THIRD . 'jcogs_img_pro/vendor/maestroerror/php-heic-to-jpg/bin/php-heic-to-jpg-windows.zip',
            PATH_THIRD . 'jcogs_img_pro/vendor/bin/heicToJpgWin',
            PATH_THIRD . 'jcogs_img_pro/vendor/bin/heif-converter-windows',
        ];

        foreach ($files_to_remove as $file_path) {
            if (is_file($file_path)) {
                @unlink($file_path);
            }
        }
    }
}

jcogs_img_pro_prune_windows_heic_binaries_on_non_windows();


return [
    'author'             => $addonJson->author,
    'author_url'         => $addonJson->author_url,
    'name'               => $addonJson->name,
    'description'        => $addonJson->description,
    'version'            => $addonJson->version,
    'namespace'          => $addonJson->namespace,
    'settings_exist'     => $addonJson->settings_exist,
    'docs_url'           => $addonJson->docs_url,
    
    // Core native EE7 services
    'services'           => [
        'Settings' => function($ee) {
            return new \JCOGSDesign\JCOGSImagePro\Service\Settings();
        },
        'ColourManagementService' => function($ee) {
            $settings = $ee->make('jcogs_img_pro:Settings');
            return new \JCOGSDesign\JCOGSImagePro\Service\ColourManagementService($settings);
        },
        'CacheKeyGenerator' => function($ee) {
            $settings = $ee->make('jcogs_img_pro:Settings');
            return new \JCOGSDesign\JCOGSImagePro\Service\CacheKeyGenerator($settings);
        },
        'Utilities' => function($ee) {
            return new \JCOGSDesign\JCOGSImagePro\Service\Utilities();
        },
        'HttpClientService' => function($ee) {
            return new \JCOGSDesign\JCOGSImagePro\Service\HttpClientService();
        },
        'UpdateMarkerScriptService' => function($ee) {
            return new \JCOGSDesign\JCOGSImagePro\Service\UpdateMarkerScriptService();
        },
        'UpdateCheckService' => function($ee) {
            $httpClient = $ee->make('jcogs_img_pro:HttpClientService');
            $utilities = $ee->make('jcogs_img_pro:Utilities');
            $updateMarkerScriptService = $ee->make('jcogs_img_pro:UpdateMarkerScriptService');
            return new \JCOGSDesign\JCOGSImagePro\Service\UpdateCheckService($httpClient, $utilities, $updateMarkerScriptService);
        },
        'ImageProcessingPipeline' => function($ee) {
            $cache_key_generator = $ee->make('jcogs_img_pro:CacheKeyGenerator');
            
            // Get Pro utilities service
            $utilities = $ee->make('jcogs_img_pro:Utilities');
            
            return new \JCOGSDesign\JCOGSImagePro\Service\ImageProcessingPipeline($cache_key_generator, $utilities);
        },
        'ImageProcessingService' => function($ee) {
            return new \JCOGSDesign\JCOGSImagePro\Service\ImageProcessingService();
        },
        'FilesystemService' => function($ee) {
            $settings = $ee->make('jcogs_img_pro:Settings');
            $utilities = $ee->make('jcogs_img_pro:Utilities');
            return new \JCOGSDesign\JCOGSImagePro\Service\FilesystemService($settings, $utilities);
        },
        'ImageUtilities' => function($ee) {
            $filesystem_service = $ee->make('jcogs_img_pro:FilesystemService');
            return new \JCOGSDesign\JCOGSImagePro\Service\ImageUtilities($filesystem_service);
        },
        'CacheManagementService' => function($ee) {
            return new \JCOGSDesign\JCOGSImagePro\Service\CacheManagementService();
        },
        'PerformanceService' => function($ee) {
            return new \JCOGSDesign\JCOGSImagePro\Service\PerformanceService();
        },
        'ValidationService' => function($ee) {
            return new \JCOGSDesign\JCOGSImagePro\Service\ValidationService();
        },
        'ErrorNotificationService' => function($ee) {
            return new \JCOGSDesign\JCOGSImagePro\Service\ErrorNotificationService();
        },
        'PresetService' => function($ee) {
            return new \JCOGSDesign\JCOGSImagePro\Service\PresetService();
        },
        'LicenseRuntimeRepository' => function($ee) {
            return new \JCOGSDesign\JCOGSImagePro\Service\License\LicenseRuntimeRepository();
        },
        'CpLicenseStatusService' => function($ee) {
            return new \JCOGSDesign\JCOGSImagePro\Service\License\CpLicenseStatusService();
        },
        'CpLicensePolicyService' => function($ee) {
            $statusService = $ee->make('jcogs_img_pro:CpLicenseStatusService');
            $runtimeRepository = $ee->make('jcogs_img_pro:LicenseRuntimeRepository');

            return new \JCOGSDesign\JCOGSImagePro\Service\License\CpLicensePolicyService(
                $statusService,
                $runtimeRepository
            );
        },
        'CpLicenseGuard' => function($ee) {
            $policyService = $ee->make('jcogs_img_pro:CpLicensePolicyService');
            return new \JCOGSDesign\JCOGSImagePro\Service\License\CpLicenseGuard($policyService);
        },
        'CpLicenseNoticeRenderer' => function($ee) {
            return new \JCOGSDesign\JCOGSImagePro\Service\License\CpLicenseNoticeRenderer();
        },
        
        // Advanced features
        'LazyLoadingService' => function($ee) {
            return new \JCOGSDesign\JCOGSImagePro\Service\Pipeline\LazyLoadingService();
        },
        'ResponsiveImageService' => function($ee) {
            return new \JCOGSDesign\JCOGSImagePro\Service\Pipeline\ResponsiveImageService();
        },
        'OutputGenerationService' => function($ee) {
            return new \JCOGSDesign\JCOGSImagePro\Service\Pipeline\OutputGenerationService();
        },
        
        // Provider Architecture - v2.0.0-rc4
        'ProviderRegistry' => function($ee) {
            $registry = new \JCOGSDesign\JCOGSImagePro\Service\ProviderRegistry();
            
            // Register built-in HAAR provider (priority 10)
            // Note: FaceDetectionService will be created later, so we pass null
            // and let the provider create it when needed
            $registry->registerFaceRecognitionProvider(
                new \JCOGSDesign\JCOGSImagePro\Service\Provider\HaarFaceDetectionProvider()
            );
            
            // Trigger extension hook for companion add-ons to register their providers
            // Companion add-ons can register higher-priority providers (20+ for local, 100+ for cloud)
            if (isset(ee()->extensions) && ee()->extensions->active_hook('jcogs_img_pro_register_providers')) {
                ee()->extensions->call('jcogs_img_pro_register_providers', $registry);
            }
            
            return $registry;
        },
    ],
    
    'models' => [
        'Preset' => 'Models\Preset',
    ],
    
    // Variable Modifiers
    'modifiers' => [
        'jcogs_img'
    ],
    
    'requires'       => [
        'php'   => $addonJson->require->php,
        'ee'    => $addonJson->require->expressionengine
    ],
];

