<?php
/**
 * کلاس محصولات شگفت‌انگیز
 */
if (!defined('ABSPATH')) exit;

class Dima_Flash_Sale {
    
    private static $instance = null;
    
    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    /**
     * ایجاد شگفت‌انگیز
     */
    public function create_flash_sale($product_id, $sale_price, $start_time, $end_time, $stock_limit = 0) {
        global $wpdb;
        
        return $wpdb->insert($wpdb->prefix . 'dima_flash_sales', array(
            'product_id' => absint($product_id),
            'sale_price' => floatval($sale_price),
            'start_time' => sanitize_text_field($start_time),
            'end_time' => sanitize_text_field($end_time),
            'stock_limit' => absint($stock_limit),
            'status' => 'active',
            'created_at' => current_time('mysql')
        ));
    }
    
    /**
     * دریافت شگفت‌انگیزهای فعال
     */
    public function get_active_sales($limit = 10) {
        global $wpdb;
        
        return $wpdb->get_results($wpdb->prepare(
            "SELECT * FROM {$wpdb->prefix}dima_flash_sales 
             WHERE status = 'active' 
               AND start_time <= NOW() 
               AND end_time > NOW()
               AND (stock_limit = 0 OR sold_count < stock_limit)
             ORDER BY start_time DESC 
             LIMIT %d",
            $limit
        ));
    }
    
    /**
     * دریافت قیمت شگفت‌انگیز محصول
     */
    public function get_flash_price($product_id) {
        global $wpdb;
        
        return $wpdb->get_row($wpdb->prepare(
            "SELECT * FROM {$wpdb->prefix}dima_flash_sales 
             WHERE product_id = %d 
               AND status = 'active' 
               AND start_time <= NOW() 
               AND end_time > NOW()
               AND (stock_limit = 0 OR sold_count < stock_limit)
             LIMIT 1",
            $product_id
        ));
    }
    
    /**
     * افزایش تعداد فروش
     */
    public function increment_sold($sale_id) {
        global $wpdb;
        
        $wpdb->query($wpdb->prepare(
            "UPDATE {$wpdb->prefix}dima_flash_sales SET sold_count = sold_count + 1 WHERE id = %d",
            $sale_id
        ));
    }
    
    /**
     * محاسبه زمان باقی‌مانده
     */
    public function get_time_remaining($end_time) {
        $end = strtotime($end_time);
        $now = time();
        $diff = max(0, $end - $now);
        
        return array(
            'days' => floor($diff / 86400),
            'hours' => floor(($diff % 86400) / 3600),
            'minutes' => floor(($diff % 3600) / 60),
            'seconds' => $diff % 60,
            'total_seconds' => $diff
        );
    }
    
    /**
     * حذف شگفت‌انگیز
     */
    public function delete_flash_sale($sale_id) {
        global $wpdb;
        
        return $wpdb->delete($wpdb->prefix . 'dima_flash_sales', array('id' => absint($sale_id)));
    }
    
    /**
     * دریافت تمام شگفت‌انگیزها (برای ادمین)
     */
    public function get_all_sales($status = null) {
        global $wpdb;
        
        $where = $status ? $wpdb->prepare("WHERE status = %s", $status) : '';
        
        return $wpdb->get_results("
            SELECT s.*, p.post_title as product_title 
            FROM {$wpdb->prefix}dima_flash_sales s
            LEFT JOIN {$wpdb->posts} p ON s.product_id = p.ID
            $where
            ORDER BY s.created_at DESC
        ");
    }
}