package com.lingniu.ingest.protocol.gb32960.handler;

import com.lingniu.ingest.api.event.VehicleEvent;
import com.lingniu.ingest.protocol.gb32960.mapper.Gb32960EventMapper;
import com.lingniu.ingest.protocol.gb32960.model.CommandBody;
import com.lingniu.ingest.protocol.gb32960.model.CommandType;
import com.lingniu.ingest.protocol.gb32960.model.EncryptType;
import com.lingniu.ingest.protocol.gb32960.model.Gb32960Header;
import com.lingniu.ingest.protocol.gb32960.model.Gb32960Message;
import com.lingniu.ingest.protocol.gb32960.model.ProtocolVersion;
import com.lingniu.ingest.protocol.gb32960.model.ResponseFlag;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * 保障 {@link Gb32960RealtimeHandler} 正确委托给 {@link Gb32960EventMapper}，
 * 不吞掉任何需要上送下游的事件。
 */
class Gb32960RealtimeHandlerTest {

    private final Gb32960RealtimeHandler handler = new Gb32960RealtimeHandler(new Gb32960EventMapper());

    @Test
    void onPlatform_login_emitsLoginEventWithKindPlatform() {
        Gb32960Message msg = buildPlatformLoginMessage("lingniu");

        List<VehicleEvent> events = handler.onPlatform(msg);

        assertThat(events).hasSize(1);
        VehicleEvent first = events.get(0);
        assertThat(first).isInstanceOf(VehicleEvent.Login.class);
        assertThat(first.vin()).isEqualTo("platform:lingniu");
        assertThat(first.metadata())
                .containsEntry("kind", "platform")
                .containsEntry("username", "lingniu");
    }

    @Test
    void onPlatform_logout_emitsLogoutEventWithKindPlatform() {
        Gb32960Message msg = new Gb32960Message(
                new Gb32960Header(
                        ProtocolVersion.V2016,
                        CommandType.PLATFORM_LOGOUT,
                        ResponseFlag.COMMAND,
                        "00000000000000000",
                        EncryptType.UNENCRYPTED,
                        0,
                        null),
                new CommandBody.PlatformLogout(Instant.parse("2026-04-20T10:00:00Z"), 1));

        List<VehicleEvent> events = handler.onPlatform(msg);

        assertThat(events).hasSize(1);
        assertThat(events.get(0)).isInstanceOf(VehicleEvent.Logout.class);
        assertThat(events.get(0).metadata()).containsEntry("kind", "platform");
    }

    private static Gb32960Message buildPlatformLoginMessage(String username) {
        return new Gb32960Message(
                new Gb32960Header(
                        ProtocolVersion.V2016,
                        CommandType.PLATFORM_LOGIN,
                        ResponseFlag.COMMAND,
                        "00000000000000000",
                        EncryptType.UNENCRYPTED,
                        0,
                        null),
                new CommandBody.PlatformLogin(
                        Instant.parse("2026-04-20T10:00:00Z"),
                        1,
                        username,
                        "secret",
                        EncryptType.UNENCRYPTED));
    }
}
