<?php
/**
 * Connection Tab
 *
 * @var array $settings
 * @var array|null $tenant_defaults
 * @var array $companies
 * @var bool $has_api_connection
 * @var string $currentEnv
 * @var bool $isCustom
 * @var string $customUrl
 * @var bool $hasProductionToken
 * @var bool $hasSandboxToken
 * @var bool $hasStagingToken
 * @var bool $hasCustomToken
 */

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

$tenantMunicipalServiceCode = (string) ($tenant_defaults['municipal_service_code'] ?? $tenant_defaults['service_code'] ?? 'N/A');
$tenantMunicipality = (string) ($tenant_defaults['municipality_code'] ?? 'N/A');
?>

<h3><?php esc_html_e('API Configuration', 'woocommerce-cloudxm-nfe'); ?></h3>
<p>
    <button type="button" id="cloudxm-nfe-test-connection" class="button">
        <?php esc_html_e('Test Connection', 'woocommerce-cloudxm-nfe'); ?>
    </button>
    <span id="cloudxm-nfe-connection-status" class="description" data-status="idle" style="margin-left: 10px;"></span>
</p>
<table class="form-table">
    <tr>
        <th scope="row"><label for="cloudxm_nfe_environment"><?php esc_html_e('Environment', 'woocommerce-cloudxm-nfe'); ?></label></th>
        <td>
            <select id="cloudxm_nfe_environment" name="cloudxm_nfe_environment" class="regular-text">
                <option value="production" <?php selected($currentEnv, 'production'); ?>><?php esc_html_e('Production (nfe.cloudxm.com.br)', 'woocommerce-cloudxm-nfe'); ?></option>
                <option value="sandbox" <?php selected($currentEnv, 'sandbox'); ?>><?php esc_html_e('Sandbox (sandbox-nfe.cloudxm.com.br)', 'woocommerce-cloudxm-nfe'); ?></option>
                <?php if (defined('WP_DEBUG') && WP_DEBUG): ?>
                <option value="staging" <?php selected($currentEnv, 'staging'); ?>><?php esc_html_e('Staging (stg-nfe.cloudxm.com.br)', 'woocommerce-cloudxm-nfe'); ?></option>
                <?php endif; ?>
                <option value="custom" <?php selected($currentEnv, 'custom'); ?>><?php esc_html_e('Custom', 'woocommerce-cloudxm-nfe'); ?></option>
            </select>
            <p class="description"><?php esc_html_e('Select the NFE server environment. The URL is set automatically for Production, Sandbox, and Staging.', 'woocommerce-cloudxm-nfe'); ?></p>
        </td>
    </tr>
    <tr id="custom-url-row" style="<?php echo $isCustom ? '' : 'display:none;'; ?>">
        <th scope="row"><label for="cloudxm_nfe_api_base_url"><?php esc_html_e('Custom API Base URL', 'woocommerce-cloudxm-nfe'); ?></label></th>
        <td>
            <input type="text" id="cloudxm_nfe_api_base_url" name="cloudxm_nfe_api_base_url" value="<?php echo esc_attr($customUrl); ?>" class="regular-text" placeholder="https://your-nfe-server.com/api">
            <p class="description"><?php esc_html_e('Base URL of your custom NFE API server.', 'woocommerce-cloudxm-nfe'); ?></p>
        </td>
    </tr>
    <tr>
        <th scope="row"><label for="cloudxm_nfe_token_production"><?php esc_html_e('Production API Token', 'woocommerce-cloudxm-nfe'); ?></label></th>
        <td>
            <input type="password" id="cloudxm_nfe_token_production" name="cloudxm_nfe_token_production" value="" class="regular-text" autocomplete="new-password">
            <p class="description">
                <?php esc_html_e('Leave blank to keep current token.', 'woocommerce-cloudxm-nfe'); ?>
                <br>
                <strong><?php esc_html_e('Status:', 'woocommerce-cloudxm-nfe'); ?></strong>
                <?php echo $hasProductionToken ? esc_html__('Configured', 'woocommerce-cloudxm-nfe') : esc_html__('Empty', 'woocommerce-cloudxm-nfe'); ?>
            </p>
        </td>
    </tr>
    <tr>
        <th scope="row"><label for="cloudxm_nfe_token_sandbox"><?php esc_html_e('Sandbox API Token', 'woocommerce-cloudxm-nfe'); ?></label></th>
        <td>
            <input type="password" id="cloudxm_nfe_token_sandbox" name="cloudxm_nfe_token_sandbox" value="" class="regular-text" autocomplete="new-password">
            <p class="description">
                <?php esc_html_e('Leave blank to keep current token.', 'woocommerce-cloudxm-nfe'); ?>
                <br>
                <strong><?php esc_html_e('Status:', 'woocommerce-cloudxm-nfe'); ?></strong>
                <?php echo $hasSandboxToken ? esc_html__('Configured', 'woocommerce-cloudxm-nfe') : esc_html__('Empty', 'woocommerce-cloudxm-nfe'); ?>
            </p>
        </td>
    </tr>
    <?php if (defined('WP_DEBUG') && WP_DEBUG): ?>
    <tr>
        <th scope="row"><label for="cloudxm_nfe_token_staging"><?php esc_html_e('Staging API Token', 'woocommerce-cloudxm-nfe'); ?></label></th>
        <td>
            <input type="password" id="cloudxm_nfe_token_staging" name="cloudxm_nfe_token_staging" value="" class="regular-text" autocomplete="new-password">
            <p class="description">
                <?php esc_html_e('API token for staging environment.', 'woocommerce-cloudxm-nfe'); ?>
                <br>
                <strong><?php esc_html_e('Status:', 'woocommerce-cloudxm-nfe'); ?></strong>
                <?php echo $hasStagingToken ? esc_html__('Configured', 'woocommerce-cloudxm-nfe') : esc_html__('Empty', 'woocommerce-cloudxm-nfe'); ?>
            </p>
        </td>
    </tr>
    <?php endif; ?>
    <tr id="custom-token-row" style="<?php echo $isCustom ? '' : 'display:none;'; ?>">
        <th scope="row"><label for="cloudxm_nfe_token_custom"><?php esc_html_e('Custom API Token', 'woocommerce-cloudxm-nfe'); ?></label></th>
        <td>
            <input type="password" id="cloudxm_nfe_token_custom" name="cloudxm_nfe_token_custom" value="" class="regular-text" autocomplete="new-password">
            <p class="description">
                <?php esc_html_e('Leave blank to keep current token.', 'woocommerce-cloudxm-nfe'); ?>
                <br>
                <strong><?php esc_html_e('Status:', 'woocommerce-cloudxm-nfe'); ?></strong>
                <?php echo $hasCustomToken ? esc_html__('Configured', 'woocommerce-cloudxm-nfe') : esc_html__('Empty', 'woocommerce-cloudxm-nfe'); ?>
            </p>
        </td>
    </tr>
