-- Earnings and Withdrawal (Greenfield)
-- Duct-tape MVP: per-order earnings ledger + withdrawal requests

-- Earnings line per order per clerk
CREATE TABLE IF NOT EXISTS `play_earnings_line` (
  `id` varchar(32) NOT NULL COMMENT 'UUID',
  `tenant_id` varchar(32) NOT NULL COMMENT '租户ID',
  `clerk_id` varchar(32) NOT NULL COMMENT '店员ID',
  `order_id` varchar(32) NOT NULL COMMENT '订单ID',
  `amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '可提现收益金额（按订单预计收入计算）',
  `unlock_time` datetime NOT NULL COMMENT '解冻时间（订单完成时间 + 冻结时长）',
  `status` varchar(16) NOT NULL DEFAULT 'frozen' COMMENT '状态：frozen/available/withdrawing/withdrawn/reversed',
  `withdrawal_id` varchar(32) DEFAULT NULL COMMENT '关联提现请求ID',
  `created_by` varchar(32) DEFAULT NULL,
  `created_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated_by` varchar(32) DEFAULT NULL,
  `updated_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '逻辑删除 1已删除 0未删除',
  `version` int NOT NULL DEFAULT '1' COMMENT '数据版本',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_earnings_clerk_status_unlock` (`clerk_id`, `status`, `unlock_time`) USING BTREE,
  KEY `idx_earnings_order` (`order_id`) USING BTREE,
  KEY `key_tenant_id` (`tenant_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='店员订单收益台账';

-- Withdrawal requests (payout via Alipay - integration TBD)
CREATE TABLE IF NOT EXISTS `play_withdrawal_request` (
  `id` varchar(32) NOT NULL COMMENT 'UUID',
  `tenant_id` varchar(32) NOT NULL COMMENT '租户ID',
  `clerk_id` varchar(32) NOT NULL COMMENT '店员ID',
  `amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '提现申请金额（冻结可用收益）',
  `fee` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '提现手续费（由租户承担，仅记录）',
  `net_amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '实付给店员金额（通常等于 amount）',
  `dest_account` varchar(128) DEFAULT NULL COMMENT '提现目标（临时：支付宝账号/登录号）',
  `status` varchar(16) NOT NULL DEFAULT 'pending' COMMENT '状态：pending/processing/success/failed/canceled',
  `out_biz_no` varchar(64) DEFAULT NULL COMMENT '对外幂等号（用于支付宝转账）',
  `provider_ref` varchar(128) DEFAULT NULL COMMENT '三方流水号/参考号',
  `failure_reason` varchar(512) DEFAULT NULL COMMENT '失败原因',
  `created_by` varchar(32) DEFAULT NULL,
  `created_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated_by` varchar(32) DEFAULT NULL,
  `updated_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '逻辑删除 1已删除 0未删除',
  `version` int NOT NULL DEFAULT '1' COMMENT '数据版本',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_withdrawal_clerk_status` (`clerk_id`, `status`) USING BTREE,
  KEY `key_tenant_id` (`tenant_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='提现请求';

-- Freeze policy configuration (tenant default + optional clerk override)
CREATE TABLE IF NOT EXISTS `play_freeze_policy` (
  `id` varchar(32) NOT NULL COMMENT 'UUID',
  `tenant_id` varchar(32) NOT NULL COMMENT '租户ID',
  `clerk_id` varchar(32) DEFAULT NULL COMMENT '店员ID（为空代表租户默认）',
  `freeze_hours` int NOT NULL DEFAULT 24 COMMENT '冻结时长（单位小时）',
  `created_by` varchar(32) DEFAULT NULL,
  `created_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated_by` varchar(32) DEFAULT NULL,
  `updated_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '逻辑删除 1已删除 0未删除',
  `version` int NOT NULL DEFAULT '1' COMMENT '数据版本',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_policy_tenant_clerk` (`tenant_id`, `clerk_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='收益冻结策略';

-- Tenant Alipay config (placeholder; integration not implemented yet)
CREATE TABLE IF NOT EXISTS `play_tenant_alipay_config` (
  `id` varchar(32) NOT NULL COMMENT 'UUID',
  `tenant_id` varchar(32) NOT NULL COMMENT '租户ID',
  `app_id` varchar(64) DEFAULT NULL COMMENT '支付宝应用APP_ID',
  `merchant_private_key` text COMMENT '商户私钥（加密存储，预留）',
  `alipay_public_key` text COMMENT '支付宝公钥（预留）',
  `notify_url` varchar(255) DEFAULT NULL COMMENT '回调地址',
  `created_by` varchar(32) DEFAULT NULL,
  `created_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated_by` varchar(32) DEFAULT NULL,
  `updated_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '逻辑删除 1已删除 0未删除',
  `version` int NOT NULL DEFAULT '1' COMMENT '数据版本',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE KEY `uk_tenant_alipay` (`tenant_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='租户支付宝配置（暂未实现）';

