using Azure.Cosmos;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using TEAMModelOS.SDK.DI;
using TEAMModelOS.SDK.Extension;
namespace TEAMModelOS.Controllers.XTest
{
    [Route("test")]
    [ApiController]
    public class TestController :ControllerBase
    {
        private readonly AzureCosmosFactory _azureCosmos;
        public TestController(AzureCosmosFactory azureCosmos) {
            _azureCosmos = azureCosmos;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [ProducesDefaultResponseType]
        //[AuthToken(Roles = "teacher")]
        [HttpPost("test-dict-null")]
        public async Task<IActionResult> DictNull(Dictionary<string, object> dict) {
            
            return Ok() ;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [ProducesDefaultResponseType]
        //[AuthToken(Roles = "teacher")]
        [HttpPost("delete")]
        public async Task<IActionResult> Delete(JsonElement request)
        {
            try
            {
             
                if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
                if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
                var client = _azureCosmos.GetCosmosClient();
                List<Task> tasksFiles = new List<Task>();
                var query = $"select c.id from c ";
                if (scope.ToString().Equals("school"))
                {
                    var ids = client.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") });
                    await foreach (var id in ids)
                    {
                        using var json = await JsonDocument.ParseAsync(id.ContentStream);
                        var jsonList = json.RootElement.GetProperty("Documents").EnumerateArray();
                        //批量删除
                        foreach (var obj in jsonList)
                        {
                          //  tasksFiles.Add(client.GetContainer("TEAMModelOS", "School").DeleteItemStreamAsync(obj.GetProperty("id").ToString(), new PartitionKey($"{code}")));
                        }
                    }
                }
                else if (scope.ToString().Equals("teacher")) {
                    var ids = client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") });
                    await foreach (var id in ids)
                    {
                        using var json = await JsonDocument.ParseAsync(id.ContentStream);
                        var jsonList = json.RootElement.GetProperty("Documents").EnumerateArray();
                        //批量删除
                        foreach (var obj in jsonList)
                        {
                            //tasksFiles.Add(client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(obj.GetProperty("id").ToString(), new PartitionKey($"{code}")));
                        }
                    }
                }
                await Task.WhenAll(tasksFiles);
                return Ok(new { code = 1 });
            }
            catch (Exception e)
            {
                return BadRequest(e.StackTrace);
            }
        }
    }
}
