// Copyright © Selmo Technology GmbH. All Rights Reserved

using System.Text;

namespace SelmoStudio.Hmi.Server.Db
{
    public static class SqlProcessValueHistoryQueryHelper
    {
        private const string PrefixColumn = "C_";

        private static string ConvertToColumnName(Guid guid)
        {
            var cleanName = guid.ToString().Replace("-", string.Empty);
            return $"{PrefixColumn}{cleanName}";
        }

        private const string TimestampColumnName = "timestamp";

        private static string CreateColumsForCreateQuery(List<Guid> tableColumns, bool includingPk)
        {
            StringBuilder columns = new();

            if (includingPk)
            {
                columns.Append("id INTEGER PRIMARY KEY AUTOINCREMENT,");
            }

            columns.Append($"{TimestampColumnName} DATETIME DEFAULT CURRENT_TIMESTAMP,");

            foreach (var column in tableColumns)
            {
                columns.Append($"{ConvertToColumnName(column)} REAL,");
            }
            var cleanColumns = columns.ToString().TrimEnd(',');
            return cleanColumns;
        }

        private static string CreateColumsForInsertQuery(List<Guid> tableColumns)
        {
            StringBuilder columns = new();

            foreach (var column in tableColumns)
            {
                columns.Append($"{ConvertToColumnName(column)},");
            }
            var cleanColumns = columns.ToString().TrimEnd(',');
            return cleanColumns;
        }

        private static string CreateTableNameForDay(DateTime dateTime, int groupId)
        {
            var tableName = $"PVH_{groupId}_{dateTime.ToString("yyMMdd")}";

            return tableName;
        }

        public static string CreateTableNameForHour(DateTime dateTime, int groupId)
        {
            var tableName = $"PVH_{groupId}_{dateTime.ToString("yyMMddhh")}";

            return tableName;
        }
    }
}