using Tiobon.Core.Common.DB;
using Tiobon.Core.Common.Seed;
using SqlSugar;

namespace Tiobon.Core.Controllers;

[Route("api/[controller]/[action]")]
[ApiController, ApiExplorerSettings(GroupName = Grouping.GroupName_Assistant)]
//[Authorize(Permissions.Name)]
public class DbFirstController : ControllerBase
{
    private readonly SqlSugarScope _sqlSugarClient;
    private readonly IWebHostEnvironment Env;

    /// <summary>
    /// 构造函数
    /// </summary>
    public DbFirstController(ISqlSugarClient sqlSugarClient, IWebHostEnvironment env)
    {
        _sqlSugarClient = sqlSugarClient as SqlSugarScope;
        Env = env;
    }

    /// <summary>
    /// 获取 整体框架 文件(主库)(一般可用第一次生成)
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public ServiceResult<string> GetFrameFiles()
    {
        var data = new ServiceResult<string>() { Success = true, Message = "" };
        data.Data += @"file path is:C:\my-file\}";
        var isMuti = BaseDBConfig.IsMulti;
        if (Env.IsDevelopment())
        {
            data.Data += $"Controller层生成：{FrameSeed.CreateControllers(_sqlSugarClient)} || ";

            BaseDBConfig.ValidConfig.ForEach(m =>
            {
                _sqlSugarClient.ChangeDatabase(m.ConfigId.ToLower());
                data.Data += $"库{m.ConfigId}-Model层生成：{FrameSeed.CreateModels(_sqlSugarClient, m.ConfigId, isMuti)} || ";
                data.Data += $"库{m.ConfigId}-IRepositorys层生成：{FrameSeed.CreateIRepositorys(_sqlSugarClient, m.ConfigId, isMuti)} || ";
                data.Data += $"库{m.ConfigId}-IServices层生成：{FrameSeed.CreateIServices(_sqlSugarClient, m.ConfigId, isMuti)} || ";
                data.Data += $"库{m.ConfigId}-Repository层生成：{FrameSeed.CreateRepository(_sqlSugarClient, m.ConfigId, isMuti)} || ";
                data.Data += $"库{m.ConfigId}-Services层生成：{FrameSeed.CreateServices(_sqlSugarClient, m.ConfigId, isMuti)} || ";
            });

            // 切回主库
            _sqlSugarClient.ChangeDatabase(MainDb.CurrentDbConnId.ToLower());
        }
        else
        {
            data.Success = false;
            data.Message = "当前不处于开发模式，代码生成不可用！";
        }

        return data;
    }

