using Azure.Cosmos;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using TEAMModelOS.Filter;
using TEAMModelOS.SDK.DI;
using TEAMModelOS.SDK.Extension;
using TEAMModelOS.SDK.Models.Cosmos.Common;
using TEAMModelOS.Services.Common;

namespace TEAMModelOS.Controllers
{
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    //[Authorize(Roles = "IES5")]
    [Route("teacher")]
    [ApiController]
    public class TeacherCommonController:ControllerBase
    {
        private readonly AzureCosmosFactory _azureCosmos;
        private readonly AzureRedisFactory _azureRedis;
        public TeacherCommonController(AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis)
        {

            _azureCosmos = azureCosmos;
            _azureRedis = azureRedis;
        }
        [ProducesDefaultResponseType]
        [HttpPost("tec-activity")]
        [AuthToken(Roles = "student,admin,teacher")]
        public async Task<IActionResult> TecActivity(JsonElement request)
        {
            if (!request.TryGetProperty("type", out JsonElement _type)) return BadRequest();
            var (id, name, pic, school) = HttpContext.GetAuthTokenInfo();
            List<StuActivity> datas = new List<StuActivity>();
            var queryc = $"SELECT value(c) from c where c.school = '{school}' and c.type='{_type}' ";
            await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<StuActivity>(queryText: queryc, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{id}") }))
            {
                datas.Add(item);
                /*using var json = await JsonDocument.ParseAsync(item.ContentStream);
                if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
                {
                    foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
                    {
                        courses.Add(obj.ToObject<object>());
                    }
                }*/
            }
            //(List<StuActivity> datas, string continuationToken) = await ActivityStudentService.FindActivity(request, id, school, _azureCosmos, _azureRedis);
            return Ok(new { datas });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        [ProducesDefaultResponseType]
        [HttpPost("delete-activity")]
        [AuthToken(Roles = "teacher,admin,student")]
        public async Task<IActionResult> DeleteActivity(JsonElement element)
        {
            try
            {
                if (!element.TryGetProperty("id", out JsonElement id)) return BadRequest();
                if (!element.TryGetProperty("code", out JsonElement code)) return BadRequest();
                if (!element.TryGetProperty("role", out JsonElement role)) return BadRequest();
                var client = _azureCosmos.GetCosmosClient();
                if (role.ValueKind.Equals(JsonValueKind.String))
                {
                    if (role.GetString().Equals("teacher") || role.GetString().Equals("admin"))
                    {
                        await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemAsync<StuActivity>($"{id}", new PartitionKey($"{code}"));
                    }
                    else if (role.GetString().Equals("student"))
                    {
                        await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemAsync<StuActivity>($"{id}", new PartitionKey($"{code}"));
                    }
                    else
                    {
                        return Ok(new { status = 500 });
                    }
                }
                else
                {
                    return Ok(new { status = 500 });
                }
                return Ok(new { status = 200 });
            }
            catch (Exception ex)
            {
                return Ok(new { status = 500 });
            }
        }
    }
}
