using System;

namespace JobTrackerApi.Services.JobImport.Plugins;

public sealed class NavPlugin : IJobSitePlugin
{
    public bool CanHandle(string url)
        => url.Contains("arbeidsplassen.nav.no", StringComparison.OrdinalIgnoreCase) ||
           url.Contains("nav.no", StringComparison.OrdinalIgnoreCase);

    public JobImportResult Parse(string html, string url)
    {
        var meta = HtmlExtract.ReadMeta(html);
        var title = meta.TryGetValue("og:title", out var t) ? t : HtmlExtract.ReadTitle(html);
        var desc = meta.TryGetValue("og:description", out var d) ? d : null;
        var siteName = meta.TryGetValue("og:site_name", out var sn) ? sn : null;

        return new JobImportResult
        {
            SourceUrl = url,
            Title = title,
            Company = siteName, // better than nothing; universal parser often gets this anyway.
            Description = HtmlExtract.ToPlainText(desc),
            Parser = "nav",
            Source = "nav",
            CountryCode = "NO",
            Success = !string.IsNullOrWhiteSpace(title) && !string.IsNullOrWhiteSpace(desc),
        };
    }
}

