package com.cloudhandson.vpdbackoffice.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import com.cloudhandson.vpdbackoffice.config.BackofficeProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

class SelectAiServiceTest {

  private final SelectAiService service = new SelectAiService(
      new BackofficeProperties(null, null, null, null, null),
      token -> new HmmMcpPrincipal(1L, "E1001", 1L),
      new ObjectMapper());

  @Test
  void acceptsOneReadOnlySelectAndRemovesTrailingSemicolon() {
    assertThat(service.validateReadOnlySql(
        "SELECT employee_code FROM hmm_hr_employees;"))
        .isEqualTo("SELECT employee_code FROM hmm_hr_employees");
  }

  @Test
  void rejectsDmlPackagesCommentsAndMultipleStatements() {
    assertThatThrownBy(() ->
        service.validateReadOnlySql("SELECT * FROM x; DELETE FROM x"))
        .isInstanceOf(AppException.class);
    assertThatThrownBy(() ->
        service.validateReadOnlySql("SELECT DBMS_LOCK.SLEEP(10) FROM dual"))
        .isInstanceOf(AppException.class);
    assertThatThrownBy(() ->
        service.validateReadOnlySql("SELECT * FROM x -- bypass"))
        .isInstanceOf(AppException.class);
  }
}
