
using CloudBuilder.Core.DatabaseAccessor;
using CloudBuilder.Core.Policy;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Transactions;
using CloudBuilder.Core.Service;
using CloudBuilder.AI.Service;
            using CloudBuilder.Core.DatabaseAccessor.Entity;
            using CloudBuilder.AI.Entity;
            using System;
            using CloudBuilder.AI.Data;
            using CloudBuilder.AI.SearchCriteria;
            
namespace CloudBuilder.Web.Entry.Controllers.Ai
{
[Authorize(AuthorizePolicy.PERMISSION)]
[ApiController]
[Route("[controller]/[action]")]
public class AiBookController : ControllerBase
{
private readonly IApplicationService service;
private readonly IAiBookService aiBookService;

public AiBookController(IApplicationService service, IAiBookService aiBookService)
{
this.service = service;
this.aiBookService= aiBookService;
}

        [PermissionOperate(name: "AiBook", operate: "Find")]
                            [HttpPost]
                            public dynamic FindByKey(string key)       
                    {
                    try
                    {            

                    var result = aiBookService.FindByKey(key);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "AiBook", operate: "Find")]
                            [HttpPost]
                            public dynamic FindByTitle(string title)       
                    {
                    try
                    {            

                    var result = aiBookService.FindByTitle(title);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "AiBook", operate: "Find")]
                            [HttpPost]
                            public dynamic FindBySearchCriteria(AiBookSearchCriteria criteria)       
                    {
                    try
                    {            

                    var result = aiBookService.FindBySearchCriteria(criteria);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "AiBook", operate: "Insert")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic Insert(AiBookData data)       
                    {
                    try
                    {            

                    var result = aiBookService.Insert(data);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "AiBook", operate: "Update")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic Update(AiBookData data)       
                    {
                    try
                    {            

                    var result = aiBookService.Update(data);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "AiBook", operate: "Delete")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic Delete(string key){
                    try
                    {            
                aiBookService.Delete(key);

                    return string.Empty;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }                    
                    }

                            [PermissionOperate(name: "AiBook", operate: "Delete")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic Deletes(string[] keys){
                    try
                    {            
                aiBookService.Deletes(keys);

                    return string.Empty;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }                    
                    }

                    
                    public class ProcessBookCompositionData
                    { 
                     public string key { get; set; }
                         public string encoding { get; set; }
                         public string pattern { get; set; }
                        
                    }
                            [PermissionOperate(name: "AiBook", operate: "Update")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic ProcessBook(ProcessBookCompositionData compositionData)       
                    {
                    try
                    {            

                    var result = aiBookService.ProcessBook(compositionData.key,compositionData.encoding,compositionData.pattern);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "AiBook", operate: "Update")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic ExtractNames(string bookGuid){
                    try
                    {            
                aiBookService.ExtractNames(bookGuid);

                    return string.Empty;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }                    
                    }

                    
                    public class ExtractNamesByChapterGuidCompositionData
                    { 
                     public string bookGuid { get; set; }
                         public string chapterGuid { get; set; }
                        
                    }
                            [PermissionOperate(name: "AiBook", operate: "Update")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic ExtractNamesByChapterGuid(ExtractNamesByChapterGuidCompositionData compositionData){
                    try
                    {            
                aiBookService.ExtractNamesByChapterGuid(compositionData.bookGuid,compositionData.chapterGuid);

                    return string.Empty;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }                    
                    }

                    
}
}