SET NAMES utf8mb4;

CREATE TABLE IF NOT EXISTS users (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  telegram_id BIGINT NOT NULL,
  username VARCHAR(64) NULL,
  first_name VARCHAR(128) NOT NULL DEFAULT '',
  last_name VARCHAR(128) NULL,
  language VARCHAR(8) NOT NULL DEFAULT 'fa',
  balance BIGINT NOT NULL DEFAULT 0,
  total_purchases BIGINT NOT NULL DEFAULT 0,
  status ENUM('active','blocked') NOT NULL DEFAULT 'active',
  notify_optional TINYINT(1) NOT NULL DEFAULT 1,
  referred_by BIGINT UNSIGNED NULL,
  created_at DATETIME NOT NULL,
  updated_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_users_tg (telegram_id),
  KEY idx_users_username (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS admin_users (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  telegram_id BIGINT NOT NULL,
  role ENUM('owner','admin','support') NOT NULL DEFAULT 'admin',
  created_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_admin_tg (telegram_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS bot_settings (
  skey VARCHAR(64) NOT NULL,
  svalue TEXT NOT NULL,
  updated_at DATETIME NOT NULL,
  PRIMARY KEY (skey)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_states (
  telegram_id BIGINT NOT NULL,
  state VARCHAR(64) NOT NULL,
  data TEXT NULL,
  updated_at DATETIME NOT NULL,
  PRIMARY KEY (telegram_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS webhook_updates (
  update_id BIGINT NOT NULL,
  received_at DATETIME NOT NULL,
  PRIMARY KEY (update_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rate_limits (
  telegram_id BIGINT NOT NULL,
  window_start INT UNSIGNED NOT NULL,
  hits INT UNSIGNED NOT NULL DEFAULT 0,
  PRIMARY KEY (telegram_id, window_start)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS durations (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  label VARCHAR(64) NOT NULL,
  days INT UNSIGNED NOT NULL,
  sort_order INT NOT NULL DEFAULT 0,
  active TINYINT(1) NOT NULL DEFAULT 1,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS pricing_rules (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  duration_id INT UNSIGNED NOT NULL,
  name VARCHAR(96) NOT NULL,
  base_price BIGINT NOT NULL DEFAULT 0,
  price_per_gb BIGINT NOT NULL DEFAULT 0,
  min_gb INT UNSIGNED NOT NULL DEFAULT 1,
  max_gb INT UNSIGNED NOT NULL DEFAULT 1000,
  min_total BIGINT NOT NULL DEFAULT 0,
  max_total BIGINT NOT NULL DEFAULT 0,
  discount_percent TINYINT UNSIGNED NOT NULL DEFAULT 0,
  priority INT NOT NULL DEFAULT 0,
  active TINYINT(1) NOT NULL DEFAULT 1,
  PRIMARY KEY (id),
  KEY idx_pr_duration (duration_id, active),
  CONSTRAINT fk_pr_duration FOREIGN KEY (duration_id) REFERENCES durations(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS coupons (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  code VARCHAR(48) NOT NULL,
  type ENUM('percent','fixed') NOT NULL,
  value BIGINT NOT NULL,
  max_discount BIGINT NOT NULL DEFAULT 0,
  min_order BIGINT NOT NULL DEFAULT 0,
  duration_id INT UNSIGNED NULL,
  pricing_rule_id INT UNSIGNED NULL,
  expires_at DATETIME NULL,
  usage_limit INT UNSIGNED NOT NULL DEFAULT 0,
  per_user_limit INT UNSIGNED NOT NULL DEFAULT 1,
  used_count INT UNSIGNED NOT NULL DEFAULT 0,
  active TINYINT(1) NOT NULL DEFAULT 1,
  created_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_coupon_code (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS subscriptions (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id BIGINT UNSIGNED NOT NULL,
  panel_username VARCHAR(64) NOT NULL,
  subscription_url TEXT NULL,
  duration_days INT UNSIGNED NOT NULL,
  last_known_status VARCHAR(24) NULL,
  last_known_limit BIGINT NULL,
  last_known_used BIGINT NULL,
  last_known_expire BIGINT NULL,
  last_synced_at DATETIME NULL,
  purchased_at DATETIME NOT NULL,
  last_renewed_at DATETIME NULL,
  deleted_at DATETIME NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_sub_username (panel_username),
  KEY idx_sub_user (user_id),
  CONSTRAINT fk_sub_user FOREIGN KEY (user_id) REFERENCES users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS orders (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id BIGINT UNSIGNED NOT NULL,
  kind ENUM('purchase','renewal') NOT NULL,
  subscription_id BIGINT UNSIGNED NULL,
  duration_id INT UNSIGNED NOT NULL,
  days INT UNSIGNED NOT NULL,
  gb INT UNSIGNED NOT NULL,
  bytes_added BIGINT UNSIGNED NOT NULL,
  pricing_rule_id INT UNSIGNED NULL,
  list_price BIGINT NOT NULL,
  discount BIGINT NOT NULL DEFAULT 0,
  final_price BIGINT NOT NULL,
  coupon_id INT UNSIGNED NULL,
  panel_username VARCHAR(64) NULL,
  status ENUM('draft','paid','provisioning','completed','refunded','needs_review','cancelled') NOT NULL DEFAULT 'draft',
  fail_reason VARCHAR(255) NULL,
  created_at DATETIME NOT NULL,
  paid_at DATETIME NULL,
  completed_at DATETIME NULL,
  PRIMARY KEY (id),
  KEY idx_orders_user (user_id),
  KEY idx_orders_status (status, paid_at),
  KEY idx_orders_sub (subscription_id),
  CONSTRAINT fk_orders_user FOREIGN KEY (user_id) REFERENCES users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS renewal_history (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_id BIGINT UNSIGNED NOT NULL,
  subscription_id BIGINT UNSIGNED NOT NULL,
  old_limit BIGINT NOT NULL,
  old_used BIGINT NOT NULL,
  old_expire BIGINT NOT NULL,
  old_status VARCHAR(24) NULL,
  added_bytes BIGINT NOT NULL,
  added_days INT NOT NULL,
  target_limit BIGINT NOT NULL,
  target_expire BIGINT NOT NULL,
  status ENUM('planned','applied','rolled_back','needs_review') NOT NULL DEFAULT 'planned',
  attempts INT UNSIGNED NOT NULL DEFAULT 0,
  note VARCHAR(255) NULL,
  created_at DATETIME NOT NULL,
  updated_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_renewal_order (order_id),
  KEY idx_renewal_sub (subscription_id),
  CONSTRAINT fk_renewal_order FOREIGN KEY (order_id) REFERENCES orders(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS wallet_transactions (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id BIGINT UNSIGNED NOT NULL,
  type ENUM('deposit','purchase','renewal','refund','admin_adjust') NOT NULL,
  amount BIGINT NOT NULL,
  balance_after BIGINT NOT NULL,
  ref_type VARCHAR(24) NULL,
  ref_id BIGINT UNSIGNED NULL,
  idempotency_key VARCHAR(96) NOT NULL,
  note VARCHAR(255) NULL,
  admin_telegram_id BIGINT NULL,
  created_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_wt_idem (idempotency_key),
  KEY idx_wt_user (user_id, id),
  KEY idx_wt_type (type, created_at),
  CONSTRAINT fk_wt_user FOREIGN KEY (user_id) REFERENCES users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS deposit_requests (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id BIGINT UNSIGNED NOT NULL,
  method ENUM('card','link','gateway') NOT NULL,
  amount BIGINT NOT NULL,
  reference VARCHAR(96) NULL,
  receipt_file_id VARCHAR(255) NULL,
  receipt_unique_id VARCHAR(64) NULL,
  status ENUM('awaiting_receipt','pending','approved','rejected','paid','failed','expired') NOT NULL,
  admin_note VARCHAR(255) NULL,
  decided_by BIGINT NULL,
  decided_at DATETIME NULL,
  gateway_name VARCHAR(32) NULL,
  gateway_ref VARCHAR(128) NULL,
  gateway_url TEXT NULL,
  created_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_dep_gateway (gateway_name, gateway_ref),
  KEY idx_dep_status (status, created_at),
  KEY idx_dep_user (user_id),
  KEY idx_dep_receipt (receipt_unique_id),
  KEY idx_dep_ref (gateway_name, reference),
  CONSTRAINT fk_dep_user FOREIGN KEY (user_id) REFERENCES users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS payment_records (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  deposit_id BIGINT UNSIGNED NOT NULL,
  gateway VARCHAR(32) NOT NULL,
  event_key VARCHAR(128) NOT NULL,
  payload TEXT NULL,
  created_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_pay_event (gateway, event_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS coupon_usages (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  coupon_id INT UNSIGNED NOT NULL,
  user_id BIGINT UNSIGNED NOT NULL,
  order_id BIGINT UNSIGNED NOT NULL,
  discount BIGINT NOT NULL,
  created_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_cu_order (order_id),
  KEY idx_cu_user (coupon_id, user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS support_tickets (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id BIGINT UNSIGNED NOT NULL,
  subject VARCHAR(160) NOT NULL,
  status ENUM('open','waiting_admin','waiting_customer','closed') NOT NULL DEFAULT 'waiting_admin',
  created_at DATETIME NOT NULL,
  updated_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  KEY idx_tk_user (user_id),
  KEY idx_tk_status (status, updated_at),
  CONSTRAINT fk_tk_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 BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  ticket_id BIGINT UNSIGNED NOT NULL,
  sender ENUM('customer','admin') NOT NULL,
  admin_telegram_id BIGINT NULL,
  body TEXT NULL,
  file_id VARCHAR(255) NULL,
  file_type ENUM('photo','document') NULL,
  created_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  KEY idx_sm_ticket (ticket_id, id),
  CONSTRAINT fk_sm_ticket FOREIGN KEY (ticket_id) REFERENCES support_tickets(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS notifications (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id BIGINT UNSIGNED NOT NULL,
  dedupe_key VARCHAR(128) NOT NULL,
  category VARCHAR(32) NOT NULL,
  body TEXT NOT NULL,
  status ENUM('pending','sent','failed') NOT NULL DEFAULT 'pending',
  attempts INT UNSIGNED NOT NULL DEFAULT 0,
  last_error VARCHAR(255) NULL,
  created_at DATETIME NOT NULL,
  sent_at DATETIME NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_notif_dedupe (dedupe_key),
  KEY idx_notif_status (status, attempts)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS broadcasts (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  admin_telegram_id BIGINT NOT NULL,
  body TEXT NULL,
  photo_file_id VARCHAR(255) NULL,
  status ENUM('queued','running','done') NOT NULL DEFAULT 'queued',
  total INT UNSIGNED NOT NULL DEFAULT 0,
  sent INT UNSIGNED NOT NULL DEFAULT 0,
  failed INT UNSIGNED NOT NULL DEFAULT 0,
  last_user_id BIGINT UNSIGNED NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS activity_logs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  actor_telegram_id BIGINT NULL,
  action VARCHAR(64) NOT NULL,
  target VARCHAR(96) NULL,
  details TEXT NULL,
  created_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  KEY idx_al_actor (actor_telegram_id, id),
  KEY idx_al_action (action, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
