SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 币商充值订单是 admin 自己的跨 APP 绑定台账；Lalu 入账走 wallet-service，Aslan/Yumi 入账走 legacy 账套，但订单号占用和审计事实统一落在这里。
SET @now_ms = CAST(UNIX_TIMESTAMP(UTC_TIMESTAMP(3)) * 1000 AS UNSIGNED);

CREATE TABLE IF NOT EXISTS admin_coin_seller_recharge_orders (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码',
  target_user_id BIGINT NOT NULL COMMENT '币商真实 user_id',
  target_display_user_id VARCHAR(64) NOT NULL DEFAULT '' COMMENT '币商短 ID/展示 ID 快照',
  target_country_id BIGINT NOT NULL DEFAULT 0 COMMENT '币商国家 ID 快照',
  target_region_id BIGINT NOT NULL DEFAULT 0 COMMENT '币商区域 ID 快照',
  usd_amount DECIMAL(18,2) NOT NULL DEFAULT 0.00 COMMENT '运营确认的 USD/USDT 等值金额',
  usd_minor_amount BIGINT NOT NULL DEFAULT 0 COMMENT 'USD/USDT 美分口径金额',
  coin_amount BIGINT NOT NULL DEFAULT 0 COMMENT '发放给币商的金币库存数量',
  provider_code VARCHAR(32) NOT NULL COMMENT 'mifapay/v5pay/usdt',
  chain VARCHAR(16) NOT NULL DEFAULT '' COMMENT 'USDT 链：TRON/BSC',
  external_order_no VARCHAR(128) NOT NULL COMMENT '三方平台商户单号或链上 tx_hash',
  provider_order_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT '三方/链上返回的订单 ID 快照',
  provider_status VARCHAR(64) NOT NULL DEFAULT '' COMMENT '三方/链上状态快照',
  provider_country_code VARCHAR(16) NOT NULL DEFAULT '' COMMENT 'V5Pay 等三方查询国家',
  provider_currency_code VARCHAR(16) NOT NULL DEFAULT '' COMMENT '三方实付币种',
  provider_amount_minor BIGINT NOT NULL DEFAULT 0 COMMENT '三方实付最小单位金额或校验返回金额',
  provider_pay_type VARCHAR(64) NOT NULL DEFAULT '' COMMENT '三方支付方式/产品类型',
  receive_address VARCHAR(128) NOT NULL DEFAULT '' COMMENT 'USDT 收款地址快照',
  order_date_ms BIGINT NOT NULL DEFAULT 0 COMMENT 'MiFaPay 等按日查询的订单日期，UTC epoch ms',
  status VARCHAR(32) NOT NULL DEFAULT 'created',
  verify_status VARCHAR(32) NOT NULL DEFAULT 'pending',
  grant_status VARCHAR(32) NOT NULL DEFAULT 'pending',
  remark TEXT NULL COMMENT '运营备注，用于补单和异常说明',
  failure_reason TEXT NULL,
  provider_payload_json TEXT NULL COMMENT '三方或链上返回原始快照',
  wallet_command_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT '发放幂等命令 ID',
  wallet_transaction_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT 'wallet-service 或 legacy 流水 ID',
  wallet_asset_type VARCHAR(64) NOT NULL DEFAULT '' COMMENT '账务资产类型',
  wallet_amount_delta BIGINT NOT NULL DEFAULT 0 COMMENT '金币库存变动数量',
  wallet_balance_after BIGINT NOT NULL DEFAULT 0 COMMENT '发放后余额快照',
  operator_user_id BIGINT UNSIGNED NOT NULL COMMENT '创建人 admin id',
  operator_name VARCHAR(64) NOT NULL DEFAULT '' COMMENT '创建人名称',
  verified_by_user_id BIGINT UNSIGNED NULL COMMENT '校验人 admin id',
  verified_by_name VARCHAR(64) NOT NULL DEFAULT '' COMMENT '校验人名称',
  granted_by_user_id BIGINT UNSIGNED NULL COMMENT '发放人 admin id',
  granted_by_name VARCHAR(64) NOT NULL DEFAULT '' COMMENT '发放人名称',
  verified_at_ms BIGINT NULL COMMENT '校验时间，UTC epoch ms',
  granted_at_ms BIGINT NULL COMMENT '发放时间，UTC epoch ms',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (id),
  UNIQUE KEY uk_admin_coin_seller_recharge_external (app_code, provider_code, chain, external_order_no),
  KEY idx_admin_coin_seller_recharge_app_time (app_code, created_at_ms),
  KEY idx_admin_coin_seller_recharge_target (target_user_id),
  KEY idx_admin_coin_seller_recharge_display (target_display_user_id),
  KEY idx_admin_coin_seller_recharge_provider (provider_code),
  KEY idx_admin_coin_seller_recharge_external (external_order_no),
  KEY idx_admin_coin_seller_recharge_status_time (status, created_at_ms),
  KEY idx_admin_coin_seller_recharge_verify (verify_status),
  KEY idx_admin_coin_seller_recharge_grant (grant_status),
  KEY idx_admin_coin_seller_recharge_command (wallet_command_id),
  KEY idx_admin_coin_seller_recharge_wallet_tx (wallet_transaction_id),
  KEY idx_admin_coin_seller_recharge_operator (operator_user_id),
  KEY idx_admin_coin_seller_recharge_verifier (verified_by_user_id),
  KEY idx_admin_coin_seller_recharge_granter (granted_by_user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='币商充值订单与外部账单绑定台账';

INSERT INTO admin_permissions (name, code, kind, description, created_at_ms, updated_at_ms) VALUES
  ('币商充值订单查看', 'finance-order:coin-seller-recharge:view', 'menu', '允许查看币商充值订单绑定台账', @now_ms, @now_ms),
  ('币商充值订单创建', 'finance-order:coin-seller-recharge:create', 'button', '允许创建币商充值订单并绑定外部订单号', @now_ms, @now_ms),
  ('币商充值订单校验', 'finance-order:coin-seller-recharge:verify', 'button', '允许校验三方订单或 USDT 交易凭证', @now_ms, @now_ms),
  ('币商充值订单发放', 'finance-order:coin-seller-recharge:grant', 'button', '允许校验通过后确认发放币商金币库存', @now_ms, @now_ms)
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  kind = VALUES(kind),
  description = VALUES(description),
  updated_at_ms = @now_ms;

INSERT INTO admin_menus (parent_id, title, code, path, icon, permission_code, sort, visible, created_at_ms, updated_at_ms)
SELECT parent.id, '币商充值订单', 'finance-coin-seller-recharge-orders', '/finance/orders/coin-seller-recharges', 'receipt', 'finance-order:coin-seller-recharge:view', 72, TRUE, @now_ms, @now_ms
FROM admin_menus parent
WHERE parent.code = 'payment'
ON DUPLICATE KEY UPDATE
  parent_id = VALUES(parent_id),
  title = VALUES(title),
  path = VALUES(path),
  icon = VALUES(icon),
  permission_code = VALUES(permission_code),
  sort = VALUES(sort),
  visible = VALUES(visible),
  updated_at_ms = @now_ms;

INSERT IGNORE INTO admin_role_permissions (role_id, permission_id)
SELECT admin_role.id, admin_permission.id
FROM admin_roles admin_role
JOIN admin_permissions admin_permission
WHERE admin_role.code = 'platform-admin'
  AND admin_permission.code IN (
    'finance-order:coin-seller-recharge:view',
    'finance-order:coin-seller-recharge:create',
    'finance-order:coin-seller-recharge:verify',
    'finance-order:coin-seller-recharge:grant'
  );

INSERT IGNORE INTO admin_role_permissions (role_id, permission_id)
SELECT admin_role.id, admin_permission.id
FROM admin_roles admin_role
JOIN admin_permissions admin_permission
WHERE admin_role.code = 'ops-admin'
  AND admin_permission.code IN (
    'finance-order:coin-seller-recharge:view',
    'finance-order:coin-seller-recharge:create',
    'finance-order:coin-seller-recharge:verify',
    'finance-order:coin-seller-recharge:grant'
  );

INSERT IGNORE INTO admin_role_permissions (role_id, permission_id)
SELECT admin_role.id, admin_permission.id
FROM admin_roles admin_role
JOIN admin_permissions admin_permission
WHERE admin_role.code IN ('auditor', 'readonly')
  AND admin_permission.code = 'finance-order:coin-seller-recharge:view';
