# -*- coding: utf-8 -*-
# Dò tiếp DTO command v2: thêm lần lượt các field cho tới khi vượt tầng validation
# (chỉ dò schema - signature giả nên xe sẽ từ chối, KHÔNG có tác động vật lý)
import json, io, sys, requests, base64, time
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.path.insert(0, r'E:\ios\vinfast\vinfast-console')

INTERNAL = json.load(open(r'E:\ios\vinfast\vinfast-console\data\internal_token.json', encoding='utf-8'))
TOK = INTERNAL['access_token']
VIN = 'RLNV5JSE4TH730406'
EMAIL = 'minhhai113he@gmail.com'
BASE = 'https://mobile.connected-car.vinfast.vn'
DEV = '5ED66E61-C9B8-408D-B5CE-44275F59BB5F'
URL = BASE + '/ccaraccessmgmt/api/v2/remote/app/command'

def headers():
    return {
        "user-agent": f"iOS - {DEV} - 2.26.11",
        "x-service-name": "CAPP", "x-app-version": "2.26.11",
        "x-device-identifier": DEV, "x-device-family": "iPhone 15 Pro",
        "x-device-platform": "iPhone15Pro", "x-device-locale": "vi_VN",
        "x-device-os-version": "27.0.1", "x-timezone": "Asia/Ho_Chi_Minh",
        "x-player-identifier": EMAIL,
        "Authorization": f"Bearer {TOK}",
        "Content-Type": "application/json",
        "x-vin-code": VIN,
    }

sig = base64.b64encode(b'\x00' * 64).decode()
ts = str(int(time.time() * 1000))

BODIES = [
    # từng bước thêm field theo lỗi lộ ra
    {'commandType': 1, 'vinCode': VIN, 'params': {},
     'messageContent': base64.b64encode(b'{}').decode(), 'signature': sig, 'userId': EMAIL},
    {'commandType': 1, 'vinCode': VIN, 'params': {},
     'messageContent': base64.b64encode(b'{}').decode(), 'signature': sig,
     'userId': EMAIL, 'messageId': ts, 'timestamp': ts},
    {'commandType': 1, 'vinCode': VIN, 'params': {},
     'messageContent': base64.b64encode(b'{}').decode(), 'signature': sig,
     'userId': EMAIL, 'messageId': ts, 'timestamp': ts,
     'messageName': 'LOCK_DOOR', 'messageType': 1, 'messageCommand': 1},
]

for i, b in enumerate(BODIES, 1):
    r = requests.post(URL, headers=headers(), json=b, timeout=20)
    print(f'[{r.status_code}] THỬ {i}: {(r.text or "")[:200]}')
