using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Indotalent.Data.Enums;

namespace Indotalent.Features.Utilities.ProgramManager.Cqrs;

public class GetProgramManagerByIdResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public string? Title { get; set; }
    public string? Summary { get; set; }
    public ProgramManagerStatus Status { get; set; }
    public ProgramManagerPriority Priority { get; set; }
    public string? ProgramManagerResourceId { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
}

public record GetProgramManagerByIdQuery(string Id) : IRequest<GetProgramManagerByIdResponse?>;

public class GetProgramManagerByIdHandler : IRequestHandler<GetProgramManagerByIdQuery, GetProgramManagerByIdResponse?>
{
    private readonly AppDbContext _context;

    public GetProgramManagerByIdHandler(AppDbContext context) => _context = context;

    public async Task<GetProgramManagerByIdResponse?> Handle(GetProgramManagerByIdQuery request, CancellationToken cancellationToken)
    {
        return await _context.ProgramManager
            .AsNoTracking()
            .Where(x => x.Id == request.Id)
            .Select(x => new GetProgramManagerByIdResponse
            {
                Id = x.Id,
                AutoNumber = x.AutoNumber,
                Title = x.Title,
                Summary = x.Summary,
                Status = x.Status,
                Priority = x.Priority,
                ProgramManagerResourceId = x.ProgramManagerResourceId,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy
            })
            .FirstOrDefaultAsync(cancellationToken);
    }
}