using Microsoft.EntityFrameworkCore;
using TodoList.Api.Data;
using TodoTask = TodoList.Core.Entities.Task;
using TodoList.Core.Interfaces;

namespace TodoList.Api.Repositories;

/// <summary>
/// 任务仓储实现类，使用 Entity Framework Core 进行数据访问
/// </summary>
public class TaskRepository : ITaskRepository
{
    private readonly TodoDbContext _context;

    /// <summary>
    /// 构造函数，注入数据库上下文
    /// </summary>
    /// <param name="context">数据库上下文</param>
    public TaskRepository(TodoDbContext context)
    {
        _context = context;
    }

    /// <summary>
    /// 获取所有任务
    /// </summary>
    /// <returns>任务列表</returns>
    public async System.Threading.Tasks.Task<List<TodoTask>> GetAllAsync()
    {
        return await _context.Tasks
            .Include(t => t.SubTasks)
            .ToListAsync();
    }

    /// <summary>
    /// 根据ID获取指定任务
    /// </summary>
    /// <param name="id">任务ID</param>
    /// <returns>任务对象，如果不存在则返回null</returns>
    public async System.Threading.Tasks.Task<TodoTask?> GetByIdAsync(int id)
    {
        return await _context.Tasks
            .Include(t => t.SubTasks)
            .FirstOrDefaultAsync(t => t.Id == id);
    }

    /// <summary>
    /// 获取所有未完成的任务
    /// </summary>
    /// <returns>未完成任务列表，按创建时间降序排列</returns>
    public async System.Threading.Tasks.Task<List<TodoTask>> GetActiveTasksAsync()
    {
        return await _context.Tasks
            .Where(t => !t.IsCompleted)
            .OrderByDescending(t => t.CreatedAt)
            .ToListAsync();
    }

    /// <summary>
    /// 获取所有已完成的任务
    /// </summary>
    /// <returns>已完成任务列表，按更新时间降序排列</returns>
    public async System.Threading.Tasks.Task<List<TodoTask>> GetCompletedTasksAsync()
    {
        return await _context.Tasks
            .Where(t => t.IsCompleted)
            .OrderByDescending(t => t.UpdatedAt)
            .ToListAsync();
    }

    /// <summary>
    /// 添加新任务
    /// </summary>
    /// <param name="task">要添加的任务对象</param>
    /// <returns>添加后的任务对象（包含生成的ID）</returns>
    public async System.Threading.Tasks.Task<TodoTask> AddAsync(TodoTask task)
    {
        _context.Tasks.Add(task);
        await _context.SaveChangesAsync();
        return task;
    }

    /// <summary>
    /// 更新任务
    /// </summary>
    /// <param name="task">要更新的任务对象</param>
    /// <returns>更新后的任务对象</returns>
    public async System.Threading.Tasks.Task<TodoTask> UpdateAsync(TodoTask task)
    {
        task.UpdatedAt = DateTime.UtcNow;
        _context.Tasks.Update(task);
        await _context.SaveChangesAsync();
        return task;
    }

    /// <summary>
    /// 删除指定ID的任务
    /// </summary>
    /// <param name="id">任务ID</param>
    public async System.Threading.Tasks.Task DeleteAsync(int id)
    {
        var task = await _context.Tasks.FindAsync(id);
        if (task != null)
        {
            _context.Tasks.Remove(task);
            await _context.SaveChangesAsync();
        }
    }

    /// <summary>
    /// 获取指定父任务的所有子任务
    /// </summary>
    /// <param name="parentTaskId">父任务ID</param>
    /// <returns>子任务列表，按创建时间降序排列</returns>
    public async System.Threading.Tasks.Task<List<TodoTask>> GetSubTasksAsync(int parentTaskId)
    {
        return await _context.Tasks
            .Where(t => t.ParentTaskId == parentTaskId)
            .OrderByDescending(t => t.CreatedAt)
            .ToListAsync();
    }
}
