<?php

namespace EasyInvoice\Admin;

class AdminController {
    public function registerPages(): void {
        // Check if we need to display a specific page
        $page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : '';
        $action = isset($_GET['action']) ? sanitize_text_field($_GET['action']) : '';
        
        if ($page === 'easy-invoice-invoices' && $action === 'verify-payment') {
            $this->loadTemplate('manual-payment-verification');
            return;
        }
        
       
    }
    
    /**
     * Load a template file
     * 
     * @param string $template_name Template name without .php extension
     * @return void
     */
    private function loadTemplate($template_name) {
        $template_path = EASY_INVOICE_PLUGIN_DIR . 'templates/admin/' . $template_name . '.php';
        if (file_exists($template_path)) {
            include $template_path;
        }
    }

    /**
     * Send admin notification for manual payment
     * 
     * @param int $invoice_id
     * @param string $payment_method
     * @return void
     */
    public function sendManualPaymentNotification($invoice_id, $payment_method) {
        // Get invoice details
        $invoice = new \EasyInvoice\Models\Invoice(get_post($invoice_id));
        
        if (!$invoice || !$invoice->getId()) {
            return;
        }
        
        // Use EmailManager to send admin notification
        $email_manager = \EasyInvoice\Services\EmailManager::getInstance();
        $email_manager->sendAdminPaymentNotification($invoice, [
            'payment_method' => $payment_method
        ]);
    }

    /**
     * Initialize the controller
     */
    public function init() {
        // Add menu pages
        add_action('admin_menu', [$this, 'addMenuPages']);
        
        // Add settings
        add_action('admin_init', [$this, 'registerSettings']);
        
        // Add custom columns
        add_filter('manage_easy-invoice_posts_columns', [$this, 'addCustomColumns']);
        add_action('manage_easy-invoice_posts_custom_column', [$this, 'renderCustomColumns'], 10, 2);
        
        // Add payment status filter
        add_action('restrict_manage_posts', [$this, 'addPaymentStatusFilter']);
        add_filter('parse_query', [$this, 'filterByPaymentStatus']);
        
        // Add manual payment notification
        add_action('easy_invoice_manual_payment_submitted', [$this, 'sendManualPaymentNotification'], 10, 2);
    }

    public function registerSettings() {
        // Payment settings
        register_setting('easy_invoice_payment_settings', 'easy_invoice_payment_methods', [
            'type' => 'array',
            'default' => ['paypal'],
            'sanitize_callback' => function($value) {
                return is_array($value) ? array_map('sanitize_text_field', $value) : [];
            }
        ]);
        
        register_setting('easy_invoice_payment_settings', 'easy_invoice_paypal_email', [
            'type' => 'string',
            'sanitize_callback' => 'sanitize_email',
        ]);
        
        register_setting('easy_invoice_payment_settings', 'easy_invoice_payment_reminder_days', [
            'type' => 'integer',
            'default' => 3,
            'sanitize_callback' => 'absint',
        ]);
        
        // Additional hook to register Pro gateway settings
        do_action('easy_invoice_register_payment_gateway_settings');
    }
} 