using Azure.Cosmos;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TEAMModelOS.SDK.DI;

namespace TEAMModelOS.SDK.Models.Service
{
    public static class SurveyService
    {
        public static async Task<string> saveMoreAsync(CosmosClient client, DingDing _dingDing, Survey survey)
        {
            try
            {
                survey.ttl = -1;
                survey.code = "Survey-" + survey.school;
                long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                survey.createTime = now;

                if (string.IsNullOrEmpty(survey.id))
                {
                    survey.id = Guid.NewGuid().ToString();
                    await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(survey, new PartitionKey($"{survey.code}"));
                }
                else
                {
                    await client.GetContainer("TEAMModelOS", "Common").UpsertItemAsync(survey, new PartitionKey($"{survey.code}"));
                }
                return survey.id;
            }
            catch (Exception e)
            {
                await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-SurveyService-saveMore\n{e.Message}{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
                return "";

            }
        }
    }
}
