-- ═══════════════════════════════════════════════════════════
-- Dima Shop - نصب دستی جداول
-- نسخه: 3.2.0
-- ═══════════════════════════════════════════════════════════
-- نحوه استفاده:
-- 1. این فایل را در phpMyAdmin import کنید
-- 2. یا از منوی Dima Shop > ابزار دیتابیس استفاده کنید
-- ═══════════════════════════════════════════════════════════

-- جدول سفارشات
CREATE TABLE IF NOT EXISTS `wp_dima_orders` (
    `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    `user_id` bigint(20) UNSIGNED DEFAULT 0,
    `session_id` varchar(64) DEFAULT NULL,
    `total_amount` decimal(12,2) NOT NULL DEFAULT 0.00,
    `discount_amount` decimal(12,2) NOT NULL DEFAULT 0.00,
    `shipping_amount` decimal(12,2) NOT NULL DEFAULT 0.00,
    `tax_amount` decimal(12,2) NOT NULL DEFAULT 0.00,
    `final_amount` decimal(12,2) NOT NULL DEFAULT 0.00,
    `reward_points_earned` int(11) DEFAULT 0,
    `reward_points_used` int(11) DEFAULT 0,
    `status` varchar(30) NOT NULL DEFAULT 'pending',
    `payment_method` varchar(50) NOT NULL,
    `transaction_id` varchar(255) DEFAULT NULL,
    `ref_id` varchar(100) DEFAULT NULL,
    `coupon_code` varchar(50) DEFAULT NULL,
    `customer_data` longtext NOT NULL,
    `ip_address` varchar(45) DEFAULT NULL,
    `user_agent` text DEFAULT NULL,
    `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
    `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `user_id` (`user_id`),
    KEY `status` (`status`),
    KEY `created_at` (`created_at`),
    KEY `transaction_id` (`transaction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- جدول آیتم‌های سفارش
CREATE TABLE IF NOT EXISTS `wp_dima_order_items` (
    `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    `order_id` bigint(20) UNSIGNED NOT NULL,
    `product_id` bigint(20) UNSIGNED NOT NULL,
    `variation_id` bigint(20) UNSIGNED DEFAULT 0,
    `quantity` int(11) UNSIGNED NOT NULL DEFAULT 1,
    `price` decimal(12,2) NOT NULL DEFAULT 0.00,
    PRIMARY KEY (`id`),
    KEY `order_id` (`order_id`),
    KEY `product_id` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- جدول کوپن‌ها
CREATE TABLE IF NOT EXISTS `wp_dima_coupons` (
    `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    `code` varchar(50) NOT NULL,
    `type` varchar(20) NOT NULL DEFAULT 'percent',
    `value` decimal(10,2) NOT NULL,
    `min_amount` decimal(12,2) DEFAULT 0.00,
    `max_uses` int(11) UNSIGNED DEFAULT 0,
    `used_count` int(11) UNSIGNED DEFAULT 0,
    `expiry_date` datetime DEFAULT NULL,
    `status` varchar(20) NOT NULL DEFAULT 'active',
    `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `code` (`code`),
    KEY `status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- جدول نظرات
CREATE TABLE IF NOT EXISTS `wp_dima_reviews` (
    `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    `product_id` bigint(20) UNSIGNED NOT NULL,
    `user_id` bigint(20) UNSIGNED DEFAULT 0,
    `author_name` varchar(100) NOT NULL,
    `author_email` varchar(100) DEFAULT NULL,
    `rating` tinyint(1) UNSIGNED NOT NULL DEFAULT 5,
    `title` varchar(200) DEFAULT NULL,
    `content` text,
    `status` varchar(20) NOT NULL DEFAULT 'pending',
    `verified_purchase` tinyint(1) DEFAULT 0,
    `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `product_id` (`product_id`),
    KEY `user_id` (`user_id`),
    KEY `status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- جدول شگفت‌انگیز
CREATE TABLE IF NOT EXISTS `wp_dima_flash_sales` (
    `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    `product_id` bigint(20) UNSIGNED NOT NULL,
    `sale_price` decimal(12,2) NOT NULL,
    `start_time` datetime NOT NULL,
    `end_time` datetime NOT NULL,
    `stock_limit` int(11) UNSIGNED DEFAULT 0,
    `sold_count` int(11) UNSIGNED DEFAULT 0,
    `status` varchar(20) NOT NULL DEFAULT 'active',
    `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `product_id` (`product_id`),
    KEY `status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- جدول امتیازات
CREATE TABLE IF NOT EXISTS `wp_dima_reward_points` (
    `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    `user_id` bigint(20) UNSIGNED NOT NULL,
    `points` int(11) NOT NULL,
    `type` varchar(30) NOT NULL,
    `reference_id` bigint(20) UNSIGNED DEFAULT NULL,
    `description` varchar(255) DEFAULT NULL,
    `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `user_id` (`user_id`),
    KEY `type` (`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- جدول لاگ‌های امنیتی
CREATE TABLE IF NOT EXISTS `wp_dima_security_logs` (
    `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    `event_type` varchar(100) NOT NULL,
    `severity` varchar(20) NOT NULL DEFAULT 'warning',
    `ip_address` varchar(45) NOT NULL,
    `user_id` bigint(20) UNSIGNED DEFAULT 0,
    `user_agent` text DEFAULT NULL,
    `details` longtext,
    `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `event_type` (`event_type`),
    KEY `ip_address` (`ip_address`),
    KEY `severity` (`severity`),
    KEY `created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;