package com.lingniu.ingest.tdenginehistory;

import org.junit.jupiter.api.Test;

import java.time.Instant;

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

class TdengineHistoryStatementsTest {

    private final TdengineHistorySchema schema = new TdengineHistorySchema("vehicle_history");
    private final TdengineHistoryStatements statements = new TdengineHistoryStatements(schema);

    @Test
    void rawFrameBatchStatementCreatesChildTableAndParameterizedInsert() {
        TdengineRawFrameRow row = new TdengineRawFrameRow(
                Instant.parse("2026-06-29T05:00:01Z"),
                "frame-1",
                Instant.parse("2026-06-29T05:00:02Z"),
                0x0200,
                0,
                Instant.parse("2026-06-29T05:00:01Z"),
                "archive://jt808/frame-1.bin",
                "sha256:abc",
                67,
                "SUCCEEDED",
                "",
                "10.0.0.1:808",
                "{\"auth\":\"passed\"}",
                "{\"body\":{\"speedKmh\":42.0}}",
                "JT808",
                "jt808:g7gps",
                "VIN123",
                "013800000000");

        TdengineBatchStatement batch = statements.rawFrame(row);

        assertThat(batch.createChildTableSql())
                .startsWith("CREATE TABLE IF NOT EXISTS raw_jt808_")
                .contains(" USING raw_frames TAGS ('JT808', 'jt808:g7gps', 'VIN123', '013800000000')");
        assertThat(batch.insertSql())
                .startsWith("INSERT INTO raw_jt808_")
                .contains("(ts, frame_id, received_at, message_id, sub_type, event_time, raw_uri, checksum, raw_size_bytes, parse_status, parse_error, peer, metadata_json, parsed_json)")
                .endsWith("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        assertThat(batch.values())
                .containsExactly(
                        row.ts(), row.frameId(), row.receivedAt(), row.messageId(), row.subType(),
                        row.eventTime(), row.rawUri(), row.checksum(), row.rawSizeBytes(), row.parseStatus(),
                        row.parseError(), row.peer(), row.metadataJson(), row.parsedJson());
    }

    @Test
    void locationBatchStatementEscapesTags() {
        TdengineLocationRow row = new TdengineLocationRow(
                Instant.parse("2026-06-29T05:00:01Z"),
                "fact-1",
                "frame-1",
                Instant.parse("2026-06-29T05:00:02Z"),
                113.1,
                23.4,
                8.0,
                42.0,
                90.0,
                1,
                3,
                null,
                "archive://gb32960/frame-1.bin",
                "GB32960",
                "vin:O'HARE",
                "O'HARE",
                "");

        TdengineBatchStatement batch = statements.location(row);

        assertThat(batch.createChildTableSql())
                .startsWith("CREATE TABLE IF NOT EXISTS loc_gb32960_")
                .contains(" USING vehicle_locations TAGS ('GB32960', 'vin:O''HARE', 'O''HARE', '')");
        assertThat(batch.values())
                .containsExactly(
                        row.ts(), row.factId(), row.frameId(), row.receivedAt(), row.longitude(), row.latitude(),
                        row.altitudeM(), row.speedKmh(), row.directionDeg(), row.alarmFlag(), row.statusFlag(),
                        row.totalMileageKm(), row.rawUri());
    }

}
