-- ============================================================
-- جداول نظام محفظة الشريك (المطعم / مكتب الغاز)
-- للاستيراد في phpMyAdmin
-- تاريخ: 2025-01-29
-- ============================================================
-- المحفظة تُعبّأ عبر زين كاش أو ماستر كارد
-- رسوم التطبيق تُستقطع من الرصيد عند كل طلبية مكتملة
-- الرصيد يمكن أن يكون سالباً (ماينص)
-- ============================================================

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
SET NAMES utf8mb4;
SET CHARACTER SET utf8mb4;

-- حذف الجداول إن وُجدت (بالترتيب العكسي بسبب المفاتيح الأجنبية)
DROP TABLE IF EXISTS `partner_wallet_topup_requests`;
DROP TABLE IF EXISTS `partner_wallet_transactions`;
DROP TABLE IF EXISTS `partner_wallet_settings`;
DROP TABLE IF EXISTS `partner_wallets`;

-- --------------------------------------------------------
-- جدول: partner_wallets (محافظ الشركاء - مطعم / مكتب غاز)
-- --------------------------------------------------------
CREATE TABLE `partner_wallets` (
  `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `owner_type` varchar(32) NOT NULL COMMENT 'restaurant | gas_office',
  `owner_id` bigint(20) UNSIGNED NOT NULL,
  `balance` decimal(15,2) NOT NULL DEFAULT 0.00 COMMENT 'الرصيد (يمكن أن يكون سالباً - ماينص)',
  `currency` varchar(10) NOT NULL DEFAULT 'IQD',
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `partner_wallets_owner_type_owner_id_unique` (`owner_type`,`owner_id`),
  KEY `partner_wallets_owner_type_owner_id_index` (`owner_type`,`owner_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='محافظ الشركاء (مطعم / مكتب غاز)';

-- --------------------------------------------------------
-- جدول: partner_wallet_settings (إعدادات المحفظة)
-- --------------------------------------------------------
CREATE TABLE `partner_wallet_settings` (
  `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `partner_wallet_id` bigint(20) UNSIGNED NOT NULL,
  `payment_methods` json DEFAULT NULL COMMENT '["zain_cash", "mastercard"]',
  `min_topup_amount` decimal(15,2) NOT NULL DEFAULT 1000.00,
  `max_topup_amount` decimal(15,2) DEFAULT NULL,
  `allow_negative_balance` tinyint(1) NOT NULL DEFAULT 1 COMMENT 'السماح بالرصيد السالب (ماينص)',
  `zain_cash_merchant_id` text DEFAULT NULL,
  `notes` text DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `partner_wallet_settings_partner_wallet_id_index` (`partner_wallet_id`),
  CONSTRAINT `partner_wallet_settings_partner_wallet_id_foreign` FOREIGN KEY (`partner_wallet_id`) REFERENCES `partner_wallets` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='إعدادات المحفظة';

-- --------------------------------------------------------
-- جدول: partner_wallet_transactions (معاملات المحفظة)
-- --------------------------------------------------------
CREATE TABLE `partner_wallet_transactions` (
  `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `partner_wallet_id` bigint(20) UNSIGNED NOT NULL,
  `type` varchar(32) NOT NULL COMMENT 'topup | deduction',
  `amount` decimal(15,2) NOT NULL COMMENT 'موجب دائماً',
  `balance_before` decimal(15,2) NOT NULL,
  `balance_after` decimal(15,2) NOT NULL,
  `payment_method` varchar(64) DEFAULT NULL COMMENT 'zain_cash | mastercard',
  `reference_type` varchar(64) DEFAULT NULL COMMENT 'food_order | gas_order | topup',
  `reference_id` bigint(20) UNSIGNED DEFAULT NULL,
  `description` varchar(255) DEFAULT NULL,
  `meta` json DEFAULT NULL,
  `status` varchar(32) NOT NULL DEFAULT 'completed' COMMENT 'pending | completed | failed | refunded',
  `external_id` varchar(255) DEFAULT NULL COMMENT 'معرف الدفع الخارجي',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `partner_wallet_transactions_partner_wallet_id_type_index` (`partner_wallet_id`,`type`),
  KEY `partner_wallet_transactions_partner_wallet_id_created_at_index` (`partner_wallet_id`,`created_at`),
  KEY `partner_wallet_transactions_reference_type_reference_id_index` (`reference_type`,`reference_id`),
  KEY `partner_wallet_transactions_status_index` (`status`),
  CONSTRAINT `partner_wallet_transactions_partner_wallet_id_foreign` FOREIGN KEY (`partner_wallet_id`) REFERENCES `partner_wallets` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='معاملات المحفظة (تعبئة / استقطاع)';

-- --------------------------------------------------------
-- جدول: partner_wallet_topup_requests (طلبات شحن المحفظة - تأكيد يدوي من الإدارة)
-- --------------------------------------------------------
CREATE TABLE `partner_wallet_topup_requests` (
  `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `partner_wallet_id` bigint(20) UNSIGNED NOT NULL,
  `owner_type` varchar(32) NOT NULL COMMENT 'restaurant | gas_office',
  `owner_id` bigint(20) UNSIGNED NOT NULL,
  `amount` decimal(15,2) NOT NULL,
  `payment_method` varchar(64) NOT NULL DEFAULT 'zain_cash' COMMENT 'zain_cash | mastercard',
  `transfer_code` varchar(128) DEFAULT NULL COMMENT 'كود التحويل',
  `transfer_from_phone` varchar(32) DEFAULT NULL COMMENT 'رقم الذي حول منه',
  `receipt_image_path` varchar(255) DEFAULT NULL COMMENT 'صورة إيصال التحويل',
  `status` enum('pending','approved','rejected') NOT NULL DEFAULT 'pending',
  `admin_notes` text DEFAULT NULL,
  `reviewed_by` bigint(20) UNSIGNED DEFAULT NULL,
  `reviewed_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `partner_wallet_topup_requests_partner_wallet_id_status_index` (`partner_wallet_id`,`status`),
  KEY `partner_wallet_topup_requests_owner_type_owner_id_index` (`owner_type`,`owner_id`),
  KEY `partner_wallet_topup_requests_status_index` (`status`),
  KEY `partner_wallet_topup_requests_created_at_index` (`created_at`),
  CONSTRAINT `partner_wallet_topup_requests_partner_wallet_id_foreign` FOREIGN KEY (`partner_wallet_id`) REFERENCES `partner_wallets` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='طلبات شحن المحفظة (تأكيد يدوي من الإدارة)';

-- --------------------------------------------------------
-- إعدادات المحفظة في app_settings (اختياري)
-- إذا كان لديك جدول app_settings ولم يسبق إضافة الحقلين،
-- انسخ السطرين التاليين ونفّذهما في تبويب SQL (بدون التعليق):
--   ALTER TABLE app_settings ADD COLUMN wallet_zain_cash_phone VARCHAR(32) NULL;
--   ALTER TABLE app_settings ADD COLUMN wallet_topup_instructions TEXT NULL;
-- إذا ظهر خطأ "العمود موجود" فاتركه ولا تنفّذ السطر مرة ثانية.
-- --------------------------------------------------------

-- ============================================================
-- انتهى الاستيراد
-- ============================================================