    /// <summary>
    /// 获取仓储层和服务层(需指定表名和数据库)
    /// </summary>
    /// <param name="ConnID">数据库链接名称</param>
    /// <param name="tableNames">需要生成的表名</param>
    /// <returns></returns>
    [HttpPost]
    public ServiceResult<string> GetFrameFilesByTableNames([FromBody] string[] tableNames, [FromQuery] string ConnID = null)
    {
        ConnID = ConnID == null ? MainDb.CurrentDbConnId.ToLower() : ConnID;

        var isMuti = BaseDBConfig.IsMulti;
        var data = new ServiceResult<string>() { Success = true, Message = "" };
        if (Env.IsDevelopment())
        {
            data.Data += $"库{ConnID}-IRepositorys层生成：{FrameSeed.CreateIRepositorys(_sqlSugarClient, ConnID, isMuti, tableNames)} || ";
            data.Data += $"库{ConnID}-IServices层生成：{FrameSeed.CreateIServices(_sqlSugarClient, ConnID, isMuti, tableNames)} || ";
            data.Data += $"库{ConnID}-Repository层生成：{FrameSeed.CreateRepository(_sqlSugarClient, ConnID, isMuti, tableNames)} || ";
            data.Data += $"库{ConnID}-Services层生成：{FrameSeed.CreateServices(_sqlSugarClient, ConnID, isMuti, tableNames)} || ";
        }
        else
        {
            data.Success = false;
            data.Message = "当前不处于开发模式，代码生成不可用！";
        }

        return data;
    }
    /// <summary>
    /// 获取实体(需指定表名和数据库)
    /// </summary>
    /// <param name="ConnID">数据库链接名称</param>
    /// <param name="tableNames">需要生成的表名</param>
    /// <returns></returns>
    [HttpPost]
    public ServiceResult<string> GetFrameFilesByTableNamesForEntity([FromBody] string[] tableNames, [FromQuery] string ConnID = null)
    {
        ConnID = ConnID == null ? MainDb.CurrentDbConnId.ToLower() : ConnID;

        var isMuti = BaseDBConfig.IsMulti;
        var data = new ServiceResult<string>() { Success = true, Message = "" };
        if (Env.IsDevelopment())
        {
            data.Data += $"库{ConnID}-Models层生成：{FrameSeed.CreateModels(_sqlSugarClient, ConnID, isMuti, tableNames)}";
        }
        else
        {
            data.Success = false;
            data.Message = "当前不处于开发模式，代码生成不可用！";
        }
        return data;
    }
    /// <summary>
    /// 获取控制器(需指定表名和数据库)
    /// </summary>
    /// <param name="ConnID">数据库链接名称</param>
    /// <param name="tableNames">需要生成的表名</param>
    /// <returns></returns>
    [HttpPost]
    public ServiceResult<string> GetFrameFilesByTableNamesForController([FromBody] string[] tableNames, [FromQuery] string ConnID = null)
    {
        ConnID = ConnID == null ? MainDb.CurrentDbConnId.ToLower() : ConnID;

        var isMuti = BaseDBConfig.IsMulti;
        var data = new ServiceResult<string>() { Success = true, Message = "" };
        if (Env.IsDevelopment())
        {
            data.Data += $"库{ConnID}-Controllers层生成：{FrameSeed.CreateControllers(_sqlSugarClient, ConnID, isMuti, tableNames)}";
        }
        else
        {
            data.Success = false;
            data.Message = "当前不处于开发模式，代码生成不可用！";
        }
        return data;
    }

    /// <summary>
    /// DbFrist 根据数据库表名 生成整体框架,包含Model层(一般可用第一次生成)
    /// </summary>
    /// <param name="ConnID">数据库链接名称</param>
    /// <param name="tableName">需要生成的表名</param>
    /// <returns></returns>
    [HttpPost]
    public ServiceResult<string> GetAllFrameFilesByTableNames([FromQuery] string tableName, [FromQuery] string ConnID = null)
    {
        string[] tableNames = new string[1];
        tableNames[0] = tableName;
        ConnID = ConnID == null ? MainDb.CurrentDbConnId.ToLower() : ConnID;

        var isMuti = BaseDBConfig.IsMulti;
        var data = new ServiceResult<string>() { Success = true, Message = "" };
        if (Env.IsDevelopment())
        {
            _sqlSugarClient.ChangeDatabase(ConnID.ToLower());
            data.Data += $"Controller层生成：{FrameSeed.CreateControllers(_sqlSugarClient, ConnID, isMuti, tableNames)} || ";
            data.Data += $"库{ConnID}-Model层生成：{FrameSeed.CreateModels(_sqlSugarClient, ConnID, isMuti, tableNames)} || ";
            //data.response += $"库{ConnID}-IRepositorys层生成：{FrameSeed.CreateIRepositorys(_sqlSugarClient, ConnID, isMuti, tableNames)} || ";
            data.Data += $"库{ConnID}-IServices层生成：{FrameSeed.CreateIServices(_sqlSugarClient, ConnID, isMuti, tableNames)} || ";
            //data.response += $"库{ConnID}-Repository层生成：{FrameSeed.CreateRepository(_sqlSugarClient, ConnID, isMuti, tableNames)} || ";
            data.Data += $"库{ConnID}-Services层生成：{FrameSeed.CreateServices(_sqlSugarClient, ConnID, isMuti, tableNames)} || ";
            // 切回主库
            _sqlSugarClient.ChangeDatabase(MainDb.CurrentDbConnId.ToLower());
        }
        else
        {
            data.Success = false;
            data.Message = "当前不处于开发模式，代码生成不可用！";
        }

        return data;
    }


}