using Azure;
using Azure.Cosmos;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using TEAMModelOS.SDK.DI;
using TEAMModelOS.SDK.Extension;
using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
using TEAMModelOS.SDK.Models;
using TEAMModelOS.SDK.Models.Cosmos.Common;

namespace TEAMModelFunction
{
    public class TriggerStuActivity
    {

        public static async Task<string> SaveStuActivity(CosmosClient client, DingDing _dingDing, List<StuActivity> stuActivities,List<StuActivity> tmdActivities) {
            try {
                if (stuActivities.IsNotEmpty())
                {
                    foreach (var x in stuActivities)
                    {
                        await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(x, new PartitionKey(x.code));
                    }
                }
                if (tmdActivities.IsNotEmpty())
                {
                    foreach (var x in tmdActivities)
                    {
                        await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(x, new PartitionKey(x.code));
                    }
                }
            } catch (Exception ex) {
                await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS，TriggerStuActivity-SaveStuActivity\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
            }
            return ""; 
        }

        public static async Task<(List<string> tmdids, List<Students> studentss)> GetStuList(CosmosClient client, DingDing _dingDing, List<string> classes, string school) {
            try {
                List<string> tmdids = new List<string>();
                List<Students> studentss = new List<Students>();
                if (!classes.IsNotEmpty()) { return (tmdids, studentss); }
                List<string> sqlList = new List<string>();
                classes.ForEach(x => { sqlList.Add($" '{x}' "); });
                string sql = string.Join(" , ", sqlList);
                List<StuList> schList = new List<StuList>();
                List<Student> students = new List<Student>();
                if (!string.IsNullOrEmpty(school)) {
                    await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
                    requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{school}") }))
                    {
                        schList.Add(item);
                    }


                    await foreach (var item in client.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator<Student>(queryText: $"select value(c) from c where c.classId in ({sql})",
                        requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") }))
                    {
                        students.Add(item);
                    }
                }
                List<StuList> tchLists = new List<StuList>();
                await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
                    requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList") }))
                {
                    tchLists.Add(item);
                }

                schList.ForEach(x => {
                    if (x.students.IsNotEmpty())
                    {
                        studentss.AddRange(x.students);
                    }
                    if (x.tmids.IsNotEmpty())
                    {
                        tmdids.AddRange(x.tmids);
                    }
                });
                tchLists.ForEach(x =>
                {
                    if (x.students.IsNotEmpty()) {
                        studentss.AddRange(x.students);
                    }
                    if (x.tmids.IsNotEmpty())
                    {
                        tmdids.AddRange(x.tmids);
                    }
                });
                students.ForEach(x => {
                    studentss.Add(new Students { id = x.id, code = x.code, schoolId = x.schoolId });
                });
                return (tmdids, studentss);
            }
            catch (Exception ex)
            {
                await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS，TriggerStuActivity-SaveStuActivity\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
            }
            return (null, null);
        }
    }
}
