using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PARR.Core.Repositories.Interfaces.TaskRepositories;
using PARR.DAL.Context;
using PARR.DAL.Repositories.Base;
using PARR.Domain.Constants;
using PARR.Domain.Entities.TaskEntities;
using PARR.Domain.Enums;

namespace PARR.DAL.Repositories.TaskRepositories
{
    internal class TaskRepository : BaseRepository<TaskItem>, ITaskRepository
    {
        public TaskRepository(DataContext dataContext, ILogger<TaskRepository> logger) : base(logger, dataContext) { }

        public async Task<int> TaskCaptureAsync(Guid taskId)
        {
            //var affectedRows = await EntityContext.Database.ExecuteSqlInterpolatedAsync($@"
            //    UPDATE ""{DatabaseSchemas.Task}"".""Tasks""
            //    SET ""{nameof(TaskItem.StatusCode)}""={(int)TaskItemStatusEnum.Processing},
            //        ""{nameof(TaskItem.DateModified)}""={DateTimeOffset.UtcNow}
            //    WHERE ""{nameof(TaskItem.Id)}""={taskId}
            //        AND ""{nameof(TaskItem.StatusCode)}""={(int)TaskItemStatusEnum.Pending}
            //");

            var sql = $@"
                        UPDATE ""{DatabaseSchemas.Task}"".""Tasks""
                        SET ""{nameof(TaskItem.StatusCode)}"" = {(int)TaskItemStatusEnum.Processing},
                            ""{nameof(TaskItem.DateModified)}"" = {{0}}
                        WHERE ""{nameof(TaskItem.Id)}"" = {{1}}
                          AND ""{nameof(TaskItem.StatusCode)}"" = {(int)TaskItemStatusEnum.Pending}";

            var affectedRows = await EntityContext.Database.ExecuteSqlRawAsync(sql, DateTimeOffset.UtcNow, taskId);

            return affectedRows;
        }

    }
}
