<?php

namespace Kitgenix\PDF_Invoicing\Modules\Settings;

defined( 'ABSPATH' ) || exit;

/**
 * Read-only diagnostics for the admin System Status panel.
 *
 * Deliberately avoids exposing anything sensitive: no API keys/secrets
 * exist in this plugin's settings, but this class still only surfaces
 * environment facts (versions, writability, active template) – never
 * customer data, order content, or file paths outside the uploads root.
 */
final class SystemStatus {

    /**
     * @return array<string,array<string,string>>
     */
    public static function get_report(): array {
        $report = [
            'environment' => [
                'PHP version'      => PHP_VERSION,
                'WordPress version'=> function_exists( 'get_bloginfo' ) ? (string) get_bloginfo( 'version' ) : 'n/a',
                'WooCommerce version' => defined( 'WC_VERSION' ) ? (string) WC_VERSION : 'n/a',
                'Plugin version'   => defined( 'KITGENIX_PDF_INVOICING_FOR_WOOCOMMERCE_VERSION' ) ? (string) KITGENIX_PDF_INVOICING_FOR_WOOCOMMERCE_VERSION : 'n/a',
                'Dompdf version'   => self::get_dompdf_version(),
                'HPOS (custom order tables)' => self::is_hpos_enabled() ? __( 'Enabled', 'kitgenix-pdf-invoicing-for-woocommerce' ) : __( 'Disabled (legacy post storage)', 'kitgenix-pdf-invoicing-for-woocommerce' ),
            ],
            'php_limits' => [
                __( 'Memory limit', 'kitgenix-pdf-invoicing-for-woocommerce' )        => (string) ini_get( 'memory_limit' ),
                __( 'Max execution time', 'kitgenix-pdf-invoicing-for-woocommerce' )  => (string) ini_get( 'max_execution_time' ) . 's',
                __( 'Upload max filesize', 'kitgenix-pdf-invoicing-for-woocommerce' ) => (string) ini_get( 'upload_max_filesize' ),
            ],
            'storage' => self::get_storage_report(),
            'template' => [
                'Active template style' => self::get_active_template_style(),
                'Atomic sequence table' => self::sequence_table_status(),
            ],
        ];

        return $report;
    }

    private static function get_dompdf_version(): string {
        if ( ! defined( 'KITGENIX_PDF_INVOICING_FOR_WOOCOMMERCE_PATH' ) ) {
            return 'n/a';
        }

        $version_file = KITGENIX_PDF_INVOICING_FOR_WOOCOMMERCE_PATH . 'vendor/dompdf/dompdf/VERSION';
        if ( ! is_readable( $version_file ) ) {
            return class_exists( \Dompdf\Dompdf::class ) ? __( 'Installed (version file unavailable)', 'kitgenix-pdf-invoicing-for-woocommerce' ) : __( 'Not installed – run composer install', 'kitgenix-pdf-invoicing-for-woocommerce' );
        }

        $version = trim( (string) file_get_contents( $version_file ) );

        return '' !== $version ? $version : 'n/a';
    }

    private static function is_hpos_enabled(): bool {
        if ( ! function_exists( 'wc_get_container' ) ) {
            return false;
        }

        try {
            $container  = \wc_get_container();
            $controller = $container->get( \Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController::class );

            return (bool) ( $controller && $controller->custom_orders_table_usage_is_enabled() );
        } catch ( \Throwable $exception ) {
            return false;
        }
    }

    /**
     * @return array<string,string>
     */
    private static function get_storage_report(): array {
        $upload_dir = function_exists( 'wp_upload_dir' ) ? wp_upload_dir() : [];
        $basedir    = ! empty( $upload_dir['basedir'] ) && is_string( $upload_dir['basedir'] ) ? $upload_dir['basedir'] : '';

        $report = [
            'Uploads directory writable' => ( '' !== $basedir && wp_is_writable( $basedir ) )
                ? __( 'Yes', 'kitgenix-pdf-invoicing-for-woocommerce' )
                : __( 'No – archived PDFs cannot be saved', 'kitgenix-pdf-invoicing-for-woocommerce' ),
        ];

        if ( '' !== $basedir ) {
            $archive_dir = trailingslashit( $basedir ) . 'kitgenix-pdf-invoicing-for-woocommerce/archive';
            $report['Archive directory'] = is_dir( $archive_dir )
                ? ( wp_is_writable( $archive_dir ) ? __( 'Exists and writable', 'kitgenix-pdf-invoicing-for-woocommerce' ) : __( 'Exists but NOT writable', 'kitgenix-pdf-invoicing-for-woocommerce' ) )
                : __( 'Not created yet (will be created on first document generation)', 'kitgenix-pdf-invoicing-for-woocommerce' );
        }

        return $report;
    }

    private static function get_active_template_style(): string {
        $settings = Settings::get_all();

        return isset( $settings['template_style'] ) ? (string) $settings['template_style'] : 'standard';
    }

    private static function sequence_table_status(): string {
        if ( ! class_exists( \Kitgenix\PDF_Invoicing\Modules\Invoicing\SequenceManager::class ) ) {
            return 'n/a';
        }

        return \Kitgenix\PDF_Invoicing\Modules\Invoicing\SequenceManager::table_exists()
            ? __( 'Active (atomic numbering)', 'kitgenix-pdf-invoicing-for-woocommerce' )
            : __( 'Unavailable – falling back to non-atomic numbering. See documentation.', 'kitgenix-pdf-invoicing-for-woocommerce' );
    }
}
