-- ===================================================================
-- 模拟邀请数据 SQL - 用于测试 Agent/Invite APIs
-- ===================================================================
-- 说明：
-- 1. 假设当前登录用户 ID = 524（邀请者）
-- 2. 创建 5 个被邀请用户（user_id: 10001-10005）
-- 3. 其中 3 个用户有付费订单（user_id: 10001, 10002, 10003）
-- 4. 2 个用户没有订单（user_id: 10004, 10005）
-- ===================================================================

-- 1. 插入被邀请用户（referer_id = 524，即当前登录用户）
-- 注意：如果这些 ID 已存在，请先手动修改为其他未使用的 ID

-- 用户 10001：有2个已支付订单
INSERT INTO `user` (
    `id`, `referer_id`, `refer_code`, `password`, `is_admin`, 
    `balance`, `commission`, `created_at`, `updated_at`
) VALUES (
    10001, 524, 'MOCK_CODE_01', '$2a$10$mock.hash.for.testing.only.password.bcrypt.hash', 0,
    0, 0, NOW(), NOW()
) ON DUPLICATE KEY UPDATE `referer_id` = 524;

-- 为用户 10001 添加 email 认证方式
INSERT INTO `user_auth_methods` (`user_id`, `auth_type`, `auth_identifier`, `verified`, `created_at`, `updated_at`)
VALUES (10001, 'email', 'mock_user_001@test.com', 1, NOW(), NOW())
ON DUPLICATE KEY UPDATE `auth_identifier` = 'mock_user_001@test.com';

-- 用户 10002：有1个已支付订单
INSERT INTO `user` (
    `id`, `referer_id`, `refer_code`, `password`, `is_admin`, 
    `balance`, `commission`, `created_at`, `updated_at`
) VALUES (
    10002, 524, 'MOCK_CODE_02', '$2a$10$mock.hash.for.testing.only.password.bcrypt.hash', 0,
    0, 0, NOW(), NOW()
) ON DUPLICATE KEY UPDATE `referer_id` = 524;

INSERT INTO `user_auth_methods` (`user_id`, `auth_type`, `auth_identifier`, `verified`, `created_at`, `updated_at`)
VALUES (10002, 'email', 'mock_user_002@test.com', 1, NOW(), NOW())
ON DUPLICATE KEY UPDATE `auth_identifier` = 'mock_user_002@test.com';

-- 用户 10003：有1个已完成订单
INSERT INTO `user` (
    `id`, `referer_id`, `refer_code`, `password`, `is_admin`, 
    `balance`, `commission`, `created_at`, `updated_at`
) VALUES (
    10003, 524, 'MOCK_CODE_03', '$2a$10$mock.hash.for.testing.only.password.bcrypt.hash', 0,
    0, 0, NOW(), NOW()
) ON DUPLICATE KEY UPDATE `referer_id` = 524;

INSERT INTO `user_auth_methods` (`user_id`, `auth_type`, `auth_identifier`, `verified`, `created_at`, `updated_at`)
VALUES (10003, 'email', 'mock_user_003@test.com', 1, NOW(), NOW())
ON DUPLICATE KEY UPDATE `auth_identifier` = 'mock_user_003@test.com';

-- 用户 10004：没有订单（历史邀请但未付费）
INSERT INTO `user` (
    `id`, `referer_id`, `refer_code`, `password`, `is_admin`, 
    `balance`, `commission`, `created_at`, `updated_at`
) VALUES (
    10004, 524, 'MOCK_CODE_04', '$2a$10$mock.hash.for.testing.only.password.bcrypt.hash', 0,
    0, 0, NOW(), NOW()
) ON DUPLICATE KEY UPDATE `referer_id` = 524;

INSERT INTO `user_auth_methods` (`user_id`, `auth_type`, `auth_identifier`, `verified`, `created_at`, `updated_at`)
VALUES (10004, 'email', 'mock_user_004@test.com', 1, NOW(), NOW())
ON DUPLICATE KEY UPDATE `auth_identifier` = 'mock_user_004@test.com';

-- 用户 10005：没有订单（历史邀请但未付费）
INSERT INTO `user` (
    `id`, `referer_id`, `refer_code`, `password`, `is_admin`, 
    `balance`, `commission`, `created_at`, `updated_at`
) VALUES (
    10005, 524, 'MOCK_CODE_05', '$2a$10$mock.hash.for.testing.only.password.bcrypt.hash', 0,
    0, 0, NOW(), NOW()
) ON DUPLICATE KEY UPDATE `referer_id` = 524;

