using dodohold.core;
using Sayaka.Common;


namespace molilian.core
{
    public partial class JdUnionPlus
    {


        public class SpreadReportInfoDTO
        {
            public int id { get; set; } = 0;
            public int accountId { get; set; } = 0;
            public string accountName { get; set; } = string.Empty;
            public DateTime report_date { get; set; } = DateTime.MinValue;
            public DateTime create_time { get; set; } = DateTime.Now;
            public DateTime last_time { get; set; } = DateTime.MinValue;

            public string accountDate { get; set; } = string.Empty;
            public int clickNum { get; set; } = 0;
            public decimal cosFee { get; set; } = 0;
            public decimal cosPrice { get; set; } = 0;
            public decimal finishCosFee { get; set; } = 0;
            public decimal finishCosPrice { get; set; } = 0;
            public int finishOrderNum { get; set; } = 0;
            public int orderNum { get; set; } = 0;
        }

        public async Task<SpreadReportInfoDTO> queryTodaySpreadEffectData(CancellationToken cancellationToken = default)
        {
            SpreadReportInfoDTO result = null;
            var ts = DateTime.Now.Convert2UnixTimestamp(true);
            string cookies = _account.cookies;

            string useragent = _account.user_agent;
            if (string.IsNullOrEmpty(useragent)) useragent = ProviderFakeUserAgent.RandomComputer;

            var args = new
            {
                funName = "querySpreadEffectData",
                param = new
                {
                    startDate = DateTime.Now.ToString("yyyy-MM-dd"),
                    endDate = DateTime.Now.ToString("yyyy-MM-dd"),
                    mediaId = "",
                    proCont = "",
                    promotionId = "",
                    sourceEmt = "",
                    pageNo = 0,
                    pageSize = 20
                },
            };


            string data = args.Convert2Json().UrlEncode();
            string url = $"https://api.m.jd.com/api?functionId=union_report&appid=unionpc&loginType=3&body={data}";

            WebClientUtility client = new WebClientUtility();
            client.Proxy = _proxy;
#if DEBUG
            client.Proxy = null;
#endif
            client.SetContentType("application/x-www-form-urlencoded");
            client.AddHeaders("X-Referer-Page", "https://union.jd.com/proManager/index");
            client.AddHeaders("X-Rp-Client", "h5_1.0.0");
            client.AddHeaders("Referer", "https://union.jd.com/");
            client.AddHeaders("Origin", "https://union.jd.com");
            client.UserAgent = useragent;
            client.AddHeaders("Cookie", cookies);
            client.Post($"body={data}");
            var response = await client.RequestAsync(url, "POST", cancellationToken);

            string body = response.Body();
            var root = body.Convert2JsonElement();
            int code = root.Read<int>("code");
            string message = root.Read<string>("message");
            if (code == 200 && "success".Equals(message))
            {
                var list = root.ElementRead("result").ElementRead("spreadReportInfoChatList");

                if (list.EnumerateArray().Any())
                {
                    result = list[0].GetRawText().Convert2Object<SpreadReportInfoDTO>();
                }
                return result;
            }
            throw new APIException(body);
        }

    }
}
