<?php
namespace ComptaFlowPro\Services;

defined('ABSPATH') || exit;

class GrandLivreExporter
{
    /**
     * Generate and download the General Ledger
     * Movement details by account with cumulative balances
     */
    public static function export(int $user_id, string $start_date, string $end_date): void
    {
        global $wpdb;
        $journal_table = $wpdb->prefix . 'cfp_journal_entries';
        
        // Retrieve sorted by account then date
        $entries = $wpdb->get_results($wpdb->prepare(
            "SELECT * FROM $journal_table 
             WHERE created_by = %d 
             AND date_ecriture BETWEEN %s AND %s
             ORDER BY compte_num ASC, date_ecriture ASC, created_at ASC",
            $user_id, $start_date, $end_date
        ));
        
        if (empty($entries)) {
            wp_die(esc_html__('No entries found for this period.', 'comptaflow-by-meetempo'));
        }
        
        // Generate CSV
        $content = self::generateGrandLivreContent($entries);
        
        // Download headers
        $filename = 'GeneralLedger_' . gmdate('Ymd') . '_' . str_replace('-', '', $start_date) . '_' . str_replace('-', '', $end_date) . '.csv';
        
        header('Content-Type: text/csv; charset=UTF-8');
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header('Pragma: no-cache');
        header('Expires: 0');
        
        echo "\xEF\xBB\xBF"; // BOM UTF-8
        echo $content;
        exit;
    }
    
    /**
     * Generate General Ledger CSV content
     */
    private static function generateGrandLivreContent(array $entries): string
    {
        $lines = [];
        
        // Group by account
        $by_account = [];
        foreach ($entries as $entry) {
            $by_account[$entry->compte_num][] = $entry;
        }
        
        // Generate ledger account by account
        foreach ($by_account as $compte_num => $account_entries) {
            $compte_lib = self::getCompteLibelle($compte_num);
            
            // Account header
            $lines[] = '';
            $lines[] = '═════════════════════════════════════════════════════';
            $lines[] = 'ACCOUNT ' . $compte_num . ' - ' . strtoupper($compte_lib);
            $lines[] = '═════════════════════════════════════════════════════';
            $lines[] = 'Date;Ref;Label;Debit;Credit;Balance';
            
            // Account movements
            $solde = 0;
            $total_debit = 0;
            $total_credit = 0;
            
            foreach ($account_entries as $entry) {
                $debit = (float)$entry->debit;
                $credit = (float)$entry->credit;
                
                $solde += $debit - $credit;
                $total_debit += $debit;
                $total_credit += $credit;
                
                $date = date_i18n('d/m/Y', strtotime($entry->date_ecriture));
                
                $lines[] = implode(';', [
                    $date,
                    $entry->piece_ref ?: 'N/A',
                    self::cleanText($entry->libelle ?: ''),
                    number_format($debit, 2, ',', ' '),
                    number_format($credit, 2, ',', ' '),
                    number_format($solde, 2, ',', ' ')
                ]);
            }
            
            // Account total
            $lines[] = '─────────────────────────────────────────────────────';
            $lines[] = implode(';', [
                'TOTAL',
                $compte_num,
                '',
                number_format($total_debit, 2, ',', ' '),
                number_format($total_credit, 2, ',', ' '),
                number_format($solde, 2, ',', ' ')
            ]);
        }
        
        return implode("\n", $lines);
    }
    
    /**
     * Account labels via ChartOfAccounts
     */
    private static function getCompteLibelle(string $compte_num): string
    {
        require_once COMPTAFLOW_PLUGIN_DIR . 'src/Services/ChartOfAccounts.php';
        
        // Exact search
        $account = \ComptaFlowPro\Services\ChartOfAccounts::getAccount($compte_num);
        if ($account) {
            return $account['label'];
        }
        
        // Search by prefix
        for ($len = strlen($compte_num) - 1; $len >= 2; $len--) {
            $prefix = substr($compte_num, 0, $len);
            $account = \ComptaFlowPro\Services\ChartOfAccounts::getAccount($prefix);
            if ($account) {
                return $account['label'];
            }
        }
        
        return 'Account ' . $compte_num;
    }
    
    /**
     * Clean text for CSV
     */
    private static function cleanText(string $text): string
    {
        $text = str_replace([';', "\n", "\r", "\t"], ' ', $text);
        $text = preg_replace('/\s+/', ' ', $text);
        return trim($text);
    }
}