</table>

<div class="notice <?php echo $has_api_connection ? 'notice-success' : 'notice-warning'; ?>">
    <p>
        <strong><?php esc_html_e('Server Connection:', 'woocommerce-cloudxm-nfe'); ?></strong>
        <?php echo $has_api_connection
            ? esc_html__('Connected', 'woocommerce-cloudxm-nfe')
            : esc_html__('Not connected (check token and base URL)', 'woocommerce-cloudxm-nfe'); ?>
    </p>
</div>

<h3><?php esc_html_e('Company Configuration', 'woocommerce-cloudxm-nfe'); ?></h3>
<table class="form-table">
    <tr>
        <th scope="row"><label for="cloudxm_nfe_selected_company_cnpj"><?php esc_html_e('Company', 'woocommerce-cloudxm-nfe'); ?></label></th>
        <td>
            <select id="cloudxm_nfe_selected_company_cnpj" name="cloudxm_nfe_selected_company_cnpj" class="regular-text">
                <option value=""><?php esc_html_e('— Use server default (primary company) —', 'woocommerce-cloudxm-nfe'); ?></option>
                <?php foreach ($companies as $company): ?>
                    <?php
                    $cnpj = $company['cnpj'] ?? '';
                    if (empty($cnpj)) {
                        continue;
                    }
                    $legalName = $company['legal_name'] ?? __('Unknown', 'woocommerce-cloudxm-nfe');
                    $formattedCnpj = $company['formatted_cnpj'] ?? $cnpj;
                    $isPrimary = !empty($company['is_primary']);
                    $label = $formattedCnpj . ' — ' . $legalName . ($isPrimary ? ' (primary)' : '');
                    ?>
                    <option value="<?php echo esc_attr($cnpj); ?>" <?php selected($settings['selected_company_cnpj'] ?? '', $cnpj); ?>>
                        <?php echo esc_html($label); ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <p class="description">
                <?php esc_html_e('Select the company to issue invoices for. If left empty, the server will use the primary company.', 'woocommerce-cloudxm-nfe'); ?>
                <?php if (empty($companies)): ?>
                    <br>
                    <em><?php esc_html_e('No companies found. Make sure the API connection is working.', 'woocommerce-cloudxm-nfe'); ?></em>
                <?php endif; ?>
            </p>
        </td>
    </tr>
</table>
