using Hotline.Orders;
using Hotline.Share.Enums.Snapshot;
using Hotline.Share.Tools;
using Hotline.Snapshot.Contracts;
using Hotline.Snapshot.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Domain.Dependency;
using XF.Domain.Exceptions;

namespace Hotline.Snapshot.Services;

public class SnapshotPointsDomainService : ISnapshotPointsDomainService, IScopeDependency
{
    private readonly IOrderSnapshotRepository _orderSnapshotRepository;
    private readonly ISnapshotPointsRecordRepository _pointsRecordRepository;

    public SnapshotPointsDomainService(IOrderSnapshotRepository orderSnapshotRepository, ISnapshotPointsRecordRepository snapshotPointsRecordRepository)
    {
        _orderSnapshotRepository = orderSnapshotRepository;
        _pointsRecordRepository = snapshotPointsRecordRepository;
    }

    public async Task AddPointsAsync(string orderId, EPointsSource source, ESnapshotSMSStatus? status, int? extraDeductedPoints, string? extraDeductionPointsTypeName = "")
    {
        if (status == null) return;
        var order = await _orderSnapshotRepository.Queryable()
            .LeftJoin<Industry>((snapshot, industry) => snapshot.IndustryId == industry.Id)
            .LeftJoin<Order>((snapshot, industry, order) => order.Id == snapshot.Id)
            .Where((snapshot, industry) => snapshot.Id == orderId)
            .Select((snapshot, industry, order) => new 
            {
                snapshot.Id,
                industry.ReportPoints,
                industry.ArgeePoints, 
                industry.RefusePoints,
                industry.Name,
                snapshot.CreatorId,
                order.HotspotId
            })
            .FirstAsync();
        if (order == null) return;
        if (order.HotspotId != null && order.HotspotId.StartsWith("18") == true) return;
        
        if (order.ReportPoints.HasValue == false)
            throw new UserFriendlyException($"{order.Name} 行业未配置积分");

        var points = new SnapshotPointsRecord
        {
            UserId = order.CreatorId,
            OrderId = orderId,
            Points = order.ReportPoints.Value,
            Source = source,
            Direction = Share.Enums.CallCenter.EPointsDirection.In,
        };
        if (source == EPointsSource.Report)
            points.Points = order.ReportPoints.Value;
        if (source == EPointsSource.Audit && status == ESnapshotSMSStatus.Agree)
            points.Points = order.ArgeePoints ?? 0;
        if (source == EPointsSource.Audit && status == ESnapshotSMSStatus.Refuse)
        {
            points.Direction = Share.Enums.CallCenter.EPointsDirection.Out;
            points.Points = order.RefusePoints ?? 0;
            points.Points *= -1;

            if (extraDeductionPointsTypeName.NotNullOrEmpty() && extraDeductedPoints != null && extraDeductedPoints != 0)
            {
                var ex = points.ToJson().FromJson<SnapshotPointsRecord>();
                ex.Source = EPointsSource.AuditDeduct;
                ex.Points = extraDeductedPoints.Value * -1;
                ex.Remark = extraDeductionPointsTypeName;
                await _pointsRecordRepository.AddAsync(ex);
            }
        }
        await _pointsRecordRepository.AddAsync(points);
    }
}
