<?php
/**
 * Dashboard Controller Class
 *
 * @package Easy_Invoice
 * @subpackage Controllers
 */

namespace EasyInvoice\Controllers;

use EasyInvoice\Constants\PagesSlugs;
use EasyInvoice\Providers\InvoiceServiceProvider;
use EasyInvoice\Providers\ClientServiceProvider;

/**
 * DashboardController handles dashboard functionality
 */
class DashboardController extends BaseController {

    /**
     * Initialize the controller
     */
    public function init() {
        // Add necessary initialization here
    }
    
    /**
     * Display method implementation
     * 
     * @param array $args Display arguments
     */
    public function display(array $args = []) {
        $page = isset($args['page']) ? $args['page'] : '';
        
        switch ($page) {
            case PagesSlugs::DASHBOARD:
                $this->displayDashboardPage();
                break;
                
            default:
                $this->displayDashboardPage();
                break;
        }
    }
    
    /**
     * Display dashboard page
     */
    protected function displayDashboardPage() {
        // Check user capability
        $error = $this->checkCapability();
        if (is_wp_error($error)) {
            wp_die($error);
        }
        
        // Get data for dashboard
        $invoice_repository = InvoiceServiceProvider::getInvoiceRepository();
        $client_repository = ClientServiceProvider::getClientRepository();
        
        // Get counts
        $total_invoices = count($invoice_repository->all());
        $paid_invoices = count($invoice_repository->findByStatus('paid'));
        $unpaid_invoices = count($invoice_repository->findByStatus('unpaid'));
        $overdue_invoices = count($invoice_repository->findByStatus('overdue'));
        
        $total_clients = count($client_repository->all());
        $active_clients = $this->getActiveClientCount($client_repository, $invoice_repository);
        
        // Get recent invoices
        $recent_invoices = $this->getRecentInvoices($invoice_repository);
        
        // Get revenue data
        $total_revenue = $this->getTotalRevenue($invoice_repository);
        $monthly_revenue = $this->getMonthlyRevenue($invoice_repository);
        
        // Display the template
        $this->displayTemplate(
            EASY_INVOICE_PLUGIN_DIR . 'templates/dashboard-page.php',
            [
                'total_invoices' => $total_invoices,
                'paid_invoices' => $paid_invoices,
                'unpaid_invoices' => $unpaid_invoices,
                'overdue_invoices' => $overdue_invoices,
                'total_clients' => $total_clients,
                'active_clients' => $active_clients,
                'recent_invoices' => $recent_invoices,
                'total_revenue' => $total_revenue,
                'monthly_revenue' => $monthly_revenue
            ]
        );
    }
    
    /**
     * Get active client count
     *
     * @param object $client_repository
     * @param object $invoice_repository
     * @return int Count of active clients
     */
    private function getActiveClientCount($client_repository, $invoice_repository) {
        $clients = $client_repository->all();
        $active_count = 0;
        
        foreach ($clients as $client) {
            try {
                $client_id = $client->getId();
                if (!$client_id) {
                    continue;
                }
                
                $client_invoices = $invoice_repository->findByCustomer($client_id);
                
                // Consider a client active if they have an invoice in the last 90 days
                $has_recent_invoice = false;
                $ninety_days_ago = strtotime('-90 days');
                
                foreach ($client_invoices as $invoice) {
                    $invoice_date = strtotime($invoice->getIssueDate());
                    if ($invoice_date && $invoice_date >= $ninety_days_ago) {
                        $has_recent_invoice = true;
                        break;
                    }
                }
                
                if ($has_recent_invoice) {
                    $active_count++;
                }
            } catch (\Exception $e) {
                // Log the error and continue with the next client
                continue;
            }
        }
        
        return $active_count;
    }
    
    /**
     * Get recent invoices
     *
     * @param object $invoice_repository
     * @return array Recent invoices
     */
    private function getRecentInvoices($invoice_repository) {
        try {
            $invoices = $invoice_repository->all();
            
            // Sort invoices by date (newest first)
            usort($invoices, function($a, $b) {
                $date_a = $a->getIssueDate() ? strtotime($a->getIssueDate()) : 0;
                $date_b = $b->getIssueDate() ? strtotime($b->getIssueDate()) : 0;
                return $date_b - $date_a;
            });
            
            // Return the 5 most recent invoices
            return array_slice($invoices, 0, 5);
        } catch (\Exception $e) {
            // Log the error and return an empty array
            return [];
        }
    }
    
