using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Hua.Todo.Application.Data;
using Hua.Todo.Application.Interfaces;
using Hua.Todo.Application.Repositories;
using Hua.Todo.Application.Services;
using Hua.Todo.Core.Interfaces;
using ITaskService = Hua.Todo.Application.Interfaces.ITaskService;

namespace Hua.Todo.Application;

/// <summary>
/// 应用层依赖注入扩展。
/// </summary>
public static class ServiceCollectionExtensions
{
    /// <summary>
    /// 注册应用层服务与 EF Core DbContext。
    /// </summary>
    /// <param name="services">服务集合。</param>
    /// <param name="connectionString">数据库连接字符串。</param>
    /// <returns>服务集合。</returns>
    public static IServiceCollection AddApplicationServices(this IServiceCollection services, string connectionString)
    {
        services.AddDbContext<TodoDbContext>(options => 
            options.UseSqlite(connectionString, b => b.MigrationsAssembly("Hua.Todo.Application")));
        services.AddScoped<ITaskRepository, TaskRepository>();
        services.AddScoped<ITaskService, TaskService>();

        return services;
    }
}