INSERT INTO `user_auth_methods` (`user_id`, `auth_type`, `auth_identifier`, `verified`, `created_at`, `updated_at`)
VALUES (10005, 'email', 'mock_user_005@test.com', 1, NOW(), NOW())
ON DUPLICATE KEY UPDATE `auth_identifier` = 'mock_user_005@test.com';



-- ===================================================================
-- 2. 插入订单数据
-- status: 2 = Paid（已支付）, 5 = Finished（已完成）
-- ===================================================================

-- 订单 1：用户 10001，已支付，金额 $9.99
INSERT INTO `order` (
    `user_id`, `order_no`, `type`, `status`, `amount`, `quantity`,
    `payment_id`, `created_at`, `updated_at`
) VALUES (
    10001, 'MOCK_ORDER_001', 1, 2, 999, 1,
    1, DATE_SUB(NOW(), INTERVAL 10 DAY), DATE_SUB(NOW(), INTERVAL 10 DAY)
);

-- 订单 2：用户 10001，已支付，金额 $19.99
INSERT INTO `order` (
    `user_id`, `order_no`, `type`, `status`, `amount`, `quantity`,
    `payment_id`, `created_at`, `updated_at`
) VALUES (
    10001, 'MOCK_ORDER_002', 1, 2, 1999, 1,
    1, DATE_SUB(NOW(), INTERVAL 5 DAY), DATE_SUB(NOW(), INTERVAL 5 DAY)
);

-- 订单 3：用户 10002，已支付，金额 $29.99
INSERT INTO `order` (
    `user_id`, `order_no`, `type`, `status`, `amount`, `quantity`,
    `payment_id`, `created_at`, `updated_at`
) VALUES (
    10002, 'MOCK_ORDER_003', 1, 2, 2999, 1,
    1, DATE_SUB(NOW(), INTERVAL 3 DAY), DATE_SUB(NOW(), INTERVAL 3 DAY)
);

-- 订单 4：用户 10003，已完成，金额 $49.99
INSERT INTO `order` (
    `user_id`, `order_no`, `type`, `status`, `amount`, `quantity`,
    `payment_id`, `created_at`, `updated_at`
) VALUES (
    10003, 'MOCK_ORDER_004', 1, 5, 4999, 1,
    1, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)
);

-- 订单 5：用户 10001，待支付（status=1，不会被统计）
INSERT INTO `order` (
    `user_id`, `order_no`, `type`, `status`, `amount`, `quantity`,
    `payment_id`, `created_at`, `updated_at`
) VALUES (
    10001, 'MOCK_ORDER_005', 1, 1, 999, 1,
    1, NOW(), NOW()
);


-- ===================================================================
-- 3. 验证查询
-- ===================================================================

-- 查询当前用户(524)的邀请统计
-- 应返回：
-- - friendlyCount: 3 (user_id 10001, 10002, 10003 有已支付/完成订单)
-- - historyCount: 5 (user_id 10001-10005 都是被邀请用户)
SELECT 
    (SELECT COUNT(*) FROM user WHERE referer_id = 524 
     AND EXISTS (SELECT 1 FROM `order` o WHERE o.user_id = user.id AND o.status IN (2, 5))
    ) as friendlyCount,
    (SELECT COUNT(*) FROM user WHERE referer_id = 524) as historyCount;

-- 查询销售记录（分页查询，前10条）
-- 应返回 4 条记录（MOCK_ORDER_001 到 MOCK_ORDER_004）
SELECT 
    o.amount,
    UNIX_TIMESTAMP(o.created_at) * 1000 as created_at,
    u.id as user_id,
    COALESCE(am.auth_identifier, 'no_email') as user_email
FROM `order` o
JOIN user u ON o.user_id = u.id
LEFT JOIN user_auth_methods am ON am.user_id = u.id AND am.auth_type = 'email'
WHERE u.referer_id = 524 AND o.status IN (2, 5)
ORDER BY o.created_at DESC
LIMIT 10 OFFSET 0;



-- ===================================================================
-- 清理命令（如需删除测试数据，请执行以下语句）
-- ===================================================================
-- DELETE FROM `order` WHERE order_no LIKE 'MOCK_ORDER_%';
-- DELETE FROM `user` WHERE id BETWEEN 10001 AND 10005;
