<?php
namespace ComptaFlowPro\Admin\Screens;

defined('ABSPATH') || exit;

class ExportScreen
{
    public static function render(): void
    {
        if (!current_user_can('manage_options')) {
            wp_die(esc_html__('You do not have sufficient permissions.', 'comptaflow-by-meetempo'));
        }
        
        echo '<div class="wrap">';
        echo '<h1>' . esc_html__('Accounting Exports', 'comptaflow-by-meetempo') . '</h1>';
        echo '<p>' . esc_html__('Download your accounting documents for the selected period.', 'comptaflow-by-meetempo') . '</p>';
        
        self::renderExportForm();
        
        echo '</div>';
    }
    
    private static function renderExportForm(): void
    {
        $current_year = gmdate('Y');
        $start_date = $current_year . '-01-01';
        $end_date = $current_year . '-12-31';
        
        echo '<div style="background: white; padding: 20px; border: 1px solid #ddd; max-width: 600px;">';
        
        echo '<table class="form-table">';
        echo '<tr>';
        echo '<th scope="row"><label>' . esc_html__('Period', 'comptaflow-by-meetempo') . '</label></th>';
        echo '<td>';
        echo '<input type="date" id="export_start_date" value="' . esc_attr($start_date) . '"> ';
        echo '<strong>' . esc_html__('to', 'comptaflow-by-meetempo') . '</strong> ';
        echo '<input type="date" id="export_end_date" value="' . esc_attr($end_date) . '">';
        echo '</td>';
        echo '</tr>';
        echo '</table>';
        
        echo '<h3>' . esc_html__('Choose your export', 'comptaflow-by-meetempo') . '</h3>';
        
        // LITE exports only (no FEC)
        $exports = [
            'balance' => [
                'label' => __('Balance', 'comptaflow-by-meetempo'), 
                'desc' => __('General balance sheet', 'comptaflow-by-meetempo')
            ],
            'journal' => [
                'label' => __('Journal', 'comptaflow-by-meetempo'), 
                'desc' => __('Chronological journal', 'comptaflow-by-meetempo')
            ],
            'grandlivre' => [
                'label' => __('General Ledger', 'comptaflow-by-meetempo'), 
                'desc' => __('Detail by account', 'comptaflow-by-meetempo')
            ],
            'resultat' => [
                'label' => __('Income Statement', 'comptaflow-by-meetempo'), 
                'desc' => __('Revenue/expenses summary', 'comptaflow-by-meetempo')
            ],
        ];
        
        foreach ($exports as $type => $info) {
            $nonce = wp_create_nonce('comptaflow_export_' . $type);
            $base_url = admin_url('admin-post.php?action=comptaflow_export_' . $type);
            
            echo '<div style="margin: 15px 0; padding: 15px; border: 1px solid #ddd; border-radius: 5px; display: flex; justify-content: space-between; align-items: center;">';
            echo '<div>';
            echo '<strong>' . esc_html($info['label']) . '</strong><br>';
            echo '<small style="color: #666;">' . esc_html($info['desc']) . '</small>';
            echo '</div>';
            echo '<a href="#" class="button comptaflow-export-btn" data-type="' . esc_attr($type) . '" data-nonce="' . esc_attr($nonce) . '" data-url="' . esc_url($base_url) . '">' . esc_html__('Download', 'comptaflow-by-meetempo') . '</a>';
            echo '</div>';
        }
        
        echo '</div>';
        
        // FEC Upsell (PRO feature)
        echo '<div style="background: #f0f7ff; padding: 20px; margin-top: 20px; border-left: 4px solid #667eea; max-width: 600px; border-radius: 4px;">';
        echo '<h3 style="margin-top: 0; color: #667eea;">📤 ' . esc_html__('FEC Export', 'comptaflow-by-meetempo') . '</h3>';
        echo '<p>' . esc_html__('The FEC (Fichier des Écritures Comptables) is mandatory for French tax audits.', 'comptaflow-by-meetempo') . '</p>';
        echo '<p><a href="' . esc_url(admin_url('admin.php?page=comptaflow-plans')) . '" class="button button-primary">' . esc_html__('Available in ComptaFlow Pro', 'comptaflow-by-meetempo') . ' →</a></p>';
        echo '</div>';
        
        // Legal notice
        echo '<div style="background: #f0f0f1; padding: 15px; margin-top: 20px; border-left: 4px solid #0073aa; max-width: 600px;">';
        echo '<h3 style="margin-top: 0;">' . esc_html__('Legal Compliance', 'comptaflow-by-meetempo') . '</h3>';
        echo '<ul style="margin-bottom: 0;">';
        echo '<li><strong>' . esc_html__('FEC', 'comptaflow-by-meetempo') . '</strong> : ' . esc_html__('Mandatory for tax audits (Pro)', 'comptaflow-by-meetempo') . '</li>';
        echo '<li><strong>' . esc_html__('Balance + Journal', 'comptaflow-by-meetempo') . '</strong> : ' . esc_html__('Recommended for accountants', 'comptaflow-by-meetempo') . '</li>';
        echo '<li><strong>' . esc_html__('Retention', 'comptaflow-by-meetempo') . '</strong> : ' . esc_html__('10 years minimum', 'comptaflow-by-meetempo') . '</li>';
        echo '</ul>';
        echo '</div>';
    }
}
