using ClaudeDo.Data.Models;
using Microsoft.EntityFrameworkCore;

namespace ClaudeDo.Data.Repositories;

public sealed class SubtaskRepository
{
    private readonly ClaudeDoDbContext _context;

    public SubtaskRepository(ClaudeDoDbContext context) => _context = context;

    public async Task AddAsync(SubtaskEntity entity, CancellationToken ct = default)
    {
        _context.Subtasks.Add(entity);
        await _context.SaveChangesAsync(ct);
    }

    public async Task<List<SubtaskEntity>> GetByTaskIdAsync(string taskId, CancellationToken ct = default)
    {
        return await _context.Subtasks
            .Where(s => s.TaskId == taskId)
            .OrderBy(s => s.OrderNum)
            .ToListAsync(ct);
    }

    public async Task UpdateAsync(SubtaskEntity entity, CancellationToken ct = default)
    {
        _context.Subtasks.Update(entity);
        await _context.SaveChangesAsync(ct);
    }

    public async Task DeleteAsync(string subtaskId, CancellationToken ct = default)
    {
        await _context.Subtasks.Where(s => s.Id == subtaskId).ExecuteDeleteAsync(ct);
    }
}
