using System;

namespace JobTrackerApi.Services.JobImport.Plugins;

public sealed class LinkedInPlugin : IJobSitePlugin
{
    public bool CanHandle(string url) => url.Contains("linkedin.com/jobs", StringComparison.OrdinalIgnoreCase);

    public JobImportResult Parse(string html, string url)
    {
        // LinkedIn heavily relies on JS; meta tags are often the best available without a headless browser.
        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,
            Company = meta.TryGetValue("og:site_name", out var sn) ? sn : null,
            Description = HtmlExtract.ToPlainText(desc),
            Parser = "linkedin",
            Source = "linkedin",
            Success = !string.IsNullOrWhiteSpace(title) && !string.IsNullOrWhiteSpace(desc),
        };
    }
}

