<?php
/**
 * کلاس گزارشات و آمار فروش
 */
if (!defined('ABSPATH')) exit;

class Dima_Reports {
    
    /**
     * آمار داشبورد
     */
    public function get_dashboard_stats() {
        global $wpdb;
        $table = $wpdb->prefix . 'dima_orders';
        
        $today = date('Y-m-d');
        $month_start = date('Y-m-01');
        
        $today_sales = $wpdb->get_var($wpdb->prepare(
            "SELECT SUM(final_amount) FROM $table WHERE DATE(created_at) = %s AND status = 'completed'",
            $today
        ));
        
        $today_orders = $wpdb->get_var($wpdb->prepare(
            "SELECT COUNT(*) FROM $table WHERE DATE(created_at) = %s",
            $today
        ));
        
        $month_sales = $wpdb->get_var($wpdb->prepare(
            "SELECT SUM(final_amount) FROM $table WHERE created_at >= %s AND status = 'completed'",
            $month_start
        ));
        
        $total_products = wp_count_posts('dima_product');
        $total_products = $total_products->publish;
        
        return array(
            'today_sales' => $today_sales ?: 0,
            'today_orders' => $today_orders ?: 0,
            'month_sales' => $month_sales ?: 0,
            'total_products' => $total_products ?: 0
        );
    }
    
    /**
     * گزارش فروش
     */
    public function get_sales_report($period = 'month') {
        global $wpdb;
        $table = $wpdb->prefix . 'dima_orders';
        
        $where = "WHERE status = 'completed'";
        
        switch ($period) {
            case 'today':
                $where .= " AND DATE(created_at) = '" . date('Y-m-d') . "'";
                break;
            case 'week':
                $week_start = date('Y-m-d', strtotime('-7 days'));
                $where .= " AND created_at >= '$week_start'";
                break;
            case 'month':
                $month_start = date('Y-m-01');
                $where .= " AND created_at >= '$month_start'";
                break;
            case 'year':
                $year_start = date('Y-01-01');
                $where .= " AND created_at >= '$year_start'";
                break;
        }
        
        $total_sales = $wpdb->get_var("SELECT SUM(final_amount) FROM $table $where");
        $total_orders = $wpdb->get_var("SELECT COUNT(*) FROM $table $where");
        $total_discounts = $wpdb->get_var("SELECT SUM(discount_amount) FROM $table $where");
        
        $average_order = $total_orders > 0 ? $total_sales / $total_orders : 0;
        
        return array(
            'total_sales' => $total_sales ?: 0,
            'total_orders' => $total_orders ?: 0,
            'average_order' => $average_order,
            'total_discounts' => $total_discounts ?: 0
        );
    }
    
    /**
     * محصولات پرفروش
     */
    public function get_top_products($limit = 10, $period = 'month') {
        global $wpdb;
        $items_table = $wpdb->prefix . 'dima_order_items';
        $orders_table = $wpdb->prefix . 'dima_orders';
        
        $where = "WHERE o.status = 'completed'";
        
        switch ($period) {
            case 'today':
                $where .= " AND DATE(o.created_at) = '" . date('Y-m-d') . "'";
                break;
            case 'week':
                $week_start = date('Y-m-d', strtotime('-7 days'));
                $where .= " AND o.created_at >= '$week_start'";
                break;
            case 'month':
                $month_start = date('Y-m-01');
                $where .= " AND o.created_at >= '$month_start'";
                break;
            case 'year':
                $year_start = date('Y-01-01');
                $where .= " AND o.created_at >= '$year_start'";
                break;
        }
        
        $results = $wpdb->get_results("
            SELECT 
                i.product_id,
                SUM(i.quantity) as total_quantity,
                SUM(i.price * i.quantity) as total_revenue
            FROM $items_table i
            INNER JOIN $orders_table o ON i.order_id = o.id
            $where
            GROUP BY i.product_id
            ORDER BY total_quantity DESC
            LIMIT $limit
        ");
        
        return $results;
    }
    
    /**
     * گزارش نموداری فروش (برای Chart.js)
     */
    public function get_chart_data($days = 30) {
        global $wpdb;
        $table = $wpdb->prefix . 'dima_orders';
        
        $data = array();
        $labels = array();
        
        for ($i = $days - 1; $i >= 0; $i--) {
            $date = date('Y-m-d', strtotime("-$i days"));
            $labels[] = date_i18n('j F', strtotime($date));
            
            $sales = $wpdb->get_var($wpdb->prepare(
                "SELECT SUM(final_amount) FROM $table WHERE DATE(created_at) = %s AND status = 'completed'",
                $date
            ));
            
            $data[] = floatval($sales);
        }
        
        return array(
            'labels' => $labels,
            'data' => $data
        );
    }
}