using System;
using System.Net.Http;
using System.Threading.Tasks;
using QuantEngine.Core.Interfaces;

namespace QuantEngine.Infrastructure.External
{
    public class YahooFinanceClient : IYahooFinanceClient
    {
        private readonly HttpClient _httpClient;
        private const string UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0 Safari/537.36";

        public YahooFinanceClient(HttpClient httpClient)
        {
            _httpClient = httpClient;
        }

        public async Task<string> FetchHistoricalDataAsync(string symbol, string range = "4mo", string interval = "1d")
        {
            var url = $"https://query1.finance.yahoo.com/v8/finance/chart/{symbol}?range={range}&interval={interval}";
            using var request = new HttpRequestMessage(HttpMethod.Get, url);
            request.Headers.Add("User-Agent", UserAgent);

            var response = await _httpClient.SendAsync(request);
            response.EnsureSuccessStatusCode();

            return await response.Content.ReadAsStringAsync();
        }
    }
}
