using MediatR;
using TakeoutSaaS.Application.App.Subscriptions.Commands;
using TakeoutSaaS.Application.App.Subscriptions.Contracts;
using TakeoutSaaS.Domain.Tenants.Repositories;

namespace TakeoutSaaS.Application.App.Subscriptions.Handlers;

/// <summary>
/// 更新订阅命令处理器。
/// </summary>
public sealed class UpdateSubscriptionCommandHandler(ISubscriptionRepository subscriptionRepository)
    : IRequestHandler<UpdateSubscriptionCommand, SubscriptionListDto?>
{
    /// <inheritdoc />
    public async Task<SubscriptionListDto?> Handle(
        UpdateSubscriptionCommand request,
        CancellationToken cancellationToken)
    {
        // 1. 获取订阅（带跟踪）
        var subscription = await subscriptionRepository.GetByIdForUpdateAsync(request.SubscriptionId, cancellationToken);

        // 2. 如果不存在，返回 null
        if (subscription is null)
        {
            return null;
        }

        // 3. 更新字段
        if (request.AutoRenew.HasValue)
        {
            subscription.AutoRenew = request.AutoRenew.Value;
        }

        if (request.Notes is not null)
        {
            subscription.Notes = request.Notes;
        }

        // 4. 更新时间戳
        subscription.UpdatedAt = DateTime.UtcNow;

        // 5. 保存变更
        await subscriptionRepository.SaveChangesAsync(cancellationToken);

        // 6. 获取更新后的订阅信息
        var result = await subscriptionRepository.GetListResultByIdAsync(request.SubscriptionId, cancellationToken);

        // 7. 如果不存在，返回 null
        if (result is null)
        {
            return null;
        }

        // 8. 映射为 DTO 并返回
        return new SubscriptionListDto
        {
            Id = result.Id,
            TenantId = result.TenantId,
            TenantName = result.TenantName,
            TenantCode = result.TenantCode,
            TenantPackageId = result.TenantPackageId,
            PackageName = result.PackageName,
            ScheduledPackageId = result.ScheduledPackageId,
            ScheduledPackageName = result.ScheduledPackageName,
            Status = result.Status,
            EffectiveFrom = result.EffectiveFrom,
            EffectiveTo = result.EffectiveTo,
            NextBillingDate = result.NextBillingDate,
            AutoRenew = result.AutoRenew,
            Notes = result.Notes,
            CreatedAt = result.CreatedAt,
            UpdatedAt = result.UpdatedAt
        };
    }
}
