using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

using OpenAuth.App;
using OpenAuth.App.Interface;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi
{

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CreateTableController : ControllerBase
    {

        IConfiguration _configuration;
        BasicTableService _app;
        TableRecordApp _tableRecordApp;
        IAuth _auth;
        ILogger<CreateTableController> _logger;

        public CreateTableController(IConfiguration configuration, BasicTableService app, TableRecordApp tableRecordApp, IAuth auth, ILogger<CreateTableController> logger)
        {
            _configuration = configuration;
            _tableRecordApp = tableRecordApp;
            _app = app;
            _auth = auth;
            _logger = logger;
        }
 
        [HttpPost]
        public Response<string> CreateTable([FromBody] CreateTableReq req)
        {
            Response<string> response = new Response<string>();
            try
            {
                response.Result = _app.CreateTable(req);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        
        [HttpPost]
        public Response<string> CreateView([FromBody] CreateViewReq req)
        {
            Response<string> response = new Response<string>();
            try
            {
                response.Result = _app.CreateView(req);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }



     
        [HttpPost]
        public Response<List<TableInfo>> uploadExcel(UploadExcelReq req)
        {
           
            Response<List<TableInfo>> response = new Response<List<TableInfo>>();
            try
            {
                response.Result = _app.UploadExcel(req);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

  
        [HttpPost]
        public Response<string> UpdateTable([FromBody] CreateTableReq req)
        {
            Response<string> response = new Response<string>();
            try
            {
                response.Result = _app.UpdateTable(req);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        [HttpGet]
     
        public  Response<PageInfo<List<TableRecord>>> LoadTableRecordInfo(string keyword, int page = 1, int limit = 15)
        {
            return  _tableRecordApp.LoadTableRecordList(keyword, page, limit);
        }
        [HttpGet]
        public Response<bool> CheckTableExist(string tableName)
        {

            Response<bool> response = new Response<bool>();
            try
            {
                response.Result = _app.CheckTableExist(tableName);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;

        }
    }
}