    /**
     * Get total revenue from paid invoices
     *
     * @param object $invoice_repository
     * @return array Total revenue by currency
     */
    private function getTotalRevenue($invoice_repository) {
        try {
            // Get all completed payments instead of using invoice data
            $payments = get_posts([
                'post_type' => 'easy_invoice_payment',
                'post_status' => 'publish',
                'meta_query' => [
                    [
                        'key' => '_status',
                        'value' => ['completed', 'approved', 'paid'],
                        'compare' => 'IN'
                    ]
                ],
                'numberposts' => -1
            ]);
            
            $revenue_by_currency = [];
            $global_currency = get_option('easy_invoice_currency_code', 'USD');
            
            // Calculate revenue from actual payments
            foreach ($payments as $payment) {
                try {
                    $payment_amount = get_post_meta($payment->ID, '_amount', true);
                    if (!is_numeric($payment_amount) || $payment_amount <= 0) {
                        continue;
                    }
                    
                    // Get currency from payment
                    $currency_code = get_post_meta($payment->ID, '_currency', true);
                    if (empty($currency_code) || $currency_code === 'global') {
                        $currency_code = $global_currency;
                    }
                    $currency_code = strtoupper($currency_code);
                    
                    $currency_symbol = \EasyInvoice\Helpers\CurrencyHelper::getCurrencySymbol($currency_code);
                    
                    // Initialize currency if not exists
                    if (!isset($revenue_by_currency[$currency_code])) {
                        $revenue_by_currency[$currency_code] = [
                            'amount' => 0,
                            'symbol' => $currency_symbol
                        ];
                    }
                    
                    $revenue_by_currency[$currency_code]['amount'] += $payment_amount;
                } catch (\Exception $e) {
                    // Log the error and continue with the next payment
                    continue;
                }
            }
            
            return $revenue_by_currency;
        } catch (\Exception $e) {
            // Log the error and return empty array
            return [];
        }
    }
    
    /**
     * Get monthly revenue data for charts
     *
     * @param object $invoice_repository
     * @return array Monthly revenue data
     */
    private function getMonthlyRevenue($invoice_repository) {
        // Initialize months for the last 12 months (rolling period)
        $monthly_revenue = array();
        
        // Get the current date and go back 11 months to create a 12-month period
        $current_date = new \DateTime();
        $start_date = clone $current_date;
        $start_date->modify('-11 months');
        
        // Initialize all 12 months
        for ($i = 0; $i < 12; $i++) {
            $month_date = clone $start_date;
            $month_date->modify("+{$i} months");
            $month_name = $month_date->format('M Y');
            $monthly_revenue[$month_name] = [];
        }
        
        try {
            // Get all completed payments (including different statuses that might be considered completed)
            $payments = get_posts([
                'post_type' => 'easy_invoice_payment',
                'post_status' => 'publish',
                'meta_query' => [
                    [
                        'key' => '_status',
                        'value' => ['completed', 'approved', 'paid'],
                        'compare' => 'IN'
                    ]
                ],
                'numberposts' => -1
            ]);
            
            // If no completed payments found, try to get any payments with amounts
            if (empty($payments)) {
                $payments = get_posts([
                    'post_type' => 'easy_invoice_payment',
                    'post_status' => 'publish',
                    'meta_query' => [
                        [
                            'key' => '_amount',
                            'value' => '0',
                            'compare' => '>'
                        ]
                    ],
                    'numberposts' => -1
                ]);
            }
            
            $global_currency = get_option('easy_invoice_currency_code', 'USD');
            
            // Calculate revenue for each month by currency
            foreach ($payments as $payment) {
                try {
                    $payment_date = get_post_meta($payment->ID, '_payment_date', true);
                    if (!$payment_date) {
                        continue;
                    }
                    
                    $payment_date_obj = new \DateTime($payment_date);
                    if ($payment_date_obj >= $start_date && $payment_date_obj <= $current_date) {
                        $month = $payment_date_obj->format('M Y');
                        $payment_amount = get_post_meta($payment->ID, '_amount', true);
                        
                        if (!is_numeric($payment_amount)) {
                            continue;
                        }
                        
                        // Get currency information from payment
                        $currency_code = get_post_meta($payment->ID, '_currency', true);
                        if (empty($currency_code) || $currency_code === 'global') {
                            $currency_code = $global_currency;
                        }
                        $currency_code = strtoupper($currency_code);
                        
                        $currency_symbol = \EasyInvoice\Helpers\CurrencyHelper::getCurrencySymbol($currency_code);
                            
                        // Initialize currency for this month if not exists
                        if (!isset($monthly_revenue[$month][$currency_code])) {
                            $monthly_revenue[$month][$currency_code] = [
                                'amount' => 0,
                                'symbol' => $currency_symbol
                            ];
                            }
                        
                        $monthly_revenue[$month][$currency_code]['amount'] += $payment_amount;
                    }
                } catch (\Exception $e) {
                    // Log the error and continue with the next payment
                    continue;
                }
            }
            return $monthly_revenue;
        } catch (\Exception $e) {
            // Log the error and return empty monthly revenue
            return $monthly_revenue;
        }
    }
} 