using CMSMicroservice.Application.Common.Interfaces;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace CMSMicroservice.Application.SitePageSettingsCQ.Commands.DeletePageImage;

// ── Command ──
public class DeletePageImageCommand : IRequest<Unit>
{
    public long Id { get; set; }
}

// ── Handler ──
public class DeletePageImageCommandHandler : IRequestHandler<DeletePageImageCommand, Unit>
{
    private readonly IApplicationDbContext _context;

    public DeletePageImageCommandHandler(IApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<Unit> Handle(DeletePageImageCommand request, CancellationToken cancellationToken)
    {
        var entity = await _context.SitePageImages
            .FirstOrDefaultAsync(x => x.Id == request.Id && !x.IsDeleted, cancellationToken);

        if (entity != null)
        {
            entity.IsDeleted = true; // Soft delete
            await _context.SaveChangesAsync(cancellationToken);
        }

        return Unit.Value;
    }
}
