using System;

namespace JobTrackerApi.Services.JobImport.Plugins;

public sealed class JobbnorgePlugin : IJobSitePlugin
{
    public bool CanHandle(string url) => url.Contains("jobbnorge.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;

        return new JobImportResult
        {
            SourceUrl = url,
            Title = title,
            Description = HtmlExtract.ToPlainText(desc),
            Parser = "jobbnorge",
            Source = "jobbnorge",
            CountryCode = "NO",
            Success = !string.IsNullOrWhiteSpace(title) && !string.IsNullOrWhiteSpace(desc),
        };
    }
}

