using Brizco.Domain.CommandQueries.Commands;
using Brizco.Domain.CommandQueries.Queries;
using Brizco.Domain.Entities.Shift;

namespace Brizco.Core.EntityServices;

public class ShiftPlanService : IShiftPlanService
{
    private readonly ISender _sender;
    private readonly IActivityService _activityService;
    private readonly IRepositoryWrapper _repositoryWrapper;

    public ShiftPlanService(ISender sender,IActivityService activityService,IRepositoryWrapper repositoryWrapper)
    {
        _sender = sender;
        _activityService = activityService;
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task ChangeShiftPlanTaskStatusAsync(Guid shiftPlanId, bool isChangeToShift, bool isDisable)
    {
        var shiftPlan = await _sender.Send(new GetShiftPlanQuery(shiftPlanId));

    }

    public async Task<bool> CreateAsync(CreateShiftPlanCommand createShiftPlanCommand, CancellationToken cancellationToken)
    {
        var shiftPlan = await _sender.Send(createShiftPlanCommand, cancellationToken);
        await _activityService.CreateActivitiesByShiftPlan(shiftPlan.Id, cancellationToken);
        return true;
    }

    public async Task<bool> UpdateAsync(UpdateShiftPlanCommand updateShiftPlanCommand, CancellationToken cancellationToken)
    {
         await _sender.Send(updateShiftPlanCommand, cancellationToken);
         await _activityService.UpdateActivitiesByShiftPlan(updateShiftPlanCommand.Id, cancellationToken);
         return true;
    }

    public async Task<bool> CompleteShiftPlanAsync(Guid id, CompleteShiftPlanRequestDto requestDtos, CancellationToken cancellationToken)
    {
        var shiftPlan = await _repositoryWrapper.SetRepository<ShiftPlan>()
            .TableNoTracking.FirstOrDefaultAsync(s => s.Id == id, cancellationToken);
        if (shiftPlan == null)
            throw new AppException("Shift plan not found", ApiResultStatusCode.NotFound);

        shiftPlan.CompletePlan(requestDtos.CompleteDescription,requestDtos.CompletePercent);
        _repositoryWrapper.SetRepository<ShiftPlan>().Update(shiftPlan);
        await _repositoryWrapper.SaveChangesAsync(cancellationToken);
        await _activityService.CompleteActivitiesAsync(requestDtos.CompleteActivities, cancellationToken);
        return true;
    }
}