<?php
namespace ComptaFlowUK\Admin\Screens;

defined('ABSPATH') || exit;

class ExportScreen
{
    public static function render(): void
    {
        if (!current_user_can('manage_options')) {
            wp_die(esc_html__('Insufficient permissions.', 'comptaflow-uk-by-meetempo'));
        }
        
        echo '<div class="wrap">';
        echo '<h1>' . esc_html__('Accounting Exports', 'comptaflow-uk-by-meetempo') . '</h1>';
        echo '<p>' . esc_html__('Download your accounting documents for the desired period.', 'comptaflow-uk-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-uk-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-uk-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-uk-by-meetempo') . '</h3>';
        
        $exports = [
            'audit_trail' => ['label' => __('Audit Trail', 'comptaflow-uk-by-meetempo'), 'desc' => __('Full journal entries export (HMRC compliant)', 'comptaflow-uk-by-meetempo')],
            'balance' => ['label' => __('Trial Balance', 'comptaflow-uk-by-meetempo'), 'desc' => __('Trial Balance report', 'comptaflow-uk-by-meetempo')],
            'journal' => ['label' => __('Journal', 'comptaflow-uk-by-meetempo'), 'desc' => __('Chronological journal', 'comptaflow-uk-by-meetempo')],
            'nominal_ledger' => ['label' => __('Nominal Ledger', 'comptaflow-uk-by-meetempo'), 'desc' => __('Detail by account', 'comptaflow-uk-by-meetempo')],
            'profit_loss' => ['label' => __('Profit & Loss', 'comptaflow-uk-by-meetempo'), 'desc' => __('Income and expenses summary', 'comptaflow-uk-by-meetempo')],
        ];
        
        foreach ($exports as $type => $info) {
            $nonce = wp_create_nonce('comptaflow_uk_export_' . $type);
            $base_url = admin_url('admin-post.php?action=comptaflow_uk_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-uk-by-meetempo') . '</a>';
            echo '</div>';
        }
        
        echo '</div>';
        
        // Compliance 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-uk-by-meetempo') . '</h3>';
        echo '<ul style="margin-bottom: 0;">';
        echo '<li><strong>' . esc_html__('Audit Trail', 'comptaflow-uk-by-meetempo') . '</strong>: ' . esc_html__('Required for HMRC inspection', 'comptaflow-uk-by-meetempo') . '</li>';
        echo '<li><strong>' . esc_html__('Trial Balance + Journal', 'comptaflow-uk-by-meetempo') . '</strong>: ' . esc_html__('Recommended for accountant review', 'comptaflow-uk-by-meetempo') . '</li>';
        echo '<li><strong>' . esc_html__('Retention', 'comptaflow-uk-by-meetempo') . '</strong>: ' . esc_html__('6 years minimum (HMRC requirement)', 'comptaflow-uk-by-meetempo') . '</li>';
        echo '</ul>';
        echo '</div>';
        
        // Script for building URL with dates
        $alert_msg = esc_js(__('Please select a period.', 'comptaflow-uk-by-meetempo'));
        $inline_js = "document.addEventListener('DOMContentLoaded', function() {"
            . "document.querySelectorAll('.comptaflow-export-btn').forEach(function(btn) {"
            . "btn.addEventListener('click', function(e) {"
            . "e.preventDefault();"
            . "var startDate = document.getElementById('export_start_date').value;"
            . "var endDate = document.getElementById('export_end_date').value;"
            . "if (!startDate || !endDate) { alert('" . $alert_msg . "'); return; }"
            . "var url = this.dataset.url"
            . " + '&start=' + encodeURIComponent(startDate)"
            . " + '&end=' + encodeURIComponent(endDate)"
            . " + '&_wpnonce=' + encodeURIComponent(this.dataset.nonce);"
            . "window.location.href = url;"
            . "});"
            . "});"
            . "});";
        wp_add_inline_script('comptaflow-uk-admin', $inline_js);
    }
}
