package lc.sips.postgres;

import lc.mecha.log.MechaLogger;
import lc.mecha.log.MechaLoggerFactory;
import lc.sips.schema.ColumnDefinition;
import lc.sips.schema.ColumnType;
import lc.sips.schema.Table;
import org.junit.jupiter.api.Test;

import java.util.Set;
import java.util.TreeSet;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class PostgresDialectTest {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(PostgresDialectTest.class);

    @Test
    public void testCreateTableReference() {
        var dialect = new PostgresDialect("junit");
        // We sort this so that the output SQL is predictable
        var cols = new TreeSet<ColumnDefinition>();
        cols.add(new ColumnDefinition("msg", ColumnType.ID, 0, "user", true));

        var t = new Table("messages", cols);
        var query = dialect.createTableQuery(t);
        logger.info("query: {}", query);

        assertEquals("CREATE TABLE \"junit_messages\" (_id BIGSERIAL PRIMARY KEY,\"msg\" BIGSERIAL REFERENCES \"user\"(_id) ON DELETE CASCADE)",
                query.get(0));
    }

    @Test
    public void testCreateTable() {
        var dialect = new PostgresDialect("junit");
        // We sort this so that the output SQL is predictable
        var cols = new TreeSet<ColumnDefinition>();

        cols.add(new ColumnDefinition("msg", ColumnType.VARCHAR, 256, null, false));
        cols.add(new ColumnDefinition("num", ColumnType.BIGINT, 0, null, false));

        var t = new Table("test", cols);
        var query = dialect.createTableQuery(t);
        logger.info("query: {}", query);

        assertEquals("CREATE TABLE \"junit_test\" (_id BIGSERIAL PRIMARY KEY,msg VARCHAR(256),num BIGINT)",
                query.get(0));
    }

    @Test
    public void testDropTable() {
        var dialect = new PostgresDialect("junit");
        var t = new Table("test",
                Set.of(new ColumnDefinition("msg", ColumnType.VARCHAR, 256,
                                null, false),
                        new ColumnDefinition("num", ColumnType.BIGINT, 0,
                                null, false)));
        var query = dialect.createTableDrop(t);
        logger.info("query: {}", query);

        assertEquals("DROP TABLE junit_test", query.get(0));
    }
}
