using System.Net;
using System.Text.RegularExpressions;
using CvMatcher.Models.Settings;
using Api.Services.Contracts;
using Microsoft.Extensions.Options;

namespace Api.Services;

public sealed class JobTextExtractor : IJobTextExtractor
{
    private readonly HttpClient _http;
    private readonly MatcherSettings _settings;

    public JobTextExtractor(HttpClient http, IOptions<MatcherSettings> options)
    {
        _http = http;
        _settings = options.Value;
        _http.Timeout = TimeSpan.FromSeconds(25);
        _http.DefaultRequestHeaders.UserAgent.ParseAdd("MyAi.ro CV Matcher/1.0");
    }

    public async Task<string> ExtractAsync(string? jobUrl, string? jobDescription, CancellationToken ct)
    {
        var pasted = Normalize(jobDescription ?? string.Empty);
        if (!string.IsNullOrWhiteSpace(pasted)) return Limit(pasted);

        if (string.IsNullOrWhiteSpace(jobUrl)) return string.Empty;
        if (!Uri.TryCreate(jobUrl, UriKind.Absolute, out var uri) || uri.Scheme is not ("http" or "https"))
        {
            throw new InvalidOperationException("Invalid job URL.");
        }

        var html = await _http.GetStringAsync(uri, ct);
        html = Regex.Replace(html, "<script[\\s\\S]*?</script>", " ", RegexOptions.IgnoreCase);
        html = Regex.Replace(html, "<style[\\s\\S]*?</style>", " ", RegexOptions.IgnoreCase);
        html = Regex.Replace(html, "<[^>]+>", " ");
        return Limit(Normalize(WebUtility.HtmlDecode(html)));
    }

    private string Limit(string value)
    {
        var max = Math.Max(4000, _settings.MaxJobTextChars);
        return value.Length <= max ? value : value[..max];
    }

    private static string Normalize(string value)
    {
        if (string.IsNullOrWhiteSpace(value)) return string.Empty;
        return string.Join(' ', value.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries)).Trim();
    }
}
