using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TEAMModelOS.Service.Models.Core;
using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
using TEAMModelOS.SDK.Module.AzureCosmosDB.Interfaces;

namespace TEAMModelOS.Controllers.Syllabus
{
    [Route("api/[controller]")]
    [ApiController]
    public class SchoolController : Controller
    {

        public IAzureCosmosDBRepository _cosmosrepository;
        public SchoolController(IAzureCosmosDBRepository cosmosDBRepository)
        {
            _cosmosrepository = cosmosDBRepository;
        }

        [HttpPost("SaveOrUpdateAll")]
        public async Task<BaseJosnRPCResponse> SaveOrUpdateAll(JosnRPCRequest<School> request)
        {
            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
            School data = new School();
            if (request.@params.id != null)
            {
               string result = await _cosmosrepository.ReplaceObject(request.@params, request.@params.id, request.@params.schoolCode);
            }
            else
            {
                request.@params.id = Guid.NewGuid().ToString();
                data = await _cosmosrepository.Save(request.@params);
            }
            return builder.Data(data).build();
        }
        [HttpPost("FindSchool")]
        public async Task<BaseJosnRPCResponse> FindSchool(JosnRPCRequest<Dictionary<string, object>> request)
        {
            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
            List<School> sc = await _cosmosrepository.FindByParams<School>(request.@params); 
            //sc.First
            return builder.Data(sc).build();
        }
    }
}
