using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System;

namespace SicboSub.Web.Models
{
    public class PlayHistoryReq
    {
        public string msisdn { get; set; } = null!;
        public string? lang { get; set; }
        public string? fromDate { get; set; }
        public string? toDate { get; set; }
        public int pageNumber { get; set; } = 0;
        public int pageSize { get; set; } = 20;
    }

    public class PlayHistoryItem
    {
        public decimal id { get; set; }
        public decimal roomId { get; set; }
        public decimal bettingType { get; set; }
        public decimal bettingValue { get; set; }
        public DateTime bettingTime { get; set; }
        public decimal winCoin { get; set; }
        public decimal backCoin { get; set; }
        public bool isWin { get; set; }
    }

    public class PlayHistoryRes : CommonResponse
    {
        public List<PlayHistoryItem>? data { get; set; }

        public PlayHistoryRes(string dataGet)
        {
            JObject jObject = JObject.Parse(dataGet);
            BuildCommonResponse(dataGet);

            if (jObject != null && jObject["data"] != null)
            {
                 // The API returns data as an object: { "history": [...], "pagination": {...} }
                 // Similar to RankingHistory structure
                var dataObj = jObject["data"];
                if (dataObj["history"] != null)
                {
                    data = dataObj["history"]!.ToObject<List<PlayHistoryItem>>();
                }
            }
        }
    }

    public class PurchaseHistoryReq
    {
        public string msisdn { get; set; } = null!;
        public string? lang { get; set; }
        public string? fromDate { get; set; }
        public string? toDate { get; set; }
        public int pageNumber { get; set; } = 0;
        public int pageSize { get; set; } = 20;
    }

    public class PurchaseHistoryItem
    {
        public decimal id { get; set; }
        public string productName { get; set; } = null!;
        public DateTime registerTime { get; set; }
        public DateTime expireTime { get; set; }
        public decimal serviceId { get; set; }
        public string channel { get; set; } = null!;
        public int status { get; set; }
        public int renew { get; set; }
        public int fee { get; set; } // Fee from Packg table
    }

    public class PurchaseHistoryRes : CommonResponse
    {
        public List<PurchaseHistoryItem>? data { get; set; }

        public PurchaseHistoryRes(string dataGet)
        {
            JObject jObject = JObject.Parse(dataGet);
            BuildCommonResponse(dataGet);

            if (jObject != null && jObject["data"] != null)
            {
                var dataObj = jObject["data"];
                if (dataObj["history"] != null)
                {
                    data = dataObj["history"]!.ToObject<List<PurchaseHistoryItem>>();
                }
            }
        }
    }

    public class ExchangeConfigReq
    {
        public string? lang { get; set; }
        public int pageNumber { get; set; } = 0;
        public int pageSize { get; set; } = 20;
    }

    public class ExchangeConfigItem
    {
        public decimal id { get; set; }
        public string productName { get; set; } = null!;
        public decimal coinCost { get; set; }
        public decimal moneyReceived { get; set; }
        public decimal feeCoin { get; set; }
        public string description { get; set; } = null!;
    }

    public class ExchangeConfigRes : CommonResponse
    {
        public List<ExchangeConfigItem>? data { get; set; }

        public ExchangeConfigRes(string dataGet)
        {
            JObject jObject = JObject.Parse(dataGet);
            BuildCommonResponse(dataGet);

            if (jObject != null && jObject["data"] != null)
            {
                var dataObj = jObject["data"]; 
                // The Package API returns { "exchangeConfigs": [...], "pagination": ... }
                if (dataObj["exchangeConfigs"] != null)
                {
                     data = dataObj["exchangeConfigs"]!.ToObject<List<ExchangeConfigItem>>();
                }
                else 
                {
                     // Fallback 
                     try {
                        data = dataObj.ToObject<List<ExchangeConfigItem>>();
                     } catch {}
                }
            }
        }
    }
    public class ExchangeRequestOtpReq
    {
        public decimal configId { get; set; }
        public string? lang { get; set; }
        public string msisdn { get; set; }
    }

    public class ExchangeVerifyOtpReq
    {
        public string otpCode { get; set; } = null!;
        public string msisdn { get; set; } = null!;
        public string? lang { get; set; }
    }
    public class ExchangeRequestOtpRes : CommonResponse
    {
        public dynamic data { get; set; }

        public ExchangeRequestOtpRes(string dataGet)
        {
             BuildCommonResponse(dataGet);
             JObject jObject = JObject.Parse(dataGet);
             if (jObject != null && jObject["data"] != null)
             {
                 data = jObject["data"];
             }
        }
    }

    public class ExchangeVerifyOtpRes : CommonResponse
    {
        public dynamic data { get; set; }

        public ExchangeVerifyOtpRes(string dataGet)
        {
             BuildCommonResponse(dataGet);
             JObject jObject = JObject.Parse(dataGet);
             if (jObject != null && jObject["data"] != null)
             {
                 data = jObject["data"];
             }
        }
    }

    public class ExchangeHistoryReq
    {
        public string msisdn { get; set; } = null!;
        public string? lang { get; set; }
        public string? fromDate { get; set; }
        public string? toDate { get; set; }
        public int pageNumber { get; set; } = 0;
        public int pageSize { get; set; } = 20;
    }

    public class ExchangeHistoryItem
    {
        public decimal id { get; set; }
        public string msisdn { get; set; } = null!;
        public decimal coinDeducted { get; set; }
        public decimal moneyAdded { get; set; }
        public DateTime exchangeTime { get; set; }
        public byte status { get; set; }
        public string? statusName { get; set; }
    }

    public class ExchangeHistoryRes : CommonResponse
    {
        public List<ExchangeHistoryItem>? data { get; set; }

        public ExchangeHistoryRes(dynamic dataGet)
        {
            string json = "";
            if (dataGet is string) json = dataGet;
            else json = Newtonsoft.Json.JsonConvert.SerializeObject(dataGet);

            BuildCommonResponse(json);
            JObject jObject = JObject.Parse(json);
            if (jObject != null && jObject["data"] != null)
            {
                data = jObject["data"]!.ToObject<List<ExchangeHistoryItem>>();
            }
        }
    }
}
