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

namespace Indotalent.Features.Pipeline.Campaign.Cqrs;

public class CampaignBudgetResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public string? Title { get; set; }
    public decimal? Amount { get; set; }
    public DateTime? BudgetDate { get; set; }
    public BudgetStatus Status { get; set; }
    public string? Description { get; set; }
}

public class CampaignExpenseResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public string? Title { get; set; }
    public decimal? Amount { get; set; }
    public DateTime? ExpenseDate { get; set; }
    public ExpenseStatus Status { get; set; }
    public string? Description { get; set; }
}

public class GetCampaignByIdResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public string? Title { get; set; }
    public string? Description { get; set; }
    public decimal? TargetRevenueAmount { get; set; }
    public DateTime? CampaignDateStart { get; set; }
    public DateTime? CampaignDateFinish { get; set; }
    public CampaignStatus Status { get; set; }
    public string? SalesTeamId { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
    public List<CampaignBudgetResponse> Budgets { get; set; } = new();
    public List<CampaignExpenseResponse> Expenses { get; set; } = new();
}

public record GetCampaignByIdQuery(string Id) : IRequest<GetCampaignByIdResponse?>;

public class GetCampaignByIdHandler : IRequestHandler<GetCampaignByIdQuery, GetCampaignByIdResponse?>
{
    private readonly AppDbContext _context;
    public GetCampaignByIdHandler(AppDbContext context) => _context = context;

    public async Task<GetCampaignByIdResponse?> Handle(GetCampaignByIdQuery request, CancellationToken cancellationToken)
    {
        return await _context.Campaign
            .AsNoTracking()
            .Include(x => x.BudgetList)
            .Include(x => x.ExpenseList)
            .Where(x => x.Id == request.Id)
            .Select(x => new GetCampaignByIdResponse
            {
                Id = x.Id,
                AutoNumber = x.AutoNumber,
                Title = x.Title,
                Description = x.Description,
                TargetRevenueAmount = x.TargetRevenueAmount,
                CampaignDateStart = x.CampaignDateStart,
                CampaignDateFinish = x.CampaignDateFinish,
                Status = x.Status,
                SalesTeamId = x.SalesTeamId,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy,
                Budgets = x.BudgetList.Select(b => new CampaignBudgetResponse
                {
                    Id = b.Id,
                    AutoNumber = b.AutoNumber,
                    Title = b.Title,
                    Amount = b.Amount,
                    BudgetDate = b.BudgetDate,
                    Status = b.Status,
                    Description = b.Description
                }).ToList(),
                Expenses = x.ExpenseList.Select(e => new CampaignExpenseResponse
                {
                    Id = e.Id,
                    AutoNumber = e.AutoNumber,
                    Title = e.Title,
                    Amount = e.Amount,
                    ExpenseDate = e.ExpenseDate,
                    Status = e.Status,
                    Description = e.Description
                }).ToList()
            }).FirstOrDefaultAsync(cancellationToken);
    }
}