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

namespace TEAMModelOS.Controllers.XTest
{
    [Route("business")]
    [ApiController]
    public class BusinessController : ControllerBase
    {
        private readonly AzureCosmosFactory _azureCosmos;
        public BusinessController(AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, DingDing dingDing)
        {
            _azureCosmos = azureCosmos;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        //[ProducesDefaultResponseType]
        //[HttpPost("get-schools")]
        //public async Task<IActionResult> GetSchools(JsonElement request) {
        //    var client = _azureCosmos.GetCosmosClient();
        //    string sql = $"select c.id as code,c.name,c.picture from c ";
        //    List<dynamic> dynamics = new List<dynamic>();
        //    await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
        //    {
        //        dynamics.Add(item);
        //    }
        //    return Ok(new { schools= dynamics,status=200});
        //}
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [ProducesDefaultResponseType]
        [HttpPost("get-school-info")]
        public async Task<IActionResult> GetSchoolInfo(JsonElement request)
        {
            
            if (!request.TryGetProperty("id", out JsonElement _code))
            {
                return BadRequest();
            }
            var client = _azureCosmos.GetCosmosClient();
            try
            {
               // string sql = $"select c.id ,c.code ,c.name from c ";
                School school = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{_code}", partitionKey: new PartitionKey("Base"));
                //List<dynamic> knowledges = new List<dynamic>();
                //var sbjs= school.period.SelectMany(x => x.subjects);
                //List<Subject> subjects = new List<Subject>();
                //if (sbjs != null) {
                //    subjects.AddRange(sbjs);
                //}
                //foreach (var sub in subjects) {
                //    await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<dynamic>(requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Knowledge-{school.id}-{sub.id}") }))
                //    {
                //        knowledges.Add(item);
                //    }
                //}
                //List<dynamic> exams = new List<dynamic>();
                //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Exam-{school.id}") }))
                //{
                //    exams.Add(item);
                //}
                //List<dynamic> votes = new List<dynamic>();
                //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Vote-{school.id}") }))
                //{
                //    votes.Add(item);
                //}
                //List<dynamic> surveys = new List<dynamic>();
                //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Survey-{school.id}") }))
                //{
                //    surveys.Add(item);
                //}
                //List<dynamic> courses = new List<dynamic>();
                //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Course-{school.id}") }))
                //{
                //    courses.Add(item);
                //}
                //List<dynamic> classes = new List<dynamic>();
                //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<dynamic>( requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Class-{school.id}") }))
                //{
                //    classes.Add(item);
                //}
                //List<dynamic> stulist = new List<dynamic>();
                //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<dynamic>( requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"StuList-{school.id}") }))
                //{
                //    stulist.Add(item);
                //}
                //List<dynamic> rooms = new List<dynamic>();
                //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<dynamic>( requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Room-{school.id}") }))
                //{
                //    rooms.Add(item);
                //}
                //List<dynamic> volumes = new List<dynamic>();
                //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<dynamic>( requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Volume-{school.id}") }))
                //{
                //    volumes.Add(item);
                //}
                return Ok(new {id= school.id,name=school.name, picture=school.picture });
            }
            catch (CosmosException ex)
            {
                return Ok(new { status = ex.Status });

            }
            catch (Exception _) {
                return Ok(new { status = 500 });
            }
        }
        [ProducesDefaultResponseType]
        [HttpPost("get-student-info")]
        public async Task<IActionResult> GetStudentInfo(JsonElement request) {
            if (!request.TryGetProperty("id", out JsonElement _id))
            {
                return BadRequest();
            }
            if (!request.TryGetProperty("code", out JsonElement _code))
            {
                return BadRequest();
            }
            var client = _azureCosmos.GetCosmosClient();
            try
            {
                Student student = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<Student>($"{_id}", partitionKey: new PartitionKey($"Base-{_code}"));
                
                return Ok(new { id = student.id, name = student.name, picture = student.picture });
            }
            catch (CosmosException ex)
            {
                return Ok(new { status = ex.Status });

            }
            catch (Exception _)
            {
                return Ok(new { status = 500 });
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [ProducesDefaultResponseType]
        [HttpPost("get-teacher-info")]
        public async Task<IActionResult> GetTeacherInfo(JsonElement request)
        {

            if (!request.TryGetProperty("id", out JsonElement _code))
            {
                return BadRequest();
            }
            var client = _azureCosmos.GetCosmosClient();
            try
            {
                //string sql = $"select c.id ,c.code ,c.name from c ";
                Teacher teacher = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Teacher>($"{_code}", partitionKey: new PartitionKey("Base"));
                //List<dynamic> exams = new List<dynamic>();
                //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Exam-{teacher.id}") }))
                //{
                //    exams.Add(item);
                //}
                //List<dynamic> votes = new List<dynamic>();
                //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Vote-{teacher.id}") }))
                //{
                //    votes.Add(item);
                //}
                //List<dynamic> surveys = new List<dynamic>();
                //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Survey-{teacher.id}") }))
                //{
                //    surveys.Add(item);
                //}
                //List<dynamic> courses = new List<dynamic>();
                //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Course-{teacher.id}") }))
                //{
                //    courses.Add(item);
                //}
                //List<dynamic> stulist = new List<dynamic>();
                //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<dynamic>(requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"StuList-{teacher.id}") }))
                //{
                //    stulist.Add(item);
                //}
                //List<dynamic> volumes = new List<dynamic>();
                //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<dynamic>(requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Volume-{teacher.id}") }))
                //{
                //    volumes.Add(item);
                //}
                return Ok(new { id = teacher.id, name = teacher.name, picture = teacher.picture });
            }
            catch (CosmosException ex)
            {
                return Ok(new { status = ex.Status });

            }
            catch (Exception _)
            {
                return Ok(new { status = 500 });
            }
        }
    }
}
