-- Apple Supplier — MySQL import
-- Generated from data.db on 2026-12-05T21:53:12.361Z
SET NAMES utf8mb4;
SET time_zone = '+00:00';
START TRANSACTION;

CREATE TABLE IF NOT EXISTS users (
  id INT NOT NULL AUTO_INCREMENT,
  name VARCHAR(120) NOT NULL,
  email VARCHAR(191) NOT NULL,
  phone VARCHAR(40) NOT NULL DEFAULT '',
  password_hash VARCHAR(255) NOT NULL,
  role ENUM('user', 'admin') NOT NULL DEFAULT 'user',
  balance BIGINT NOT NULL DEFAULT 0,
  reseller_pct INT NOT NULL DEFAULT 0,
  api_key VARCHAR(191) NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_users_email (email),
  UNIQUE KEY uq_users_api_key (api_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS services (
  id INT NOT NULL AUTO_INCREMENT,
  code VARCHAR(60) NOT NULL,
  name_ar VARCHAR(200) NOT NULL,
  name_en VARCHAR(200) NULL,
  category ENUM('checks', 'network_unlock', 'diagnostics') NOT NULL,
  price INT NOT NULL,
  cost INT NOT NULL DEFAULT 0,
  description VARCHAR(1000) NOT NULL DEFAULT '',
  description_en VARCHAR(1000) NULL,
  eta_hours INT NOT NULL DEFAULT 24,
  active TINYINT NOT NULL DEFAULT 1,
  sort INT NOT NULL DEFAULT 0,
  PRIMARY KEY (id),
  UNIQUE KEY uq_services_code (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS orders (
  id INT NOT NULL AUTO_INCREMENT,
  user_id INT NOT NULL,
  service_id INT NOT NULL,
  device_info VARCHAR(100) NOT NULL,
  device_model VARCHAR(60) NOT NULL DEFAULT '',
  status ENUM('pending', 'processing', 'completed', 'rejected') NOT NULL DEFAULT 'pending',
  price INT NOT NULL,
  cost INT NOT NULL DEFAULT 0,
  note VARCHAR(1000) NOT NULL DEFAULT '',
  result_note VARCHAR(1000) NULL,
  accepted_at DATETIME NULL,
  service_date DATE NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_orders_user (user_id, id),
  KEY idx_orders_status (status, id),
  KEY idx_orders_created (created_at),
  CONSTRAINT fk_orders_user FOREIGN KEY (user_id) REFERENCES users (id),
  CONSTRAINT fk_orders_service FOREIGN KEY (service_id) REFERENCES services (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS transactions (
  id INT NOT NULL AUTO_INCREMENT,
  user_id INT NOT NULL,
  type ENUM('deposit', 'order', 'refund', 'adjust') NOT NULL,
  amount INT NOT NULL,
  balance_after BIGINT NOT NULL,
  ref VARCHAR(100) NOT NULL DEFAULT '',
  note VARCHAR(400) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_transactions_user (user_id, id),
  CONSTRAINT fk_transactions_user FOREIGN KEY (user_id) REFERENCES users (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS deposits (
  id INT NOT NULL AUTO_INCREMENT,
  user_id INT NOT NULL,
  amount INT NOT NULL,
  method VARCHAR(20) NOT NULL,
  reference VARCHAR(191) NOT NULL DEFAULT '',
  status ENUM('pending', 'approved', 'rejected') NOT NULL DEFAULT 'pending',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  reviewed_at DATETIME NULL,
  PRIMARY KEY (id),
  KEY idx_deposits_ref (reference),
  KEY idx_deposits_user (user_id, id),
  CONSTRAINT fk_deposits_user FOREIGN KEY (user_id) REFERENCES users (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS support_messages (
  id INT NOT NULL AUTO_INCREMENT,
  user_id INT NOT NULL,
  sender ENUM('user', 'admin') NOT NULL,
  body VARCHAR(4000) NOT NULL,
  read_by_user TINYINT NOT NULL DEFAULT 0,
  read_by_admin TINYINT NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_support_user (user_id, id),
  CONSTRAINT fk_support_user FOREIGN KEY (user_id) REFERENCES users (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS audit_log (
  id INT NOT NULL AUTO_INCREMENT,
  actor_id INT NOT NULL,
  action VARCHAR(40) NOT NULL,
  entity VARCHAR(40) NOT NULL,
  entity_id INT NULL,
  details VARCHAR(1000) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_audit_entity (entity, entity_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS password_resets (
  id INT NOT NULL AUTO_INCREMENT,
  user_id INT NOT NULL,
  token_hash CHAR(64) NOT NULL,
  expires_at DATETIME NOT NULL,
  used TINYINT NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_resets_token (token_hash),
  KEY idx_resets_user (user_id),
  CONSTRAINT fk_resets_user FOREIGN KEY (user_id) REFERENCES users (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- users: 2 rows
INSERT INTO users (id, name, email, phone, password_hash, role, balance, created_at, reseller_pct, api_key) VALUES (1, 'مدير النظام', 'admin@local', '', '$2a$10$CWcWtzsXPuYLYCF9mp0Ude57frrsTF6l7GC2CzKmEAz3Zb/X8llBC', 'admin', 0, '2026-12-05 19:29:43', 0, NULL);
INSERT INTO users (id, name, email, phone, password_hash, role, balance, created_at, reseller_pct, api_key) VALUES (2, 'Fouad Shabban', 'fouad@example.com', '01000000000', '$2a$10$.BiH/XacyIdA.bR6rcM2WOidjbWlTMhOHcDNL9fRYXH8z.Ct.3unO', 'user', 9956, '2026-12-05 19:29:43', 0, 'as_efdd14b52a704a4f90dd390692ed2e33554e0eddbc2de1ff');

-- services: 12 rows
INSERT INTO services (id, code, name_ar, category, price, cost, description, eta_hours, active, sort, name_en, description_en) VALUES (1, 'icloud-status', 'فحص حالة iCloud / FMI (تشغيل أو إيقاف)', 'checks', 4400, 2000, 'حالة FMI فقط (On/Off) — بدون أي بيانات شخصية أو إزالة للقفل.', 1, 1, 0, 'iCloud / FMI status check (On or Off)', 'FMI status only (On/Off) — no personal data and no lock removal.');
INSERT INTO services (id, code, name_ar, category, price, cost, description, eta_hours, active, sort, name_en, description_en) VALUES (2, 'blacklist-status', 'فحص البلاك ليست والحالة', 'checks', 5500, 2500, 'يوضح إن كان الجهاز نظيفاً أم مرتبطاً بقائمة المفقود/المسروق.', 1, 1, 1, 'Blacklist and device status check', 'Shows whether the device is clean or reported as lost or stolen.');
INSERT INTO services (id, code, name_ar, category, price, cost, description, eta_hours, active, sort, name_en, description_en) VALUES (3, 'warranty-check', 'فحص الضمان وتاريخ الشراء', 'checks', 3300, 1500, 'حالة الضمان وتاريخ الشراء التقريبي.', 1, 1, 2, 'Warranty and purchase date check', 'Warranty status and estimated purchase date.');
INSERT INTO services (id, code, name_ar, category, price, cost, description, eta_hours, active, sort, name_en, description_en) VALUES (4, 'simlock-check', 'فحص قفل الشبكة (SIM-Lock)', 'checks', 4400, 2000, 'يوضح الشبكة المقفول عليها الجهاز وهل هو مفتوح.', 1, 1, 3, 'SIM-lock status check', 'Shows which carrier the device is locked to and whether it is unlocked.');
INSERT INTO services (id, code, name_ar, category, price, cost, description, eta_hours, active, sort, name_en, description_en) VALUES (5, 'unlock-eligibility', 'فحص أهلية فك الشبكة', 'checks', 11000, 6000, 'فحص مبدئي قبل إرسال طلب الفك لتقليل حالات الرفض.', 24, 1, 4, 'Network unlock eligibility check', 'A pre-check before submitting an unlock request to reduce rejections.');
INSERT INTO services (id, code, name_ar, category, price, cost, description, eta_hours, active, sort, name_en, description_en) VALUES (6, 'gsx-report', 'تقرير جهاز كامل (GSX)', 'diagnostics', 42000, 25000, 'موديل، سعة، لون، دولة الشراء، وحالة الجهاز.', 24, 1, 5, 'Full device report (GSX)', 'Model, capacity, color, country of purchase, and device status.');
INSERT INTO services (id, code, name_ar, category, price, cost, description, eta_hours, active, sort, name_en, description_en) VALUES (7, 'unlock-att-clean', 'فك قفل AT&T (نظيف ومؤهل فقط)', 'network_unlock', 309500, 220000, 'للأجهزة نظيفة الحالة والمؤهلة فقط، تسليم خلال 24–48 ساعة.', 48, 1, 6, 'AT&T network unlock (clean & eligible only)', 'For clean, eligible devices only. Delivery within 24–48 hours.');
INSERT INTO services (id, code, name_ar, category, price, cost, description, eta_hours, active, sort, name_en, description_en) VALUES (8, 'unlock-tmobile-clean', 'فك قفل T-Mobile (نظيف ومؤهل)', 'network_unlock', 357500, 260000, 'أجهزة مؤهلة فقط (Fully Paid / Eligible).', 48, 1, 7, 'T-Mobile network unlock (clean & eligible)', 'Eligible devices only (Fully Paid / Eligible).');
INSERT INTO services (id, code, name_ar, category, price, cost, description, eta_hours, active, sort, name_en, description_en) VALUES (9, 'unlock-vodafone-uk', 'فك قفل Vodafone UK', 'network_unlock', 103000, 70000, 'خدمة قياسية، تسليم خلال 1–3 أيام.', 72, 1, 8, 'Vodafone UK network unlock', 'Standard service. Delivery within 1–3 days.');
INSERT INTO services (id, code, name_ar, category, price, cost, description, eta_hours, active, sort, name_en, description_en) VALUES (10, 'unlock-softbank-jp', 'فك قفل Softbank Japan', 'network_unlock', 54600, 35000, 'يدعم الأجهزة النظيفة وغير المدفوعة.', 48, 1, 9, 'Softbank Japan network unlock', 'Supports clean and unpaid devices.');
INSERT INTO services (id, code, name_ar, category, price, cost, description, eta_hours, active, sort, name_en, description_en) VALUES (11, 'icloud-ownership-review', 'مراجعة ملكية الجهاز وأهلية التعامل الرسمي مع iCloud', 'diagnostics', 2900, 0, 'مراجعة IMEI وحالة FMI (On/Off) وصورة الفاتورة، مع تقرير أهلية وخطوات التقديم الرسمي عبر Apple Support. خدمة استشارية فقط، ولا تتضمن إزالة أو تجاوز أي قفل.', 24, 1, 10, 'Device ownership & iCloud lock eligibility review', 'Review of IMEI, FMI status (On/Off) and invoice, with an eligibility report and the official owner-initiated steps through Apple Support. Advisory only; no lock removal or bypass.');
INSERT INTO services (id, code, name_ar, category, price, cost, description, eta_hours, active, sort, name_en, description_en) VALUES (12, 'icloud-ownership-review-priority', 'مراجعة ملكية عاجلة — تقرير أهلية iCloud', 'diagnostics', 5900, 0, 'نسخة عاجلة من مراجعة الملكية: فحص الحالة والفاتورة وإصدار التقرير خلال 12 ساعة، مع خطوات التقديم الرسمي عبر Apple Support. خدمة استشارية فقط، بدون أي فك أو تجاوز.', 12, 1, 11, 'Priority ownership review — iCloud lock eligibility report', 'Priority version of the ownership review: status and invoice check with the report delivered within 12 hours, plus the official owner-initiated steps through Apple Support. Advisory only; no lock removal or bypass.');

-- orders: 1 rows
INSERT INTO orders (id, user_id, service_id, device_info, status, price, cost, note, accepted_at, created_at, updated_at, result_note, device_model, service_date) VALUES (1, 2, 1, '12165464343131344666', 'processing', 44, 20, '', '2026-12-05 20:18:04', '2026-12-05 19:36:12', '2026-12-05 20:18:04', NULL, 'iphone12', NULL);

-- transactions: 1 rows
INSERT INTO transactions (id, user_id, type, amount, balance_after, ref, note, created_at) VALUES (1, 2, 'order', -44, 9956, 'ORDER-1', 'فحص حالة iCloud / FMI (تشغيل أو إيقاف)', '2026-12-05 19:36:12');

-- support_messages: 2 rows
INSERT INTO support_messages (id, user_id, sender, body, read_by_user, read_by_admin, created_at) VALUES (4, 2, 'user', 'ممكن الغي الخدمه', 1, 1, '2026-12-05 20:10:24');
INSERT INTO support_messages (id, user_id, sender, body, read_by_user, read_by_admin, created_at) VALUES (5, 2, 'admin', 'تحت امرك الخدمه الغيت', 1, 1, '2026-12-05 20:11:17');

-- audit_log: 3 rows
INSERT INTO audit_log (id, actor_id, action, entity, entity_id, details, created_at) VALUES (7, 1, 'service_toggle', 'service', 3, 'disabled', '2026-12-05 20:43:54');
INSERT INTO audit_log (id, actor_id, action, entity, entity_id, details, created_at) VALUES (8, 1, 'service_toggle', 'service', 3, 'enabled', '2026-12-05 20:43:54');
INSERT INTO audit_log (id, actor_id, action, entity, entity_id, details, created_at) VALUES (9, 1, 'api_key', 'user', 2, 'generated', '2026-12-05 20:50:23');

COMMIT;
