<?php
/**
 * Plugin activation handler for Zoventic Invoices.
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class ZINV_Activator {

    public static function activate(): void {
        self::check_innodb();

        $plugin_dir      = defined( 'ZINV_PLUGIN_DIR' ) ? ZINV_PLUGIN_DIR : plugin_dir_path( __DIR__ );
        $plugin_basename = defined( 'ZINV_PLUGIN_BASENAME' ) ? ZINV_PLUGIN_BASENAME : 'zoventic-invoices/zoventic-invoices.php';

        require_once $plugin_dir . 'includes/class-installer.php';

        try {
            ZINV_Installer::create_tables();
            ZINV_Installer::register_capabilities();
            ZINV_Installer::seed_defaults();
        } catch ( \Throwable $e ) {
            // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
            error_log( '[ZINV_Activator] activation failed: ' . $e->getMessage() );
            deactivate_plugins( $plugin_basename );
            wp_die(
                esc_html__( 'Zoventic Invoices installation failed. Please check your server error log.', 'zoventic-invoices' ),
                esc_html__( 'Zoventic Invoices — Installation Error', 'zoventic-invoices' ),
                [ 'response' => 500, 'back_link' => true ]
            );
        }

        $version = defined( 'ZINV_VERSION' ) ? ZINV_VERSION : '1.0.0';
        update_option( 'zinv_db_version', $version, false );
        update_option( 'zinv_activated_at', gmdate( 'Y-m-d H:i:s' ), false );

        flush_rewrite_rules( false );
    }

    private static function check_innodb(): void {
        global $wpdb;
        $plugin_basename = defined( 'ZINV_PLUGIN_BASENAME' ) ? ZINV_PLUGIN_BASENAME : 'zoventic-invoices/zoventic-invoices.php';

        // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $engines = $wpdb->get_results( 'SHOW ENGINES', ARRAY_A );
        // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching

        $has_innodb = false;
        if ( is_array( $engines ) ) {
            foreach ( $engines as $e ) {
                if ( isset( $e['Engine'] ) && 'innodb' === strtolower( $e['Engine'] ) ) {
                    $support = strtoupper( $e['Support'] ?? '' );
                    if ( 'YES' === $support || 'DEFAULT' === $support ) {
                        $has_innodb = true;
                        break;
                    }
                }
            }
        }

        if ( ! $has_innodb ) {
            deactivate_plugins( $plugin_basename );
            wp_die(
                esc_html__( 'Zoventic Invoices requires the InnoDB storage engine, which is not available on this server.', 'zoventic-invoices' ),
                esc_html__( 'Zoventic Invoices — Incompatible Server', 'zoventic-invoices' ),
                [ 'response' => 500, 'back_link' => true ]
            );
        }

        $db_version = $wpdb->db_version();
        $is_maria   = stripos( $wpdb->db_server_info(), 'MariaDB' ) !== false;
        $min_needed = $is_maria ? '10.2' : '5.7.8';
        if ( version_compare( $db_version, $min_needed, '<' ) ) {
            deactivate_plugins( $plugin_basename );
            wp_die(
                sprintf(
                    /* translators: 1: required DB version, 2: detected version */
                    esc_html__( 'Zoventic Invoices requires %1$s or higher (detected %2$s).', 'zoventic-invoices' ),
                    esc_html( ( $is_maria ? 'MariaDB ' : 'MySQL ' ) . $min_needed ),
                    esc_html( $db_version )
                ),
                esc_html__( 'Zoventic Invoices — Incompatible Database Version', 'zoventic-invoices' ),
                [ 'response' => 500, 'back_link' => true ]
            );
        }
    }
}

