<?php
/**
 * Installation Guide Template
 *
 * @package KiviCare_API
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
?>

<div class="wrap">
    <div class="care-api-docs">
        
        <!-- Header -->
        <div class="care-api-header">
            <div class="api-version">v<?php echo esc_html( CARE_API_VERSION ); ?></div>
            <h1><?php esc_html_e( 'Care API Installation Guide', 'care-api' ); ?></h1>
            <p><?php esc_html_e( 'Complete guide for installing and configuring the Care API plugin', 'care-api' ); ?></p>
        </div>

        <div class="api-docs-content">
            
            <!-- Requirements Section -->
            <div class="endpoint-section">
                <h2><?php esc_html_e( 'System Requirements', 'care-api' ); ?></h2>
                
                <table class="params-table">
                    <thead>
                        <tr>
                            <th><?php esc_html_e( 'Component', 'care-api' ); ?></th>
                            <th><?php esc_html_e( 'Minimum Version', 'care-api' ); ?></th>
                            <th><?php esc_html_e( 'Recommended', 'care-api' ); ?></th>
                            <th><?php esc_html_e( 'Notes', 'care-api' ); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td><strong>WordPress</strong></td>
                            <td>6.0</td>
                            <td>6.4+</td>
                            <td><?php esc_html_e( 'Latest stable version recommended', 'care-api' ); ?></td>
                        </tr>
                        <tr>
                            <td><strong>PHP</strong></td>
                            <td>8.1</td>
                            <td>8.2+</td>
                            <td><?php esc_html_e( 'Modern PHP version for better performance', 'care-api' ); ?></td>
                        </tr>
                        <tr>
                            <td><strong>MySQL</strong></td>
                            <td>5.7</td>
                            <td>8.0+</td>
                            <td><?php esc_html_e( 'MySQL 8.0+ or MariaDB 10.3+', 'care-api' ); ?></td>
                        </tr>
                        <tr>
                            <td><strong>Memory Limit</strong></td>
                            <td>128MB</td>
                            <td>256MB+</td>
                            <td><?php esc_html_e( 'Higher memory for large datasets', 'care-api' ); ?></td>
                        </tr>
                        <tr>
                            <td><strong>KiviCare Plugin</strong></td>
                            <td>3.0.0</td>
                            <td><?php esc_html_e( 'Latest', 'care-api' ); ?></td>
                            <td><?php esc_html_e( 'Base KiviCare plugin required', 'care-api' ); ?></td>
                        </tr>
                    </tbody>
                </table>

                <div class="notice notice-info">
                    <h4><?php esc_html_e( 'Server Requirements', 'care-api' ); ?></h4>
                    <ul>
                        <li><?php esc_html_e( 'cURL extension enabled', 'care-api' ); ?></li>
                        <li><?php esc_html_e( 'JSON extension enabled', 'care-api' ); ?></li>
                        <li><?php esc_html_e( 'OpenSSL extension for JWT tokens', 'care-api' ); ?></li>
                        <li><?php esc_html_e( 'mod_rewrite enabled for pretty permalinks', 'care-api' ); ?></li>
                    </ul>
                </div>
            </div>

            <!-- Installation Steps -->
            <div class="endpoint-section">
                <h2><?php esc_html_e( 'Installation Steps', 'care-api' ); ?></h2>
                
                <h3><?php esc_html_e( 'Step 1: Install KiviCare Base Plugin', 'care-api' ); ?></h3>
                <p><?php esc_html_e( 'Before installing the API plugin, ensure the base KiviCare plugin is installed and activated:', 'care-api' ); ?></p>
                <ol>
                    <li><?php esc_html_e( 'Download KiviCare plugin from official source', 'care-api' ); ?></li>
                    <li><?php esc_html_e( 'Upload and activate through WordPress admin', 'care-api' ); ?></li>
                    <li><?php esc_html_e( 'Complete KiviCare initial setup wizard', 'care-api' ); ?></li>
                    <li><?php esc_html_e( 'Verify database tables are created', 'care-api' ); ?></li>
                </ol>

                <h3><?php esc_html_e( 'Step 2: Install Care API Plugin', 'care-api' ); ?></h3>
                
                <h4><?php esc_html_e( 'Method 1: WordPress Admin Upload', 'care-api' ); ?></h4>
                <ol>
                    <li><?php esc_html_e( 'Go to Plugins > Add New > Upload Plugin', 'care-api' ); ?></li>
                    <li><?php esc_html_e( 'Select the care-api.zip file', 'care-api' ); ?></li>
                    <li><?php esc_html_e( 'Click "Install Now" and then "Activate"', 'care-api' ); ?></li>
                </ol>

                <h4><?php esc_html_e( 'Method 2: FTP Upload', 'care-api' ); ?></h4>
                <ol>
                    <li><?php esc_html_e( 'Extract the plugin files', 'care-api' ); ?></li>
                    <li><?php esc_html_e( 'Upload the care-api folder to /wp-content/plugins/', 'care-api' ); ?></li>
                    <li><?php esc_html_e( 'Activate the plugin from WordPress admin', 'care-api' ); ?></li>
                </ol>

                <h4><?php esc_html_e( 'Method 3: WP-CLI Installation', 'care-api' ); ?></h4>
                <div class="code-example">
                    <div class="code-example-header">
                        <span class="code-language">bash</span>
                        <button class="copy-button" title="<?php esc_attr_e( 'Copy to clipboard', 'care-api' ); ?>">
                            <i class="dashicons dashicons-clipboard"></i>
                        </button>
                    </div>
                    <div class="code-content"># Install from zip file
wp plugin install /path/to/care-api.zip --activate

# Or install from directory
wp plugin activate care-api</div>
                </div>
            </div>

            <!-- Configuration -->
            <div class="endpoint-section">
                <h2><?php esc_html_e( 'Initial Configuration', 'care-api' ); ?></h2>
                
                <h3><?php esc_html_e( 'Step 1: Verify Installation', 'care-api' ); ?></h3>
                <p><?php esc_html_e( 'After activation, check that the plugin is working correctly:', 'care-api' ); ?></p>
                <ul>
                    <li><?php esc_html_e( 'Navigate to Care API menu in WordPress admin', 'care-api' ); ?></li>
                    <li><?php esc_html_e( 'Check the System Status section shows all green', 'care-api' ); ?></li>
                    <li><?php printf( 
                        esc_html__( 'Test the API status endpoint: %s', 'care-api' ), 
                        '<code>' . esc_url( rest_url( 'care/v1/status' ) ) . '</code>'
                    ); ?></li>
                </ul>

                <h3><?php esc_html_e( 'Step 2: Configure Permalinks', 'care-api' ); ?></h3>
                <p><?php esc_html_e( 'WordPress REST API requires pretty permalinks:', 'care-api' ); ?></p>
                <ol>
                    <li><?php esc_html_e( 'Go to Settings > Permalinks', 'care-api' ); ?></li>
                    <li><?php esc_html_e( 'Select any option except "Plain"', 'care-api' ); ?></li>
                    <li><?php esc_html_e( 'Click "Save Changes"', 'care-api' ); ?></li>
                </ol>

                <h3><?php esc_html_e( 'Step 3: Set Up User Roles', 'care-api' ); ?></h3>
                <p><?php esc_html_e( 'The plugin automatically creates necessary capabilities, but you may need to assign roles:', 'care-api' ); ?></p>
                <ul>
                    <li><strong><?php esc_html_e( 'Administrators:', 'care-api' ); ?></strong> <?php esc_html_e( 'Full API access automatically granted', 'care-api' ); ?></li>
                    <li><strong><?php esc_html_e( 'Doctors:', 'care-api' ); ?></strong> <?php esc_html_e( 'Medical data access for their patients', 'care-api' ); ?></li>
                    <li><strong><?php esc_html_e( 'Patients:', 'care-api' ); ?></strong> <?php esc_html_e( 'Read access to their own data', 'care-api' ); ?></li>
                    <li><strong><?php esc_html_e( 'Receptionists:', 'care-api' ); ?></strong> <?php esc_html_e( 'Appointment management access', 'care-api' ); ?></li>
                </ul>
            </div>

            <!-- WordPress Configuration -->
            <div class="endpoint-section">
                <h2><?php esc_html_e( 'WordPress Configuration', 'care-api' ); ?></h2>
                
                <h3><?php esc_html_e( 'wp-config.php Settings', 'care-api' ); ?></h3>
                <p><?php esc_html_e( 'Add these optional constants to your wp-config.php file for advanced configuration:', 'care-api' ); ?></p>
                
                <div class="code-example">
                    <div class="code-example-header">
                        <span class="code-language">PHP</span>
                        <button class="copy-button" title="<?php esc_attr_e( 'Copy to clipboard', 'care-api' ); ?>">
                            <i class="dashicons dashicons-clipboard"></i>
                        </button>
                    </div>
                    <div class="code-content">// Care API Configuration
define( 'CARE_API_DEBUG', false ); // Enable debug mode
define( 'CARE_API_CACHE_TTL', 3600 ); // Cache time to live (seconds)
define( 'KIVICARE_JWT_SECRET', 'your-custom-jwt-secret-key' ); // Custom JWT secret

// Optional: Increase memory and execution time
define( 'WP_MEMORY_LIMIT', '256M' );
ini_set( 'max_execution_time', 300 );</div>
                </div>

                <h3><?php esc_html_e( '.htaccess Configuration', 'care-api' ); ?></h3>
                <p><?php esc_html_e( 'For Apache servers, ensure these rules are in your .htaccess file:', 'care-api' ); ?></p>
                
                <div class="code-example">
                    <div class="code-example-header">
                        <span class="code-language">Apache</span>
                        <button class="copy-button" title="<?php esc_attr_e( 'Copy to clipboard', 'care-api' ); ?>">
                            <i class="dashicons dashicons-clipboard"></i>
                        </button>
                    </div>
                    <div class="code-content"># Enable Authorization header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# CORS headers for API (if needed)
<IfModule mod_headers.c>
    Header always set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
    Header always set Access-Control-Allow-Headers "Authorization, Content-Type, X-WP-Nonce"
</IfModule></div>
                </div>
            </div>

            <!-- Testing Installation -->
            <div class="endpoint-section">
                <h2><?php esc_html_e( 'Testing Your Installation', 'care-api' ); ?></h2>
                
                <h3><?php esc_html_e( 'Quick API Test', 'care-api' ); ?></h3>
                <p><?php esc_html_e( 'Test basic API connectivity using these endpoints:', 'care-api' ); ?></p>

                <h4>1. <?php esc_html_e( 'API Status Check', 'care-api' ); ?></h4>
                <div class="code-example">
                    <div class="code-example-header">
                        <span class="code-language">cURL</span>
                        <button class="copy-button" title="<?php esc_attr_e( 'Copy to clipboard', 'care-api' ); ?>">
                            <i class="dashicons dashicons-clipboard"></i>
                        </button>
                    </div>
                    <div class="code-content">curl -X GET <?php echo esc_url( rest_url( 'care/v1/status' ) ); ?></div>
                </div>
                <p><strong><?php esc_html_e( 'Expected Response:', 'care-api' ); ?></strong> <?php esc_html_e( 'JSON with status: "active" and version information', 'care-api' ); ?></p>

                <h4>2. <?php esc_html_e( 'Authentication Test', 'care-api' ); ?></h4>
                <div class="code-example">
                    <div class="code-example-header">
                        <span class="code-language">cURL</span>
                        <button class="copy-button" title="<?php esc_attr_e( 'Copy to clipboard', 'care-api' ); ?>">
                            <i class="dashicons dashicons-clipboard"></i>
                        </button>
                    </div>
                    <div class="code-content">curl -X POST <?php echo esc_url( rest_url( 'care/v1/auth/login' ) ); ?> \
  -H "Content-Type: application/json" \
  -d '{"username":"your_username","password":"your_password"}'</div>
                </div>
                <p><strong><?php esc_html_e( 'Expected Response:', 'care-api' ); ?></strong> <?php esc_html_e( 'JSON with success: true and a JWT token', 'care-api' ); ?></p>

                <h4>3. <?php esc_html_e( 'Authenticated Request Test', 'care-api' ); ?></h4>
                <div class="code-example">
                    <div class="code-example-header">
                        <span class="code-language">cURL</span>
                        <button class="copy-button" title="<?php esc_attr_e( 'Copy to clipboard', 'care-api' ); ?>">
                            <i class="dashicons dashicons-clipboard"></i>
                        </button>
                    </div>
                    <div class="code-content">curl -X GET <?php echo esc_url( rest_url( 'care/v1/clinics' ) ); ?> \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"</div>
                </div>
                <p><strong><?php esc_html_e( 'Expected Response:', 'care-api' ); ?></strong> <?php esc_html_e( 'JSON with clinic data or empty array if no clinics exist', 'care-api' ); ?></p>
            </div>

            <!-- Troubleshooting -->
            <div class="endpoint-section">
                <h2><?php esc_html_e( 'Troubleshooting', 'care-api' ); ?></h2>
                
                <h3><?php esc_html_e( 'Common Issues and Solutions', 'care-api' ); ?></h3>

                <div class="notice notice-error">
                    <h4><?php esc_html_e( 'Issue: API endpoints return 404 Not Found', 'care-api' ); ?></h4>
                    <p><strong><?php esc_html_e( 'Solutions:', 'care-api' ); ?></strong></p>
                    <ul>
                        <li><?php esc_html_e( 'Go to Settings > Permalinks and click "Save Changes"', 'care-api' ); ?></li>
                        <li><?php esc_html_e( 'Ensure mod_rewrite is enabled on your server', 'care-api' ); ?></li>
                        <li><?php esc_html_e( 'Check that pretty permalinks are not set to "Plain"', 'care-api' ); ?></li>
                    </ul>
                </div>

                <div class="notice notice-error">
                    <h4><?php esc_html_e( 'Issue: Authorization header not being passed', 'care-api' ); ?></h4>
                    <p><strong><?php esc_html_e( 'Solutions:', 'care-api' ); ?></strong></p>
                    <ul>
                        <li><?php esc_html_e( 'Add authorization header rewrite rules to .htaccess', 'care-api' ); ?></li>
                        <li><?php esc_html_e( 'Configure your web server to pass Authorization headers', 'care-api' ); ?></li>
                        <li><?php esc_html_e( 'Use X-WP-Nonce header as alternative authentication method', 'care-api' ); ?></li>
                    </ul>
                </div>

                <div class="notice notice-error">
                    <h4><?php esc_html_e( 'Issue: KiviCare database tables not found', 'care-api' ); ?></h4>
                    <p><strong><?php esc_html_e( 'Solutions:', 'care-api' ); ?></strong></p>
                    <ul>
                        <li><?php esc_html_e( 'Ensure KiviCare base plugin is installed and activated', 'care-api' ); ?></li>
                        <li><?php esc_html_e( 'Complete KiviCare setup wizard to create database tables', 'care-api' ); ?></li>
                        <li><?php esc_html_e( 'Check database prefix settings in wp-config.php', 'care-api' ); ?></li>
                    </ul>
                </div>

                <h3><?php esc_html_e( 'Debug Mode', 'care-api' ); ?></h3>
                <p><?php esc_html_e( 'Enable debug mode for detailed error reporting:', 'care-api' ); ?></p>
                
                <div class="code-example">
                    <div class="code-example-header">
                        <span class="code-language">PHP</span>
                        <button class="copy-button" title="<?php esc_attr_e( 'Copy to clipboard', 'care-api' ); ?>">
                            <i class="dashicons dashicons-clipboard"></i>
                        </button>
                    </div>
                    <div class="code-content">// Add to wp-config.php
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'CARE_API_DEBUG', true );</div>
                </div>

                <p><?php esc_html_e( 'Debug logs will be written to /wp-content/debug.log', 'care-api' ); ?></p>
            </div>

            <!-- Next Steps -->
            <div class="endpoint-section">
                <h2><?php esc_html_e( 'Next Steps', 'care-api' ); ?></h2>
                
                <div class="notice notice-success">
                    <p><?php esc_html_e( 'Congratulations! Your Care API installation is complete. Here\'s what to do next:', 'care-api' ); ?></p>
                    <ul>
                        <li><?php esc_html_e( 'Explore the API documentation to understand available endpoints', 'care-api' ); ?></li>
                        <li><?php esc_html_e( 'Use the built-in API tester to experiment with different endpoints', 'care-api' ); ?></li>
                        <li><?php esc_html_e( 'Create your first integration using the code examples', 'care-api' ); ?></li>
                        <li><?php esc_html_e( 'Configure user roles and permissions for your team', 'care-api' ); ?></li>
                        <li><?php esc_html_e( 'Set up monitoring and logging for production use', 'care-api' ); ?></li>
                    </ul>
                </div>

                <h3><?php esc_html_e( 'Useful Resources', 'care-api' ); ?></h3>
                <ul>
                    <li><a href="<?php echo admin_url( 'admin.php?page=care-api-docs' ); ?>"><?php esc_html_e( 'API Documentation', 'care-api' ); ?></a></li>
                    <li><a href="<?php echo admin_url( 'admin.php?page=care-api-tester' ); ?>"><?php esc_html_e( 'API Tester Tool', 'care-api' ); ?></a></li>
                    <li><a href="<?php echo admin_url( 'admin.php?page=care-api-settings' ); ?>"><?php esc_html_e( 'API Settings', 'care-api' ); ?></a></li>
                    <li><a href="https://descomplicar.pt" target="_blank"><?php esc_html_e( 'Support & Documentation', 'care-api' ); ?></a></li>
                </ul>
            </div>

        </div>
    </div>
</div>