diff --git a/JobTrackerApi/Controllers/ProfileCvController.Parsing.cs b/JobTrackerApi/Controllers/ProfileCvController.Parsing.cs new file mode 100644 index 0000000..bf4b431 --- /dev/null +++ b/JobTrackerApi/Controllers/ProfileCvController.Parsing.cs @@ -0,0 +1,1320 @@ +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using System.Text.RegularExpressions; +using JobTrackerApi.Data; +using JobTrackerApi.Services; +using JobTrackerApi.Models; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace JobTrackerApi.Controllers; +public sealed partial class ProfileCvController : ControllerBase +{ + private static void AnnotateStructuredCv(StructuredCvProfile profile, string method, double confidence) + { + var now = DateTimeOffset.UtcNow; + profile.Metadata ??= new StructuredCvMetadata(); + profile.Metadata.Fields ??= new Dictionary(); + + void SetIf(string key, string? value) + { + if (string.IsNullOrWhiteSpace(value)) return; + profile.Metadata.Fields[key] = new StructuredCvFieldMetadata + { + Confidence = confidence, + Method = method, + SourceSnippet = value.Length > 180 ? value[..180] : value, + ReviewState = "suggested", + LastUpdatedAtUtc = now, + }; + } + + SetIf("contact.fullName", profile.Contact.FullName); + SetIf("contact.headline", profile.Contact.Headline); + SetIf("contact.email", profile.Contact.Email); + SetIf("contact.phone", profile.Contact.Phone); + SetIf("contact.location", profile.Contact.Location); + SetIf("contact.website", profile.Contact.Website); + SetIf("contact.linkedIn", profile.Contact.LinkedIn); + SetIf("summary", profile.Summary.FirstOrDefault()); + SetIf("skills", profile.Skills.FirstOrDefault()); + SetIf("languages", profile.Languages.FirstOrDefault()?.Name); + SetIf("interests", profile.Interests.FirstOrDefault()); + SetIf("jobs", profile.Jobs.FirstOrDefault()?.Title ?? profile.Jobs.FirstOrDefault()?.Company); + SetIf("education", profile.Education.FirstOrDefault()?.Qualification ?? profile.Education.FirstOrDefault()?.Institution); + } + + private async Task TryExtractStructuredCvAsync(string text, CancellationToken cancellationToken) + { + var structuredJson = await _aiService.SummarizeSectionAsync( + "Extract this CV into structured JSON. Return only valid JSON with this exact top-level shape: { \"version\": \"1\", \"contact\": { \"fullName\": string|null, \"headline\": string|null, \"email\": string|null, \"phone\": string|null, \"location\": string|null, \"website\": string|null, \"linkedin\": string|null }, \"summary\": string[], \"jobs\": [{ \"title\": string|null, \"company\": string|null, \"location\": string|null, \"start\": string|null, \"end\": string|null, \"isCurrent\": boolean, \"bullets\": string[], \"skills\": string[] }], \"education\": [{ \"qualification\": string|null, \"qualificationLevel\": \"Secondary\"|\"Diploma/Certificate\"|\"Bachelor\"|\"Master\"|\"PhD\"|\"Other\"|null, \"institution\": string|null, \"location\": string|null, \"start\": string|null, \"end\": string|null, \"details\": string[] }], \"certifications\": [{ \"name\": string|null, \"issuer\": string|null, \"location\": string|null, \"date\": string|null, \"details\": string[] }], \"projects\": [{ \"name\": string|null, \"role\": string|null, \"location\": string|null, \"start\": string|null, \"end\": string|null, \"bullets\": string[], \"skills\": string[] }], \"skills\": string[], \"languages\": [{ \"name\": string|null, \"level\": string|null, \"notes\": string|null }], \"interests\": string[], \"otherSections\": [{ \"title\": string|null, \"items\": string[] }] }. Preserve facts only. Do not invent anything. If a field is unknown, use null or an empty array. Keep wording close to the source. Profile location should only be the candidate's current/home location. Education location must be the institution location. Work location must be employer/job location. Never place skill lists such as Python or Ruby into location fields. Preserve the original qualification text in education. Set qualificationLevel to the normalized enum when you can infer it, otherwise null. Put unmatched content in otherSections.", + text, + 3200, + 900); + + if (string.IsNullOrWhiteSpace(structuredJson)) return null; + var extracted = ExtractJsonObject(structuredJson); + if (string.IsNullOrWhiteSpace(extracted)) return null; + + var parsed = StructuredCvProfileJson.Deserialize(extracted); + if (!IsMeaningfullyStructured(parsed)) return null; + + AnnotateStructuredCv(parsed, "llm", 0.82); + return parsed; + } + + private static bool IsMeaningfullyStructured(StructuredCvProfile profile) + { + return !string.IsNullOrWhiteSpace(profile.Contact.FullName) + || profile.Summary.Count > 0 + || profile.Jobs.Count > 0 + || profile.Education.Count > 0 + || profile.Skills.Count > 0 + || profile.Languages.Count > 0 + || profile.Interests.Count > 0 + || profile.OtherSections.Count > 0; + } + + private static string? ExtractJsonObject(string raw) + { + var trimmed = raw.Trim(); + if (trimmed.StartsWith("```", StringComparison.Ordinal)) + { + trimmed = Regex.Replace(trimmed, "^```(?:json)?\\s*|\\s*```$", string.Empty, RegexOptions.IgnoreCase); + } + + var start = trimmed.IndexOf('{'); + var end = trimmed.LastIndexOf('}'); + if (start < 0 || end <= start) return null; + return trimmed[start..(end + 1)]; + } + + private static string? GuessFullName(string source) + { + var normalized = source.Replace("\r\n", "\n"); + foreach (var line in normalized.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Take(6)) + { + var cleaned = line.Trim().TrimStart('#').Trim(); + cleaned = Regex.Replace(cleaned, @"(?<=[a-z])(?=[A-Z])", " "); + if (cleaned.Length < 4 || cleaned.Length > 80) continue; + if (cleaned.Contains('@') || Regex.IsMatch(cleaned, @"\d")) continue; + + var nameMatch = Regex.Match(cleaned, @"^(?[A-Z][A-Za-z'`.-]+(?:\s+[A-Z][A-Za-z'`.-]+){1,3})(?:\s+(?:Real Estate Agent|Store Manager|Web Developer|Developer|Engineer|Consultant|Specialist|Analyst).*)?$", RegexOptions.IgnoreCase); + if (nameMatch.Success) + { + return nameMatch.Groups["name"].Value.Trim(); + } + + if (!Regex.IsMatch(cleaned, @"^[A-Z][A-Za-z'`.-]+(?:\s+[A-Z][A-Za-z'`.-]+){1,4}$")) continue; + return cleaned; + } + + return null; + } + + private static string? GuessFullNameFromEmail(string? email) + { + if (string.IsNullOrWhiteSpace(email) || !email.Contains('@')) return null; + var localPart = email[..email.IndexOf('@')].Trim(); + if (string.IsNullOrWhiteSpace(localPart)) return null; + var parts = Regex.Split(localPart, @"[._-]+") + .Select(part => part.Trim()) + .Where(part => part.Length > 0) + .Select(part => char.ToUpperInvariant(part[0]) + part[1..].ToLowerInvariant()) + .ToList(); + return parts.Count >= 2 ? string.Join(" ", parts) : null; + } + + private static string NormalizeTextForStructuredParsing(string source) + { + if (string.IsNullOrWhiteSpace(source)) return string.Empty; + + var text = source.Replace("\r\n", "\n").Trim(); + if (!LooksLikeFlattenedCvExtraction(text)) return text; + + text = Regex.Replace(text, @"\b([A-Z](?:\s+[A-Z]){2,})\b", match => + { + var collapsed = Regex.Replace(match.Value, @"\s+", string.Empty); + foreach (var alias in SectionAliases) + { + var aliasLettersOnly = Regex.Replace(alias.Key, @"[^A-Za-z]", string.Empty); + if (collapsed.Equals(aliasLettersOnly, StringComparison.OrdinalIgnoreCase)) + { + return $"\n\n## {alias.Value}\n"; + } + } + + return match.Value; + }); + + foreach (var alias in SectionAliases.OrderByDescending(pair => pair.Key.Length)) + { + text = Regex.Replace( + text, + $@"(? section.Name == "Contact"); + if (!string.IsNullOrWhiteSpace(contactSection.Content)) + { + var contactFallback = StructuredCvProfileJson.FromSections(new[] { new StructuredCvSection { Name = "Contact", Content = contactSection.Content } }); + profile.Contact.Location = PreferDetectedLocation(contactSection.Content, contactFallback.Contact.Location, profile.Contact.FullName); + profile.Contact.Headline ??= CleanHeadline(contactFallback.Contact.Headline, profile.Contact.FullName); + } + else + { + profile.Contact.Location = PreferDetectedLocation(rawSource, NullIfWhitespace(Regex.Match(rawSource, @"\b[A-Z][a-z]+(?:[\s-][A-Z][a-z]+)*(?:,\s*[A-Z][a-z]+(?:\s+[A-Z][a-z]+)*){1,2}\b").Value), profile.Contact.FullName); + } + + if (string.IsNullOrWhiteSpace(profile.Contact.Location)) + { + var firstTenLines = normalized.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Take(10).ToList(); + profile.Contact.Location = firstTenLines.FirstOrDefault(line => + !line.Contains('@') + && !Regex.IsMatch(line, @"https?://|www\.", RegexOptions.IgnoreCase) + && Regex.IsMatch(line, @"^[A-Z][A-Za-z.' -]+(?:,\s*[A-Z][A-Za-z.' -]+)?$") + && !line.Contains("Skills", StringComparison.OrdinalIgnoreCase) + && !line.Contains("Summary", StringComparison.OrdinalIgnoreCase) + && !line.Contains("Developer", StringComparison.OrdinalIgnoreCase) + && !line.Contains("Agent", StringComparison.OrdinalIgnoreCase) + && !string.Equals(line, profile.Contact.FullName, StringComparison.OrdinalIgnoreCase)); + } + + if (!string.IsNullOrWhiteSpace(profile.Contact.Location)) + { + profile.Contact.Location = Regex.Replace(profile.Contact.Location, @"\bSkills\b.*$", string.Empty, RegexOptions.IgnoreCase).Trim(' ', ','); + } + + var summarySection = sections.FirstOrDefault(section => section.Name == "Professional Summary"); + var flattenedSummary = Regex.Match( + rawSource, + @"(?:A\s+B\s+O\s+U\s+T\s+M\s+E|P\s+R\s+O\s+F\s+I\s+L\s+E|S\s+U\s+M\s+M\s+A\s+R\s+Y)\s*(?.*?)(?=(?:I\s+N\s+T\s+E\s+R\s+E\s+S\s+T\s+S|E\s+X\s+P\s+E\s+R\s+I\s+E\s+N\s+C\s+E|E\s+D\s+U\s+C\s+A\s+T\s+I\s+O\s+N|C\s+O\s+N\s+T\s+A\s+C\s+T|$))", + RegexOptions.IgnoreCase | RegexOptions.Singleline); + if (flattenedSummary.Success) + { + profile.Summary = SplitSentences(flattenedSummary.Groups["body"].Value, 5) + .Where(item => !Regex.IsMatch(item, @"^:?\s*https?://", RegexOptions.IgnoreCase)) + .ToList(); + } + else if (!string.IsNullOrWhiteSpace(summarySection.Content)) + { + profile.Summary = SplitSentences(summarySection.Content, 5) + .Where(item => !Regex.IsMatch(item, @"^:?\s*https?://", RegexOptions.IgnoreCase)) + .ToList(); + } + + var interestsSection = sections.FirstOrDefault(section => section.Name == "Interests"); + if (!string.IsNullOrWhiteSpace(interestsSection.Content)) + { + profile.Interests = SplitListLike(interestsSection.Content); + } + else + { + var flattenedInterests = Regex.Match( + rawSource, + @"I\s+N\s+T\s+E\s+R\s+E\s+S\s+T\s+S\s*(?.*?)(?=(?:E\s+X\s+P\s+E\s+R\s+I\s+E\s+N\s+C\s+E|C\s+O\s+N\s+T\s+A\s+C\s+T|E\s+D\s+U\s+C\s+A\s+T\s+I\s+O\s+N|$))", + RegexOptions.IgnoreCase | RegexOptions.Singleline); + if (flattenedInterests.Success) + { + profile.Interests = SplitSentences(flattenedInterests.Groups["body"].Value, 4); + } + } + + var languagesSection = sections.FirstOrDefault(section => section.Name == "Languages"); + if (!string.IsNullOrWhiteSpace(languagesSection.Content)) + { + profile.Languages = ParseLanguagesHeuristically(languagesSection.Content); + } + else + { + profile.Languages = ParseLanguagesHeuristically(rawSource); + } + + var skills = new HashSet(StringComparer.OrdinalIgnoreCase); + foreach (var skill in ExtractSkillsHeuristically(rawSource)) + { + skills.Add(skill); + } + profile.Skills = skills.ToList(); + + var educationSection = sections.FirstOrDefault(section => section.Name == "Education"); + if (!string.IsNullOrWhiteSpace(educationSection.Content)) + { + profile.Education = ParseEducationHeuristically(educationSection.Content); + } + + var certificationsSection = sections.FirstOrDefault(section => section.Name == "Certifications"); + if (!string.IsNullOrWhiteSpace(certificationsSection.Content)) + { + profile.Certifications = StructuredCvProfileJson.FromSections(new[] { new StructuredCvSection { Name = "Certifications", Content = certificationsSection.Content } }).Certifications; + } + + var projectsSection = sections.FirstOrDefault(section => section.Name == "Projects"); + if (!string.IsNullOrWhiteSpace(projectsSection.Content)) + { + profile.Projects = StructuredCvProfileJson.FromSections(new[] { new StructuredCvSection { Name = "Projects", Content = projectsSection.Content } }).Projects; + } + + var experienceSection = sections.FirstOrDefault(section => section.Name == "Work Experience"); + if (!string.IsNullOrWhiteSpace(experienceSection.Content)) + { + profile.Jobs = ParseJobsHeuristically(experienceSection.Content); + } + else if (profile.Jobs.Count == 0) + { + profile.Jobs = ParseJobsHeuristically(normalized); + } + + if (profile.OtherSections.Count == 0 && sections.Any(section => section.Name == "General")) + { + var general = sections.First(section => section.Name == "General"); + if (!string.IsNullOrWhiteSpace(general.Content) && profile.Summary.Count == 0) + { + profile.Summary = SplitSentences(general.Content, 3); + } + } + + return StructuredCvProfileJson.Normalize(profile); + } + + private static List SplitSentences(string content, int limit) + { + return Regex.Split(content.Replace("\r\n", " "), @"(?<=[.!?])\s+") + .Select(value => value.Trim()) + .Where(value => value.Length > 20) + .Take(limit) + .ToList(); + } + + private static readonly string[] ConservativeSkillHints = + { + "C#", ".NET", "ASP.NET", "SQL", "JavaScript", "TypeScript", "Python", "Ruby on Rails", "Ruby", "React", "Azure", "Azure DevOps", "GitHub", "CI/CD", "HTML5", "CSS", "MySQL", "PHP OOP", "Project management", "Revenue generation", "Business development", "Effective marketing", "Organisational capacity", "Operability and commitment", "Attention to Detail", "Property Valuation", "Retail Market Analysis", "Client Relationship Management", "Digital Marketing" + }; + + private static List SplitListLike(string content) + { + return content + .Replace("\r\n", "\n") + .Split(new[] { '\n', ',', ';', '•', '●' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .SelectMany(item => item.Contains(" ", StringComparison.Ordinal) ? Regex.Split(item, @"\s{2,}") : new[] { item }) + .Select(item => item.Trim().TrimStart('-', '•', '*', ' ')) + .Where(item => item.Length > 1) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToList(); + } + + private static IEnumerable ExtractConservativeSkills(string content) + { + foreach (var skill in ConservativeSkillHints) + { + if (Regex.IsMatch(content, $@"(? ExtractSkillsFromBullets(IEnumerable bullets) + { + return ExtractConservativeSkills(string.Join("\n", bullets)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToList(); + } + + private static IEnumerable ExtractSkillsHeuristically(string content) + { + var yielded = new HashSet(StringComparer.OrdinalIgnoreCase); + + foreach (var skill in ExtractConservativeSkills(content)) + { + if (yielded.Add(skill)) yield return skill; + } + + var highlightsMatch = Regex.Match(content, @"(?:Highlights|Core Skills|Skills|Technical Skills|Skill Highlights|Competencies)\s*(?.*?)(?=(?:Experience|Education|Languages|Interests|Projects|Certifications|$))", RegexOptions.IgnoreCase | RegexOptions.Singleline); + if (highlightsMatch.Success) + { + foreach (var item in SplitListLike(highlightsMatch.Groups["body"].Value)) + { + var trimmed = item.Trim(); + if (trimmed.Length >= 3 && trimmed.Length <= 80 && trimmed.Count(char.IsLetter) >= 3) + { + if (yielded.Add(trimmed)) yield return trimmed; + } + } + } + } + + private static string? NormalizeDetectedPhone(string? value) + { + var trimmed = NullIfWhitespace(value); + if (trimmed is null) return null; + + var digits = trimmed.Count(char.IsDigit); + if (digits < 7) return null; + + var looksLikeRawCoordinates = trimmed.Contains(" -") && digits > 18 && !trimmed.Contains('+') && !trimmed.Contains('('); + if (looksLikeRawCoordinates) return null; + + return trimmed; + } + + private static string? NormalizeDetectedWebsite(string? value, string? email) + { + var trimmed = NullIfWhitespace(value); + if (trimmed is null) return null; + if (!trimmed.Contains('.', StringComparison.Ordinal)) return null; + if (trimmed.Contains('@')) return null; + if (trimmed.Equals("gmail.com", StringComparison.OrdinalIgnoreCase)) return null; + + var candidate = trimmed.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? trimmed : $"https://{trimmed}"; + if (!Uri.TryCreate(candidate, UriKind.Absolute, out var uri)) return null; + if (string.IsNullOrWhiteSpace(uri.Host) || !uri.Host.Contains('.', StringComparison.Ordinal)) return null; + + return trimmed.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? trimmed : uri.Host; + } + + private static string? ExtractPreferredWebsite(string rawSource, string? email) + { + foreach (Match match in Regex.Matches(rawSource, @"\b(?:https?://)?(?:www\.)?[A-Z0-9.-]+\.[A-Z]{2,}(?:/[A-Z0-9._~:/?#\[\]@!$&'()*+,;=-]*)?", RegexOptions.IgnoreCase)) + { + var candidate = NormalizeDetectedWebsite(match.Value, email); + if (candidate is null) continue; + if (candidate.Contains("linkedin.com", StringComparison.OrdinalIgnoreCase)) continue; + return candidate; + } + + return null; + } + + private static string? PreferDetectedLocation(string source, string? fallback, string? fullName = null) + { + var normalizedFallback = NullIfWhitespace(fallback); + if (normalizedFallback is not null) + { + normalizedFallback = Regex.Replace(normalizedFallback, @",?\s*(Hobbies|Education)\b.*$", string.Empty, RegexOptions.IgnoreCase).Trim(' ', ','); + } + + if (IsPlausibleLocationValue(normalizedFallback, fullName)) + { + return normalizedFallback; + } + + var lines = source.Replace("\r\n", "\n").Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + foreach (var rawLine in lines.Take(10)) + { + var line = Regex.Replace(rawLine, @",?\s*(Hobbies|Education)\b.*$", string.Empty, RegexOptions.IgnoreCase).Trim(' ', ','); + if (!IsPlausibleLocationValue(line, fullName)) continue; + return line; + } + + return IsPlausibleLocationValue(normalizedFallback, fullName) ? normalizedFallback : null; + } + + private static bool IsPlausibleLocationValue(string? value, string? fullName) + { + var candidate = NullIfWhitespace(value); + if (candidate is null) return false; + if (LooksLikeRoleOrHeadline(candidate)) return false; + if (!string.IsNullOrWhiteSpace(fullName)) + { + if (candidate.Equals(fullName, StringComparison.OrdinalIgnoreCase)) return false; + if (candidate.StartsWith(fullName + " ", StringComparison.OrdinalIgnoreCase)) return false; + } + + if (candidate.Contains("Education", StringComparison.OrdinalIgnoreCase) + || candidate.Contains("Hobbies", StringComparison.OrdinalIgnoreCase) + || candidate.Contains("Skills", StringComparison.OrdinalIgnoreCase) + || candidate.Contains("Summary", StringComparison.OrdinalIgnoreCase)) return false; + if (candidate.Contains('@') || Regex.IsMatch(candidate, @"https?://|www\.", RegexOptions.IgnoreCase)) return false; + if (candidate.Count(char.IsDigit) >= 5) return false; + if (Regex.IsMatch(candidate, @"^\d+\s+.+")) return true; + + var normalized = Regex.Replace(candidate, @"\s+", " ").Trim(' ', ','); + if (normalized.Length > 80) return false; + + if (Regex.IsMatch(normalized, @"^[A-Z][A-Za-z.' -]+,\s*[A-Z][A-Za-z.' -]+(?:,\s*[A-Z][A-Za-z.' -]+)?$")) return true; + if (Regex.IsMatch(normalized, @"^[A-Z][A-Za-z.' -]+(?:\s+[A-Z][A-Za-z.' -]+){0,2}$") && !LooksLikeRoleOrHeadline(normalized)) return true; + + return false; + } + + private static bool LooksLikeRoleOrHeadline(string value) + { + return Regex.IsMatch(value, @"\b(real estate agent|developer|engineer|manager|consultant|specialist|analyst|designer|technician|administrator|architect|director|coordinator|assistant|lead|owner|founder|recruiter|teacher|writer|producer|officer|supervisor|sales)\b", RegexOptions.IgnoreCase); + } + + private static bool LooksLikePersonName(string value) + { + return Regex.IsMatch(value, @"^[A-Z][A-Za-z'`.-]+(?:\s+[A-Z][A-Za-z'`.-]+){1,3}$") + && !LooksLikeRoleOrHeadline(value); + } + + private static bool ArePlausibleJobs(List? jobs, string? fullName) + { + if (jobs is null || jobs.Count == 0) return false; + return jobs.Any(job => IsPlausibleJob(job, fullName)); + } + + private static int ScoreJobs(List? jobs, string? fullName) + { + if (jobs is null || jobs.Count == 0) return 0; + var first = jobs[0]; + var score = 0; + if (IsPlausibleJob(first, fullName)) score += 5; + if (!string.IsNullOrWhiteSpace(first.Title) && LooksLikeRoleOrHeadline(first.Title)) score += 4; + if (!string.IsNullOrWhiteSpace(first.Company)) score += 2; + if (!string.IsNullOrWhiteSpace(first.Start) || !string.IsNullOrWhiteSpace(first.End)) score += 2; + if (first.Bullets.Count > 0) score += 2; + score += Math.Min(jobs.Count, 3); + return score; + } + + private static bool IsPlausibleJob(StructuredCvJob? job, string? fullName) + { + if (job is null) return false; + var title = NullIfWhitespace(job.Title); + var company = NullIfWhitespace(job.Company); + var location = NullIfWhitespace(job.Location); + var hasEvidence = !string.IsNullOrWhiteSpace(company) + || !string.IsNullOrWhiteSpace(location) + || !string.IsNullOrWhiteSpace(job.Start) + || !string.IsNullOrWhiteSpace(job.End) + || job.Bullets.Count > 0; + + if (title is null) return hasEvidence; + if (!string.IsNullOrWhiteSpace(fullName) && title.Equals(fullName, StringComparison.OrdinalIgnoreCase)) return false; + if (LooksLikePersonName(title)) return false; + if (title.Contains('@') || Regex.IsMatch(title, @"https?://|www\.", RegexOptions.IgnoreCase)) return false; + if (Regex.IsMatch(title, @"^(?:\d{2}/\d{4}|\d{4})\s*(?:[-–]|to)\s*(?:\d{2}/\d{4}|\d{4}|Present|Current)$", RegexOptions.IgnoreCase)) return false; + if (!hasEvidence && !LooksLikeRoleOrHeadline(title)) return false; + return true; + } + + private static string? CleanHeadline(string? value, string? fullName) + { + var trimmed = NullIfWhitespace(value); + if (trimmed is null) return null; + if (!string.IsNullOrWhiteSpace(fullName) && trimmed.Equals(fullName, StringComparison.OrdinalIgnoreCase)) return null; + if (trimmed.Contains('@') || trimmed.Count(char.IsDigit) > 3) return null; + return trimmed; + } + + private static List ParseLanguagesHeuristically(string content) + { + var languages = new List(); + var candidates = Regex.Split(content.Replace("\r\n", "\n"), @"[\n,;]+|(?<=[.!?])\s+") + .Select(item => item.Trim()) + .Where(item => item.Length > 1); + + foreach (var candidate in candidates) + { + var level = HumanLanguageCatalog.ExtractLevel(candidate); + if (level is null) continue; + + foreach (var name in HumanLanguageCatalog.ExtractLanguageNames(candidate)) + { + languages.Add(new StructuredCvLanguage { Name = name, Level = level }); + } + } + + return languages + .GroupBy(language => language.Name, StringComparer.OrdinalIgnoreCase) + .Select(group => group.First()) + .ToList(); + } + + private static List ParseEducationHeuristically(string content) + { + var normalized = content.Replace("\r\n", "\n").Trim(); + var blocks = Regex.Split(normalized, @"\n\s*\n|(?=###\s+)|(?=(?:Bachelor|Master|Doctor|Associate|Diploma|Certificate|BSc|BA|MSc|MA|PhD)\b)", RegexOptions.IgnoreCase) + .Select(block => block.Trim()) + .Where(block => block.Length > 0) + .ToList(); + + var items = new List(); + foreach (var block in blocks) + { + var candidate = StructuredCvProfileJson.FromSections(new[] { new StructuredCvSection { Name = "Education", Content = block } }).Education; + if (candidate.Count > 0) + { + items.AddRange(candidate); + continue; + } + + var lines = block.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToList(); + if (lines.Count == 0) continue; + + var dateMatch = Regex.Match(block, @"\b(\d{4})\s*[-–]\s*(\d{4}|Present|Current)\b", RegexOptions.IgnoreCase); + var institutionLine = lines.FirstOrDefault(line => line.StartsWith("+ ", StringComparison.Ordinal))?.TrimStart('+', ' '); + var qualificationLine = lines.FirstOrDefault(line => !line.StartsWith("+ ", StringComparison.Ordinal) && !Regex.IsMatch(line, @"^\d{4}\s*[-–]")); + if (qualificationLine is null && lines.Count > 0) qualificationLine = lines[0]; + + if (qualificationLine is null && institutionLine is null) continue; + items.Add(new StructuredCvEducation + { + Qualification = TitleCasePreservingAcronyms(qualificationLine), + QualificationLevel = InferQualificationLevel(qualificationLine), + Institution = TitleCasePreservingAcronyms(institutionLine), + Start = dateMatch.Success ? dateMatch.Groups[1].Value : null, + End = dateMatch.Success ? dateMatch.Groups[2].Value : null, + Details = lines.Where(line => line.StartsWith("- ", StringComparison.Ordinal)).Select(line => line[2..].Trim()).ToList(), + }); + } + + return items; + } + + private static List ParseJobsHeuristically(string content) + { + var normalized = content.Replace("\r\n", "\n").Trim(); + var structured = StructuredCvProfileJson.FromSections(new[] { new StructuredCvSection { Name = "Work Experience", Content = normalized } }).Jobs; + var earlierRoles = ParseEarlierRoles(normalized); + foreach (var role in earlierRoles) + { + if (!structured.Any(job => string.Equals(job.Title, role.Title, StringComparison.OrdinalIgnoreCase) + && string.Equals(job.Company, role.Company, StringComparison.OrdinalIgnoreCase))) + { + structured.Add(role); + } + } + if (ArePlausibleJobs(structured, null)) + { + return structured; + } + + var simpleLines = normalized.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + var inlineDateIndex = Array.FindIndex(simpleLines, line => Regex.IsMatch(line, @".+\d{2}/\d{4}\s+to\s+\d{2}/\d{4}", RegexOptions.IgnoreCase) || Regex.IsMatch(line, @".+\d{4}\s*(?:[-–]|to)\s*(?:\d{4}|Present|Current)", RegexOptions.IgnoreCase)); + if (inlineDateIndex >= 0) + { + var titleLine = Regex.Replace(simpleLines[inlineDateIndex], @"\s*[-–]?\s*\d{2}/\d{4}\s+to\s+\d{2}/\d{4}.*$", string.Empty, RegexOptions.IgnoreCase); + titleLine = Regex.Replace(titleLine, @"\s*[-–]?\s*\d{4}\s*[-–]\s*(?:\d{4}|Present|Current).*$", string.Empty, RegexOptions.IgnoreCase).Trim(); + var companyOrLocation = inlineDateIndex + 1 < simpleLines.Length ? simpleLines[inlineDateIndex + 1] : null; + var datesMatch = Regex.Match(simpleLines[inlineDateIndex], @"(\d{2}/\d{4}|\d{4})\s*(?:to|[-–])\s*(\d{2}/\d{4}|\d{4}|Present|Current)", RegexOptions.IgnoreCase); + var bullets = simpleLines.Skip(inlineDateIndex + 2).Where(line => line.Length > 12).ToList(); + if (!string.IsNullOrWhiteSpace(titleLine)) + { + return new List + { + new StructuredCvJob + { + Title = titleLine, + Company = companyOrLocation, + Start = datesMatch.Success ? datesMatch.Groups[1].Value : null, + End = datesMatch.Success ? datesMatch.Groups[2].Value : null, + IsCurrent = datesMatch.Success && (string.Equals(datesMatch.Groups[2].Value, "Present", StringComparison.OrdinalIgnoreCase) || string.Equals(datesMatch.Groups[2].Value, "Current", StringComparison.OrdinalIgnoreCase)), + Bullets = bullets, + Skills = ExtractSkillsFromBullets(bullets), + } + }; + } + } + + var dateIndex = Array.FindIndex(simpleLines, line => Regex.IsMatch(line, @"(?:\d{2}/\d{4}|\d{4})\s*(?:[-–]|to)\s*(?:\d{2}/\d{4}|\d{4}|Present|Current)", RegexOptions.IgnoreCase)); + if (dateIndex >= 0) + { + if (dateIndex + 2 < simpleLines.Length && LooksLikeRoleOrHeadline(simpleLines[dateIndex + 1])) + { + var datesLine = simpleLines[dateIndex]; + var titleLine = simpleLines[dateIndex + 1]; + var companyLine = simpleLines[dateIndex + 2]; + var bullets = SplitSentences(string.Join(" ", simpleLines.Skip(dateIndex + 3)), 6); + var parts = Regex.Split(datesLine, @"\s*[-–]\s*"); + return new List + { + new StructuredCvJob + { + Title = titleLine, + Company = companyLine, + Start = parts.FirstOrDefault(), + End = parts.Skip(1).FirstOrDefault(), + IsCurrent = string.Equals(parts.Skip(1).FirstOrDefault(), "Present", StringComparison.OrdinalIgnoreCase) || string.Equals(parts.Skip(1).FirstOrDefault(), "Current", StringComparison.OrdinalIgnoreCase), + Bullets = bullets, + Skills = ExtractSkillsFromBullets(bullets), + } + }; + } + + if (dateIndex >= 2) + { + var titleLine = simpleLines[dateIndex - 2]; + var locationLine = simpleLines[dateIndex - 1]; + var datesLine = simpleLines[dateIndex]; + var bullets = simpleLines.Skip(dateIndex + 1).Where(line => line.Length > 12).ToList(); + var parts = Regex.Split(datesLine, @"\s*[-–]\s*"); + return new List + { + new StructuredCvJob + { + Title = titleLine, + Location = locationLine, + Start = parts.FirstOrDefault(), + End = parts.Skip(1).FirstOrDefault(), + IsCurrent = string.Equals(parts.Skip(1).FirstOrDefault(), "Present", StringComparison.OrdinalIgnoreCase) || string.Equals(parts.Skip(1).FirstOrDefault(), "Current", StringComparison.OrdinalIgnoreCase), + Bullets = bullets, + Skills = ExtractSkillsFromBullets(bullets), + } + }; + } + } + + var pattern = new Regex(@"(?[A-Z][A-Z\s/&-]{3,})\s*\n(?<dates>\d{4}\s*[-–]\s*(?:\d{4}|Present|Current))(?<body>.*?)(?=(?:\n[A-Z][A-Z\s/&-]{3,}\s*\n\d{4}\s*[-–]\s*(?:\d{4}|Present|Current))|\z)", RegexOptions.Singleline); + var jobs = new List<StructuredCvJob>(); + + foreach (Match match in pattern.Matches(normalized)) + { + var body = match.Groups["body"].Value.Trim(); + var employer = NullIfWhitespace(Regex.Match(body, @"\+\s*([^\n]+)").Groups[1].Value); + var dates = Regex.Split(match.Groups["dates"].Value, @"\s*[-–]\s*"); + var bullets = SplitSentences(Regex.Replace(body, @"\+\s*[^\n]+", string.Empty), 6); + + jobs.Add(new StructuredCvJob + { + Title = TitleCasePreservingAcronyms(match.Groups["title"].Value), + Company = employer, + Start = NullIfWhitespace(dates.FirstOrDefault()), + End = NullIfWhitespace(dates.Skip(1).FirstOrDefault()), + IsCurrent = string.Equals(dates.Skip(1).FirstOrDefault(), "present", StringComparison.OrdinalIgnoreCase) || string.Equals(dates.Skip(1).FirstOrDefault(), "current", StringComparison.OrdinalIgnoreCase), + Bullets = bullets, + Skills = ExtractSkillsFromBullets(bullets), + }); + } + + return jobs; + } + + private static List<StructuredCvJob> ParseEarlierRoles(string content) + { + var heading = Regex.Match(content, @"(?im)^\s*(?:[-*]\s*)?Earlier roles(?:\s*\(part[- ]?time\))?\s*:?\s*$"); + if (!heading.Success) return new List<StructuredCvJob>(); + + var roles = new List<StructuredCvJob>(); + foreach (var rawLine in content[(heading.Index + heading.Length)..].Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) + { + if (rawLine.StartsWith('#')) break; + var line = rawLine.Trim().TrimStart('-', '*', '•', ' '); + var dates = Regex.Match(line, @"(?<start>\d{4})\s*[-–—]\s*(?<end>\d{4}|Present|Current)", RegexOptions.IgnoreCase); + if (!dates.Success) continue; + + var identity = Regex.Replace(line, @"\s*[|,(]?\s*\d{4}\s*[-–—]\s*(?:\d{4}|Present|Current)\s*\)?\s*$", string.Empty, RegexOptions.IgnoreCase).Trim(); + var parts = Regex.Split(identity, @"\s+(?:—|–|\||at)\s+", RegexOptions.IgnoreCase); + if (parts.Length != 2 || parts.Any(string.IsNullOrWhiteSpace)) continue; + + roles.Add(new StructuredCvJob + { + Title = parts[0].Trim(), + Company = parts[1].Trim(), + Start = dates.Groups["start"].Value, + End = dates.Groups["end"].Value, + IsCurrent = dates.Groups["end"].Value.Equals("Present", StringComparison.OrdinalIgnoreCase) + || dates.Groups["end"].Value.Equals("Current", StringComparison.OrdinalIgnoreCase), + }); + } + return roles; + } + + private static string? TitleCasePreservingAcronyms(string? value) + { + if (string.IsNullOrWhiteSpace(value)) return null; + + var words = value.Trim() + .Split(' ', StringSplitOptions.RemoveEmptyEntries) + .Select(word => word.Length <= 3 && word.All(char.IsUpper) + ? word + : char.ToUpperInvariant(word[0]) + word[1..].ToLowerInvariant()) + .ToArray(); + + return string.Join(" ", words); + } + + private static string? InferQualificationLevel(string? value) + { + var candidate = value?.Trim(); + if (string.IsNullOrWhiteSpace(candidate)) return null; + if (Regex.IsMatch(candidate, @"\b(phd|doctorate|dphil)\b", RegexOptions.IgnoreCase)) return "PhD"; + if (Regex.IsMatch(candidate, @"\b(master(?:'s)?|msc|m\.sc|ma|m\.a|mba|meng)\b", RegexOptions.IgnoreCase)) return "Master"; + if (Regex.IsMatch(candidate, @"\b(bachelor(?:'s)?|bsc|b\.sc|ba|b\.a|beng|degree)\b", RegexOptions.IgnoreCase)) return "Bachelor"; + if (Regex.IsMatch(candidate, @"\b(diploma|certificate|certification|nvq|btec|level\s*\d+|apprenticeship|associate)\b", RegexOptions.IgnoreCase)) return "Diploma/Certificate"; + if (Regex.IsMatch(candidate, @"\b(gcse|a-?level|secondary|high school)\b", RegexOptions.IgnoreCase)) return "Secondary"; + return "Other"; + } + + private static int CountWords(string? text) + { + if (string.IsNullOrWhiteSpace(text)) return 0; + return text.Trim().Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries).Length; + } + + private static string? NullIfWhitespace(string? value) + { + return string.IsNullOrWhiteSpace(value) ? null : value.Trim(); + } + + private static List<(string Name, string Content)> ParseSections(string source) + { + var lines = source.Replace("\r\n", "\n").Split('\n'); + var sections = new List<(string Name, List<string> Lines)>(); + var currentName = "General"; + var currentLines = new List<string>(); + + void Flush() + { + var content = string.Join("\n", currentLines).Trim(); + if (!string.IsNullOrWhiteSpace(content)) + { + sections.Add((currentName, new List<string>(currentLines))); + } + currentLines.Clear(); + } + + foreach (var raw in lines) + { + var line = raw.Trim(); + var canonicalHeading = CanonicalizeSectionHeading(line); + if (canonicalHeading is not null) + { + Flush(); + currentName = canonicalHeading; + continue; + } + + currentLines.Add(raw); + } + + Flush(); + + if (sections.Count == 0) + { + return new List<(string Name, string Content)> { ("General", source.Trim()) }; + } + + return sections + .Select(section => (section.Name, string.Join("\n", section.Lines).Trim())) + .Where(section => !string.IsNullOrWhiteSpace(section.Item2)) + .ToList(); + } + + private static List<StructuredCvSection> BuildSectionsFromClassifiedBlocks(List<ClassifiedCvBlock> classifiedBlocks) + { + var sectionBuckets = new List<StructuredCvSection>(); + foreach (var block in classifiedBlocks) + { + var existing = sectionBuckets.FirstOrDefault(section => section.Name == block.SectionName); + if (existing is null) + { + sectionBuckets.Add(new StructuredCvSection { Name = block.SectionName, Content = block.Content, WordCount = CountWords(block.Content) }); + } + else + { + existing.Content = $"{existing.Content}\n\n{block.Content}".Trim(); + existing.WordCount = CountWords(existing.Content); + } + } + + return sectionBuckets.Where(section => !string.IsNullOrWhiteSpace(section.Content)).ToList(); + } + + private static StructuredCvProfile BuildStructuredCvFromClassifiedBlocks(List<ClassifiedCvBlock> classifiedBlocks) + { + var profile = new StructuredCvProfile(); + var now = DateTimeOffset.UtcNow; + var summary = new List<string>(); + var skills = new HashSet<string>(StringComparer.OrdinalIgnoreCase); + + foreach (var block in classifiedBlocks) + { + switch (block.SectionName) + { + case "Professional Summary": + foreach (var item in (block.Classification?.Summary is { Count: > 0 } + ? block.Classification.Summary + : SplitClassifierContent(block.Content, 5))) + { + summary.Add(item); + } + ApplyClassifierFieldMetadata(profile, "summary", summary.FirstOrDefault(), block, now); + break; + case "Skills": + foreach (var item in (block.Classification?.Skills is { Count: > 0 } + ? block.Classification.Skills.Where(skill => !string.IsNullOrWhiteSpace(skill)).Select(skill => skill.Trim()) + : SplitClassifierSkills(block.Content))) + { + skills.Add(item); + } + ApplyClassifierFieldMetadata(profile, "skills", skills.FirstOrDefault(), block, now); + break; + case "Work Experience": + var job = BuildJobFromClassifiedBlock(block); + if (job is not null) + { + var index = profile.Jobs.Count; + profile.Jobs.Add(job); + ApplyClassifierFieldMetadata(profile, $"jobs[{index}].title", job.Title, block, now); + ApplyClassifierFieldMetadata(profile, $"jobs[{index}].company", job.Company, block, now); + ApplyClassifierFieldMetadata(profile, $"jobs[{index}].location", job.Location, block, now); + } + break; + case "Education": + var education = BuildEducationFromClassifiedBlock(block); + if (education is not null) + { + var index = profile.Education.Count; + profile.Education.Add(education); + ApplyClassifierFieldMetadata(profile, $"education[{index}].qualification", education.Qualification, block, now); + ApplyClassifierFieldMetadata(profile, $"education[{index}].institution", education.Institution, block, now); + } + break; + default: + if (!string.IsNullOrWhiteSpace(block.Content)) + { + profile.OtherSections.Add(new StructuredCvOtherSection + { + Title = block.SectionName, + Items = SplitClassifierContent(block.Content, 6) + }); + } + break; + } + } + + profile.Summary = summary.Distinct(StringComparer.OrdinalIgnoreCase).ToList(); + profile.Skills = skills.ToList(); + profile.Sections = BuildSectionsFromClassifiedBlocks(classifiedBlocks); + + var averageConfidence = classifiedBlocks + .Select(block => block.Classification?.Confidence) + .Where(value => value.HasValue) + .Select(value => value!.Value) + .DefaultIfEmpty(0.74) + .Average(); + AnnotateStructuredCv(profile, "classifier", averageConfidence); + return StructuredCvProfileJson.Normalize(profile); + } + + private static StructuredCvJob? BuildJobFromClassifiedBlock(ClassifiedCvBlock block) + { + var classification = block.Classification; + if (classification is null) return null; + + var bullets = classification.Bullets is { Count: > 0 } + ? classification.Bullets.Where(bullet => !string.IsNullOrWhiteSpace(bullet)).Select(bullet => bullet.Trim()).ToList() + : SplitClassifierContent(block.OriginalBlock, 6); + + var job = new StructuredCvJob + { + Title = NullIfWhitespace(classification.Title), + Company = NullIfWhitespace(classification.Company), + Location = NullIfWhitespace(classification.Location), + Start = NullIfWhitespace(classification.Start), + End = NullIfWhitespace(classification.End), + IsCurrent = string.Equals(classification.End, "Present", StringComparison.OrdinalIgnoreCase) || string.Equals(classification.End, "Current", StringComparison.OrdinalIgnoreCase), + Bullets = bullets, + Skills = classification.Skills is { Count: > 0 } + ? classification.Skills.Where(skill => !string.IsNullOrWhiteSpace(skill)).Select(skill => skill.Trim()).ToList() + : SplitClassifierSkills(block.OriginalBlock) + }; + + return StructuredCvProfileJson.Normalize(new StructuredCvProfile { Jobs = new List<StructuredCvJob> { job } }).Jobs.FirstOrDefault(); + } + + private static StructuredCvEducation? BuildEducationFromClassifiedBlock(ClassifiedCvBlock block) + { + var classification = block.Classification; + if (classification is null) return null; + + var education = new StructuredCvEducation + { + Qualification = NullIfWhitespace(classification.Title), + Institution = NullIfWhitespace(classification.Company), + Location = NullIfWhitespace(classification.Location), + Start = NullIfWhitespace(classification.Start), + End = NullIfWhitespace(classification.End), + Details = classification.Bullets is { Count: > 0 } + ? classification.Bullets.Where(bullet => !string.IsNullOrWhiteSpace(bullet)).Select(bullet => bullet.Trim()).ToList() + : SplitClassifierContent(block.OriginalBlock, 5) + }; + + return StructuredCvProfileJson.Normalize(new StructuredCvProfile { Education = new List<StructuredCvEducation> { education } }).Education.FirstOrDefault(); + } + + private static List<string> SplitClassifierContent(string content, int limit) + { + return content + .Replace("\r\n", "\n") + .Split(new[] { '\n', '•' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .SelectMany(line => line.Contains(". ", StringComparison.Ordinal) + ? Regex.Split(line, @"(?<=[.!?])\s+") + : new[] { line }) + .Select(item => item.Trim().TrimStart('-', '•', '*', '+', ' ')) + .Where(item => item.Length > 2) + .Take(limit) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToList(); + } + + private static List<string> SplitClassifierSkills(string content) + { + return content + .Replace("\r\n", "\n") + .Split(new[] { '\n', ',', ';', '•' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Select(item => item.Trim().TrimStart('-', '•', '*', '+', ' ')) + .Where(item => item.Length > 1 && item.Length <= 48 && !LooksLikeDateLikeValue(item) && !item.Contains('@')) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToList(); + } + + private static bool LooksLikeDateLikeValue(string value) + { + return Regex.IsMatch(value, @"^(?:\d{4}|(?:Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|August|Sep|Sept|September|Oct|October|Nov|November|Dec|December)\s+\d{4}|Present|Current)(?:\s*[-–]\s*(?:\d{4}|(?:Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|August|Sep|Sept|September|Oct|October|Nov|November|Dec|December)\s+\d{4}|Present|Current))?$", RegexOptions.IgnoreCase); + } + + private static void ApplyClassifierFieldMetadata(StructuredCvProfile profile, string key, string? value, ClassifiedCvBlock block, DateTimeOffset now) + { + if (string.IsNullOrWhiteSpace(value)) return; + + profile.Metadata.Fields[key] = new StructuredCvFieldMetadata + { + Confidence = block.Classification?.Confidence ?? 0.74, + Method = "classifier", + SourceSnippet = block.OriginalBlock.Length > 180 ? block.OriginalBlock[..180] : block.OriginalBlock, + SourceBlockId = $"block-{block.Index}", + ReviewState = string.Equals(block.SectionName, "General", StringComparison.OrdinalIgnoreCase) ? "needs-review" : "suggested", + LastUpdatedAtUtc = now, + }; + } + + private async Task<List<ClassifiedCvBlock>> ClassifyBlocksAsync(string parseSource, CancellationToken cancellationToken) + { + var blocks = Regex.Split(parseSource.Replace("\r\n", "\n"), @"\n\s*\n") + .Select(block => block.Trim()) + .Where(block => block.Length >= 24) + .ToList(); + + if (blocks.Count == 0) return new List<ClassifiedCvBlock>(); + + var results = new List<ClassifiedCvBlock>(); + for (var index = 0; index < blocks.Count; index++) + { + var block = blocks[index]; + var classification = await _cvAiClassifier.ClassifyBlockAsync(block, cancellationToken); + var sectionName = classification?.Section; + if (!string.IsNullOrWhiteSpace(sectionName) && SectionAliases.TryGetValue(sectionName, out var canonical)) + { + sectionName = canonical; + } + + if (string.IsNullOrWhiteSpace(sectionName) || string.Equals(sectionName, "Other", StringComparison.OrdinalIgnoreCase)) + { + sectionName = "General"; + } + + var content = block; + if (string.Equals(sectionName, "Work Experience", StringComparison.OrdinalIgnoreCase) && classification is not null) + { + var lines = new List<string>(); + if (!string.IsNullOrWhiteSpace(classification.Title)) lines.Add($"### {classification.Title.Trim()}"); + var endIsCurrent = string.Equals(classification.End, "Present", StringComparison.OrdinalIgnoreCase) || string.Equals(classification.End, "Current", StringComparison.OrdinalIgnoreCase); + var dateRange = FormatDateRangeForSection(classification.Start, classification.End, endIsCurrent); + var meta = string.Join(" | ", new[] { classification.Company, classification.Location, dateRange }.Where(value => !string.IsNullOrWhiteSpace(value))); + if (!string.IsNullOrWhiteSpace(meta)) lines.Add(meta); + if (classification.Bullets is not null) + { + lines.AddRange(classification.Bullets.Where(bullet => !string.IsNullOrWhiteSpace(bullet)).Select(bullet => $"- {bullet.Trim()}")); + } + if (lines.Count > 0) content = string.Join("\n", lines); + } + else if (string.Equals(sectionName, "Education", StringComparison.OrdinalIgnoreCase) && classification is not null) + { + var lines = new List<string>(); + if (!string.IsNullOrWhiteSpace(classification.Title)) lines.Add($"### {classification.Title.Trim()}"); + var dateRange = FormatDateRangeForSection(classification.Start, classification.End, false); + var meta = string.Join(" | ", new[] { classification.Company, classification.Location, dateRange }.Where(value => !string.IsNullOrWhiteSpace(value))); + if (!string.IsNullOrWhiteSpace(meta)) lines.Add(meta); + if (classification.Bullets is not null) + { + lines.AddRange(classification.Bullets.Where(bullet => !string.IsNullOrWhiteSpace(bullet)).Select(bullet => $"- {bullet.Trim()}")); + } + if (lines.Count > 0) content = string.Join("\n", lines); + } + else if (string.Equals(sectionName, "Skills", StringComparison.OrdinalIgnoreCase)) + { + var items = classification?.Skills is { Count: > 0 } + ? classification.Skills.Where(skill => !string.IsNullOrWhiteSpace(skill)).Select(skill => skill.Trim()).ToList() + : SplitClassifierSkills(block); + if (items.Count > 0) content = string.Join("\n", items); + } + else if (string.Equals(sectionName, "Professional Summary", StringComparison.OrdinalIgnoreCase)) + { + var items = classification?.Summary is { Count: > 0 } + ? classification.Summary.Where(line => !string.IsNullOrWhiteSpace(line)).Select(line => $"- {line.Trim()}") + : classification?.Bullets is { Count: > 0 } + ? classification.Bullets.Where(bullet => !string.IsNullOrWhiteSpace(bullet)).Select(bullet => $"- {bullet.Trim()}") + : Enumerable.Empty<string>(); + var materialized = items.ToList(); + if (materialized.Count > 0) content = string.Join("\n", materialized); + } + + results.Add(new ClassifiedCvBlock(index + 1, block, sectionName, content, classification)); + } + + return results; + } + + private static string? FormatDateRangeForSection(string? start, string? end, bool isCurrent) + { + if (string.IsNullOrWhiteSpace(start) && string.IsNullOrWhiteSpace(end)) return null; + if (string.IsNullOrWhiteSpace(start)) return end; + return $"{start} - {(isCurrent ? "Present" : end ?? "Present")}"; + } + + private async Task<string> MaybeReconstructStructuredCvAsync(string text, CancellationToken cancellationToken) + { + var normalized = text.Trim(); + var forceAiNormalizer = string.Equals(Environment.GetEnvironmentVariable("CV_FORCE_AI_NORMALIZER"), "true", StringComparison.OrdinalIgnoreCase); + if (forceAiNormalizer) + { + var forced = await _cvAiNormalizer.NormalizeAsync(normalized, cancellationToken); + if (!string.IsNullOrWhiteSpace(forced?.NormalizedText)) + { + return forced.NormalizedText.Trim(); + } + } + + var looksFlattened = LooksLikeFlattenedCvExtraction(normalized); + var hasRecoverableSignals = HasRecoverableSectionSignals(normalized); + + if (!looksFlattened && hasRecoverableSignals) + { + return normalized; + } + + var reconstructed = await _aiService.SummarizeSectionAsync( + "Reconstruct this CV text extracted from a PDF into a clean, readable master CV in markdown. Preserve facts only. Recover clear sections such as Contact, Professional Summary, Work Experience, Education, Skills, Languages, and Interests when present. Split contact details onto their own lines, turn noisy all-caps/spaced headings into normal headings, keep dates with the correct roles and employers, and remove layout/OCR artifacts. Do not invent employers, titles, dates, or metrics. Return only the reconstructed CV text.", + normalized, + 2800, + 900); + + var candidate = string.IsNullOrWhiteSpace(reconstructed) ? normalized : reconstructed.Trim(); + if (LooksLikeFlattenedCvExtraction(candidate) || !HasRecoverableSectionSignals(candidate)) + { + var aiNormalized = await _cvAiNormalizer.NormalizeAsync(normalized, cancellationToken); + if (!string.IsNullOrWhiteSpace(aiNormalized?.NormalizedText)) + { + return aiNormalized.NormalizedText.Trim(); + } + } + + return candidate; + } + + private static bool LooksLikeFlattenedCvExtraction(string text) + { + if (string.IsNullOrWhiteSpace(text)) return false; + + var normalized = text.Replace("\r\n", "\n"); + var lineCount = normalized.Split('\n').Count(line => !string.IsNullOrWhiteSpace(line)); + var spacedHeadingCount = Regex.Matches(normalized, @"\b(?:[A-Z]\s){3,}[A-Z]\b").Count; + var knownHeadingHits = SectionAliases.Keys.Count(alias => normalized.Contains(alias, StringComparison.OrdinalIgnoreCase)); + var bulletCount = Regex.Matches(normalized, @"[•●▪◦]").Count; + + return (lineCount <= 6 && normalized.Length >= 500) + || spacedHeadingCount >= 3 + || (knownHeadingHits >= 3 && lineCount <= 12) + || (normalized.Contains(" + ") && bulletCount > 0 && lineCount <= 10); + } + + private static bool LooksLikeNormalizedMarkdownCv(string text) + { + if (string.IsNullOrWhiteSpace(text)) return false; + return Regex.IsMatch(text, @"(?im)^#\s+(Contact|Professional Summary|Work Experience|Education|Skills|Languages|Interests|Projects|Certifications)\s*$"); + } + + private static StructuredCvProfile BuildStructuredCvFromNormalizedMarkdown(string text) + { + text = SeparateGluedDateAndTitle(text); + var sections = ParseSections(text) + .Select(section => new StructuredCvSection + { + Name = section.Name, + Content = section.Content, + WordCount = CountWords(section.Content), + }) + .ToList(); + + var profile = StructuredCvProfileJson.FromSections(sections); + profile.Sections = sections; + var workExperience = sections.FirstOrDefault(section => section.Name == "Work Experience")?.Content; + if (!string.IsNullOrWhiteSpace(workExperience)) + { + profile.Jobs.RemoveAll(job => (job.Title ?? string.Empty).StartsWith("Earlier roles", StringComparison.OrdinalIgnoreCase)); + foreach (var role in ParseEarlierRoles(workExperience)) + { + if (!profile.Jobs.Any(job => string.Equals(job.Title, role.Title, StringComparison.OrdinalIgnoreCase) + && string.Equals(job.Company, role.Company, StringComparison.OrdinalIgnoreCase))) profile.Jobs.Add(role); + } + } + + if (string.IsNullOrWhiteSpace(profile.Contact.FullName)) + { + profile.Contact.FullName = GuessFullName(text) ?? GuessFullNameFromEmail(profile.Contact.Email); + } + + var contactSection = sections.FirstOrDefault(section => section.Name == "Contact"); + profile.Contact.Location = PreferDetectedLocation(contactSection?.Content ?? text, profile.Contact.Location, profile.Contact.FullName); + profile.Summary = CondenseSummary(profile.Summary); + profile.Skills = OrderSkills(profile.Skills); + profile.Interests = CleanInterestItems(profile.Interests); + + foreach (var job in profile.Jobs) + { + job.Bullets = job.Bullets.Where(bullet => !bullet.Contains("Detail not specified", StringComparison.OrdinalIgnoreCase)).ToList(); + } + + foreach (var education in profile.Education) + { + education.Details = education.Details.Where(detail => !detail.Contains("Detail not specified", StringComparison.OrdinalIgnoreCase)).ToList(); + } + + return profile; + } + + private static List<string> CondenseSummary(List<string> summary) + { + if (summary.Count <= 1) return summary; + var joined = string.Join(" ", summary).Trim(); + return string.IsNullOrWhiteSpace(joined) ? new List<string>() : new List<string> { joined }; + } + + private static List<string> OrderSkills(List<string> skills) + { + return skills + .Select(CleanSkillGroupPrefix) + .Where(skill => skill.Length > 0) + .Distinct(StringComparer.OrdinalIgnoreCase) + .OrderBy(skill => skill, StringComparer.OrdinalIgnoreCase) + .ToList(); + } + + private static string CleanSkillGroupPrefix(string skill) + => Regex.Replace(skill.Trim(), @"^(?:Development|DevOps(?:\s*&\s*Infrastructure)?|Infrastructure|Practices|Tools|Technologies|Technical Skills)\s*:\s*", string.Empty, RegexOptions.IgnoreCase).Trim(); + + private static string SeparateGluedDateAndTitle(string text) + => Regex.Replace(text, @"(?<date>\b\d{4}\s*[-–—]\s*(?:\d{4}|Present|Current))(?<title>[\p{L}][^\r\n]*)", "${title}\n${date}", RegexOptions.IgnoreCase); + + private static string RepairKnownMojibake(string text) + => text + .Replace("ø", "ø", StringComparison.Ordinal) + .Replace("Ø", "Ø", StringComparison.Ordinal) + .Replace("æ", "æ", StringComparison.Ordinal) + .Replace("Æ", "Æ", StringComparison.Ordinal) + .Replace("Ã¥", "å", StringComparison.Ordinal) + .Replace("Ã…", "Å", StringComparison.Ordinal) + .Replace("–", "–", StringComparison.Ordinal) + .Replace("—", "—", StringComparison.Ordinal) + .Replace("’", "’", StringComparison.Ordinal) + .Replace("“", "“", StringComparison.Ordinal) + .Replace("”", "”", StringComparison.Ordinal); + + private static List<string> CleanInterestItems(List<string> interests) + { + return interests + .Where(item => !item.Contains("linkedin", StringComparison.OrdinalIgnoreCase) + && !item.Contains("realtor", StringComparison.OrdinalIgnoreCase) + && !Regex.IsMatch(item, @"https?://|www\.", RegexOptions.IgnoreCase)) + .ToList(); + } + + private static string? CanonicalizeSectionHeading(string line) + { + if (string.IsNullOrWhiteSpace(line)) return null; + + var normalized = line.Trim(); + if (normalized.StartsWith("#", StringComparison.Ordinal)) + { + normalized = normalized.TrimStart('#').Trim(); + } + + normalized = normalized.TrimEnd(':').Trim(); + if (normalized.Length == 0 || normalized.Length > 60) return null; + if (normalized.Contains('.') || normalized.Contains(" ")) return null; + + return SectionAliases.TryGetValue(normalized, out var canonical) ? canonical : null; + } + + private static bool HasRecoverableSectionSignals(string text) + { + var sections = ParseSections(text); + return sections.Any(section => !string.Equals(section.Name, "General", StringComparison.OrdinalIgnoreCase)) + || Regex.IsMatch(text, @"(?im)^\s*(Contact|Professional Summary|Summary|Work Experience|Experience|Education|Skills|Languages|Interests)\s*:?") + || Regex.IsMatch(text, @"(?im)^\s*#\s*(Contact|Professional Summary|Summary|Work Experience|Experience|Education|Skills|Languages|Interests)"); + } + + private static async Task<string> ExtractTextAsync(IFormFile file, string extension) + { + if (string.Equals(extension, ".txt", StringComparison.OrdinalIgnoreCase) || string.Equals(extension, ".md", StringComparison.OrdinalIgnoreCase)) + { + using var stream = file.OpenReadStream(); + using var reader = new StreamReader(stream, Encoding.UTF8, detectEncodingFromByteOrderMarks: true); + return (await reader.ReadToEndAsync()).Trim(); + } + + await using var memory = new MemoryStream(); + await file.CopyToAsync(memory); + var bytes = memory.ToArray(); + + if (string.Equals(extension, ".pdf", StringComparison.OrdinalIgnoreCase)) + { + var raw = Encoding.Latin1.GetString(bytes); + var textMatches = Regex.Matches(raw, @"\((.*?)\)Tj", RegexOptions.Singleline) + .Select(match => match.Groups[1].Value) + .Concat(Regex.Matches(raw, @"\[(.*?)\]TJ", RegexOptions.Singleline) + .SelectMany(match => Regex.Matches(match.Groups[1].Value, @"\((.*?)\)", RegexOptions.Singleline).Select(x => x.Groups[1].Value))) + .Where(value => !string.IsNullOrWhiteSpace(value)) + .Select(value => Regex.Unescape(value)) + .ToList(); + + var joined = textMatches.Count > 0 ? string.Join(" ", textMatches) : raw; + var scrubbed = Regex.Replace(joined, @"[\x00-\x08\x0B\x0C\x0E-\x1F]", " "); + return Regex.Replace(scrubbed, @"\s+", " ").Trim(); + } + + if (string.Equals(extension, ".docx", StringComparison.OrdinalIgnoreCase)) + { + using var archive = new System.IO.Compression.ZipArchive(new MemoryStream(bytes), System.IO.Compression.ZipArchiveMode.Read, leaveOpen: false); + var entry = archive.GetEntry("word/document.xml"); + if (entry is null) return string.Empty; + using var entryStream = entry.Open(); + using var reader = new StreamReader(entryStream, Encoding.UTF8); + var xml = await reader.ReadToEndAsync(); + var withoutTags = Regex.Replace(xml, "<[^>]+>", " "); + var decoded = System.Net.WebUtility.HtmlDecode(withoutTags) ?? string.Empty; + return Regex.Replace(decoded, @"\s+", " ").Trim(); + } + + return string.Empty; + } +} diff --git a/JobTrackerApi/Controllers/ProfileCvController.Pipeline.cs b/JobTrackerApi/Controllers/ProfileCvController.Pipeline.cs new file mode 100644 index 0000000..9ab849b --- /dev/null +++ b/JobTrackerApi/Controllers/ProfileCvController.Pipeline.cs @@ -0,0 +1,448 @@ +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using System.Text.RegularExpressions; +using JobTrackerApi.Data; +using JobTrackerApi.Services; +using JobTrackerApi.Models; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace JobTrackerApi.Controllers; +public sealed partial class ProfileCvController : ControllerBase +{ + private static TailoredCvDocument BuildMasterCvDocument(StructuredCvProfile structuredCv, string templateId, string? targetRole, string? fallbackHeadline, string? companyName) + { + var normalized = StructuredCvProfileJson.Normalize(structuredCv); + var customSections = new List<TailoredCvCustomSection>(); + if (normalized.Certifications.Count > 0) + { + customSections.Add(new TailoredCvCustomSection + { + Title = "Certifications", + Items = normalized.Certifications.Select(certification => string.Join(" | ", new[] { certification.Name, certification.Issuer, certification.Location, certification.Date }.Where(value => !string.IsNullOrWhiteSpace(value)))).Where(value => !string.IsNullOrWhiteSpace(value)).ToList(), + }); + } + if (normalized.Projects.Count > 0) + { + customSections.Add(new TailoredCvCustomSection + { + Title = "Projects", + Items = normalized.Projects.Select(project => string.Join(" | ", new[] { project.Name, project.Role, project.Location, FormatDateRangeForSection(project.Start, project.End, false) }.Where(value => !string.IsNullOrWhiteSpace(value)))).Where(value => !string.IsNullOrWhiteSpace(value)).ToList(), + }); + } + if (normalized.Languages.Count > 0) + { + customSections.Add(new TailoredCvCustomSection + { + Title = "Languages", + Items = normalized.Languages.Select(language => string.Join(": ", new[] { language.Name, language.Level }.Where(value => !string.IsNullOrWhiteSpace(value)))).Where(value => !string.IsNullOrWhiteSpace(value)).ToList(), + }); + } + customSections.AddRange(normalized.OtherSections.Select(section => new TailoredCvCustomSection { Title = section.Title, Items = section.Items })); + + return TailoredCvDraftJson.Normalize(new TailoredCvDocument + { + TemplateId = templateId, + Headline = normalized.Contact.Headline ?? targetRole ?? fallbackHeadline ?? companyName, + Summary = normalized.Summary, + SelectedSkills = normalized.Skills, + Experience = normalized.Jobs.Select(job => new TailoredCvExperienceItem + { + Title = job.Title, + Company = job.Company, + Location = job.Location, + Start = job.Start, + End = job.End, + IsCurrent = job.IsCurrent, + Bullets = job.Bullets, + }).ToList(), + Education = normalized.Education.Select(education => new TailoredCvEducationItem + { + Qualification = education.Qualification, + QualificationLevel = education.QualificationLevel, + Institution = education.Institution, + Location = education.Location, + Start = education.Start, + End = education.End, + Details = education.Details, + }).ToList(), + CustomSections = customSections, + RenderOptions = new TailoredCvRenderOptions + { + ShowPhoto = true, + AccentColor = templateId switch + { + "harvard" => "brick", + "auckland" => "emerald", + "edinburgh" => "plum", + "monarch" => "#7c2d12", + "fjord" => "#0f4c5c", + _ => "slate", + }, + SectionOrder = new List<string> { "summary", "skills", "experience", "education", "custom" }, + } + }); + } + + private async Task<StructuredCvProfile> BuildStructuredCvAsync(string text, CancellationToken cancellationToken) + { + if (LooksLikeNormalizedMarkdownCv(text)) + { + var normalized = BuildStructuredCvFromNormalizedMarkdown(text); + AnnotateStructuredCv(normalized, "normalized-markdown", 0.78); + return StructuredCvProfileJson.Normalize(normalized); + } + + var parseSource = NormalizeTextForStructuredParsing(text); + var parsedSections = ParseSections(parseSource) + .Select(section => new StructuredCvSection + { + Name = section.Name, + Content = section.Content, + WordCount = CountWords(section.Content), + }) + .ToList(); + var hasRealSections = parsedSections.Any(section => !string.Equals(section.Name, "General", StringComparison.OrdinalIgnoreCase)); + + List<ClassifiedCvBlock> classifiedBlocks = new(); + List<StructuredCvSection> fallbackSections = parsedSections; + StructuredCvProfile? classifierFallback = null; + + if (!hasRealSections) + { + classifiedBlocks = await ClassifyBlocksAsync(parseSource, cancellationToken); + var hasMeaningfulClassifierStructure = classifiedBlocks.Any(block => !string.Equals(block.SectionName, "General", StringComparison.OrdinalIgnoreCase)); + if (hasMeaningfulClassifierStructure) + { + fallbackSections = BuildSectionsFromClassifiedBlocks(classifiedBlocks); + classifierFallback = BuildStructuredCvFromClassifiedBlocks(classifiedBlocks); + } + } + + var sectionFallback = StructuredCvProfileJson.FromSections(fallbackSections); + AnnotateStructuredCv(sectionFallback, "repair", 0.56); + var heuristicFallback = BuildHeuristicStructuredCv(parseSource, text); + AnnotateStructuredCv(heuristicFallback, "deterministic", 0.68); + heuristicFallback.Sections = new List<StructuredCvSection>(); + var fallback = StructuredCvProfileJson.Merge(heuristicFallback, sectionFallback); + if (classifierFallback is not null) + { + fallback = StructuredCvProfileJson.Merge(classifierFallback, fallback); + } + fallback.Contact.FullName ??= GuessFullName(text) ?? GuessFullNameFromEmail(fallback.Contact.Email); + var extracted = await TryExtractStructuredCvAsync(parseSource, cancellationToken); + var merged = StructuredCvProfileJson.Merge(extracted, fallback); + merged.Contact.FullName ??= GuessFullName(text) ?? GuessFullNameFromEmail(merged.Contact.Email); + + if (!IsPlausibleLocationValue(merged.Contact.Location, merged.Contact.FullName)) + { + merged.Contact.Location = PreferDetectedLocation(text, null, merged.Contact.FullName); + } + + merged.Jobs = merged.Jobs + .Where(job => !LooksLikePersonName(job.Title ?? string.Empty)) + .ToList(); + + var reparsedJobs = ParseJobsHeuristically(text) + .Where(job => !LooksLikePersonName(job.Title ?? string.Empty)) + .ToList(); + var existingFirstTitle = merged.Jobs.FirstOrDefault()?.Title; + var reparsedFirstTitle = reparsedJobs.FirstOrDefault()?.Title; + + if (LooksLikePersonName(existingFirstTitle ?? string.Empty) + && LooksLikeRoleOrHeadline(reparsedFirstTitle ?? string.Empty) + && ArePlausibleJobs(reparsedJobs, merged.Contact.FullName)) + { + merged.Jobs = reparsedJobs; + } + else if (ArePlausibleJobs(merged.Jobs, merged.Contact.FullName)) + { + if (ScoreJobs(reparsedJobs, merged.Contact.FullName) > ScoreJobs(merged.Jobs, merged.Contact.FullName)) + { + merged.Jobs = reparsedJobs; + } + } + else if (ArePlausibleJobs(reparsedJobs, merged.Contact.FullName)) + { + merged.Jobs = reparsedJobs; + } + + return StructuredCvProfileJson.Normalize(merged); + } + + private async Task<CvUploadArtifact> SaveUploadArtifactAsync(ApplicationUser user, IFormFile file, CancellationToken cancellationToken) + { + var extension = Path.GetExtension(file.FileName ?? string.Empty); + var userRoot = Path.Combine(_paths.CvArtifactsRoot, user.Id); + Directory.CreateDirectory(userRoot); + + var storedFileName = $"{DateTimeOffset.UtcNow:yyyyMMddHHmmss}-{Guid.NewGuid():N}{extension}"; + var storagePath = Path.Combine(userRoot, storedFileName); + + await using (var target = System.IO.File.Create(storagePath)) + await using (var source = file.OpenReadStream()) + { + await source.CopyToAsync(target, cancellationToken); + } + + await using var hashStream = System.IO.File.OpenRead(storagePath); + var shaBytes = await SHA256.HashDataAsync(hashStream, cancellationToken); + + return new CvUploadArtifact + { + OwnerUserId = user.Id, + OriginalFileName = file.FileName ?? storedFileName, + StoredFileName = storedFileName, + MimeType = file.ContentType ?? "application/octet-stream", + ByteSize = file.Length, + Sha256 = Convert.ToHexString(shaBytes), + StoragePath = storagePath, + UploadedAtUtc = DateTimeOffset.UtcNow, + }; + } + + private async Task<ExtractionPipelineResult> ExtractStructuredCvFromFileAsync(IFormFile file, string extension, CancellationToken cancellationToken) + { + string text; + var canUseAiExtraction = string.Equals(extension, ".pdf", StringComparison.OrdinalIgnoreCase) + || string.Equals(extension, ".docx", StringComparison.OrdinalIgnoreCase) + || string.Equals(extension, ".txt", StringComparison.OrdinalIgnoreCase) + || string.Equals(extension, ".md", StringComparison.OrdinalIgnoreCase) + || string.Equals(extension, ".png", StringComparison.OrdinalIgnoreCase) + || string.Equals(extension, ".jpg", StringComparison.OrdinalIgnoreCase) + || string.Equals(extension, ".jpeg", StringComparison.OrdinalIgnoreCase) + || string.Equals(extension, ".webp", StringComparison.OrdinalIgnoreCase); + + if (canUseAiExtraction) + { + await using var uploadStream = file.OpenReadStream(); + var extracted = await _aiService.ExtractTextAsync(uploadStream, file.FileName ?? $"cv{extension}", file.ContentType, cancellationToken); + text = extracted?.Text?.Trim() ?? string.Empty; + } + else + { + text = string.Empty; + } + + if (string.IsNullOrWhiteSpace(text)) + { + text = (await ExtractTextAsync(file, extension)).Trim(); + } + if (string.IsNullOrWhiteSpace(text)) + { + throw new InvalidOperationException("The uploaded CV file could not be read or was empty."); + } + + text = RepairKnownMojibake(text); + var normalizedText = (await MaybeReconstructStructuredCvAsync(text, cancellationToken)).Trim(); + var structuredCv = await BuildStructuredCvAsync(normalizedText, cancellationToken); + return new ExtractionPipelineResult(text, normalizedText, structuredCv); + } + + private async Task ApplyTextExtractionRunAsync(ApplicationUser user, string trigger, string rawText, string normalizedText, StructuredCvProfile structuredCv, int? artifactId, CancellationToken cancellationToken) + { + var run = new CvExtractionRun + { + OwnerUserId = user.Id, + ArtifactId = artifactId, + Trigger = trigger, + ParserVersion = ParserVersion, + NormalizerVersion = NormalizerVersion, + LlmPromptVersion = LlmPromptVersion, + Status = "applied", + RawExtractedText = rawText, + NormalizedText = normalizedText, + StartedAtUtc = DateTimeOffset.UtcNow, + CompletedAtUtc = DateTimeOffset.UtcNow, + AppliedAtUtc = DateTimeOffset.UtcNow, + }; + _db.CvExtractionRuns.Add(run); + await _db.SaveChangesAsync(cancellationToken); + + structuredCv.Metadata.ProfileVersion = (user.CurrentCvProfileVersion ?? 0) + 1; + structuredCv.Metadata.AppliedExtractionRunId = run.Id; + structuredCv.Metadata.UpdatedAtUtc = DateTimeOffset.UtcNow; + await _careerProfileService.SaveVersionAsync(user.Id, structuredCv, trigger, cancellationToken); + var structuredJson = StructuredCvProfileJson.Serialize(structuredCv); + run.StructuredProfileJson = structuredJson; + + user.ProfileCvText = normalizedText; + user.ProfileCvStructureJson = structuredJson; + user.CurrentCvExtractionRunId = run.Id; + user.CurrentCvProfileVersion = structuredCv.Metadata.ProfileVersion; + if (artifactId.HasValue) + { + user.CurrentCvUploadArtifactId = artifactId.Value; + } + + var update = await _users.UpdateAsync(user); + if (!update.Succeeded) + { + run.Status = "failed"; + run.ErrorMessage = string.Join("; ", update.Errors.Select(e => e.Description)); + await _db.SaveChangesAsync(cancellationToken); + throw new InvalidOperationException(run.ErrorMessage); + } + + await _db.SaveChangesAsync(cancellationToken); + await PruneExtractionRunsAsync(user.Id, cancellationToken); + } + + private async Task<CvExtractionRun> CreateQueuedRunAsync(string ownerUserId, int? artifactId, string trigger, CancellationToken cancellationToken) + { + var run = new CvExtractionRun + { + OwnerUserId = ownerUserId, + ArtifactId = artifactId, + Trigger = trigger, + ParserVersion = ParserVersion, + NormalizerVersion = NormalizerVersion, + LlmPromptVersion = LlmPromptVersion, + Status = "queued", + StartedAtUtc = DateTimeOffset.UtcNow, + }; + _db.CvExtractionRuns.Add(run); + await _db.SaveChangesAsync(cancellationToken); + return run; + } + + // Invoked by CvProcessingHostedService (this controller is also registered as a + // transient service). NonAction keeps it off the HTTP surface: without it the + // controller-level [Route] exposes it as an any-verb endpoint. + [NonAction] + public async Task ProcessQueuedRunAsync(int runId, CancellationToken cancellationToken) + { + var run = await _db.CvExtractionRuns.FirstOrDefaultAsync(x => x.Id == runId, cancellationToken); + if (run is null) return; + var user = await _users.FindByIdAsync(run.OwnerUserId); + if (user is null) + { + run.Status = "failed"; + run.ErrorMessage = "CV processing user was not found."; + run.CompletedAtUtc = DateTimeOffset.UtcNow; + await _db.SaveChangesAsync(cancellationToken); + return; + } + + run.Status = "running"; + run.ErrorMessage = null; + await _db.SaveChangesAsync(cancellationToken); + + try + { + switch (run.Trigger) + { + case "rebuild": + { + if (string.IsNullOrWhiteSpace(user.ProfileCvText)) throw new InvalidOperationException("Add or import CV text before rebuilding it."); + var rebuilt = await _aiService.SummarizeSectionAsync( + "Rewrite this CV into a stronger master CV with clear sections such as Professional Summary, Core Skills, Experience Highlights, and Selected Achievements. Preserve only factual claims, avoid inventing employers or metrics, and make the output clean and ready for tailoring to job applications. Return only the rebuilt CV text.", + user.ProfileCvText, + 2200, + 700); + if (string.IsNullOrWhiteSpace(rebuilt)) throw new InvalidOperationException("The AI service could not rebuild your CV text right now."); + + var normalizedText = rebuilt.Trim(); + var structuredCv = await BuildStructuredCvAsync(normalizedText, cancellationToken); + await CompleteQueuedRunForReviewAsync(run, normalizedText, normalizedText, structuredCv, cancellationToken); + break; + } + case "improve": + { + if (string.IsNullOrWhiteSpace(user.ProfileCvText)) throw new InvalidOperationException("Add or import CV text before improving it."); + var improved = await _aiService.SummarizeSectionAsync( + "Rewrite this CV into a cleaner, better-structured master CV profile. Preserve factual claims, employers, skills, and measurable results. Improve clarity, tighten wording, use strong bullet-style phrasing, and keep it ready for further tailoring to specific roles. Return only the improved CV text.", + user.ProfileCvText, + 1800, + 500); + if (string.IsNullOrWhiteSpace(improved)) throw new InvalidOperationException("The AI service could not improve your CV text right now."); + + var normalizedText = improved.Trim(); + var structuredCv = await BuildStructuredCvAsync(normalizedText, cancellationToken); + await CompleteQueuedRunForReviewAsync(run, normalizedText, normalizedText, structuredCv, cancellationToken); + break; + } + case "reprocess": + { + var artifact = await _db.CvUploadArtifacts.AsNoTracking().FirstOrDefaultAsync(x => x.Id == run.ArtifactId && x.OwnerUserId == user.Id, cancellationToken); + if (artifact is null) throw new InvalidOperationException("Upload a CV before reprocessing it."); + if (string.IsNullOrWhiteSpace(artifact.StoragePath) || !System.IO.File.Exists(artifact.StoragePath)) + { + throw new InvalidOperationException("The stored CV artifact could not be found for reprocessing."); + } + + await using var stream = System.IO.File.OpenRead(artifact.StoragePath); + var file = new FormFile(stream, 0, stream.Length, "file", artifact.OriginalFileName) + { + Headers = new HeaderDictionary(), + ContentType = artifact.MimeType + }; + var extension = Path.GetExtension(artifact.OriginalFileName ?? string.Empty); + var result = await ExtractStructuredCvFromFileAsync(file, extension, cancellationToken); + await CompleteQueuedRunForReviewAsync(run, result.RawText, result.NormalizedText, result.StructuredCv, cancellationToken); + break; + } + default: + throw new InvalidOperationException($"Unsupported CV processing trigger '{run.Trigger}'."); + } + + await SendRunCompletionEmailAsync(user, run, true, cancellationToken); + } + catch (Exception ex) + { + run.Status = "failed"; + run.ErrorMessage = ex.Message; + run.CompletedAtUtc = DateTimeOffset.UtcNow; + await _db.SaveChangesAsync(cancellationToken); + await PruneExtractionRunsAsync(user.Id, cancellationToken); + await SendRunCompletionEmailAsync(user, run, false, cancellationToken); + _logger.LogWarning(ex, "CV processing run {RunId} failed for user {UserId}", run.Id, user.Id); + } + } + + private async Task CompleteQueuedRunForReviewAsync(CvExtractionRun run, string rawText, string normalizedText, StructuredCvProfile structuredCv, CancellationToken cancellationToken) + { + run.RawExtractedText = rawText; + run.NormalizedText = normalizedText; + run.StructuredProfileJson = StructuredCvProfileJson.Serialize(structuredCv); + run.Status = "pending_review"; + run.CompletedAtUtc = DateTimeOffset.UtcNow; + await _db.SaveChangesAsync(cancellationToken); + await PruneExtractionRunsAsync(run.OwnerUserId, cancellationToken); + } + + private async Task PruneExtractionRunsAsync(string ownerUserId, CancellationToken cancellationToken) + { + var expired = await _db.CvExtractionRuns + .Where(x => x.OwnerUserId == ownerUserId && x.Status != "queued" && x.Status != "running") + .OrderByDescending(x => x.StartedAtUtc) + .Skip(ExtractionRunRetentionCount) + .ToListAsync(cancellationToken); + if (expired.Count == 0) return; + _db.CvExtractionRuns.RemoveRange(expired); + await _db.SaveChangesAsync(cancellationToken); + } + + private async Task SendRunCompletionEmailAsync(ApplicationUser user, CvExtractionRun run, bool success, CancellationToken cancellationToken) + { + if (string.IsNullOrWhiteSpace(user.Email)) return; + + var subject = success ? $"Your CV {run.Trigger} is complete" : $"Your CV {run.Trigger} failed"; + var body = success + ? $"Your CV {run.Trigger} request finished successfully.\n\nRun ID: {run.Id}\nStatus: {run.Status}\nCompleted: {run.CompletedAtUtc:O}\n" + : $"Your CV {run.Trigger} request failed.\n\nRun ID: {run.Id}\nStatus: {run.Status}\nError: {run.ErrorMessage}\nCompleted: {run.CompletedAtUtc:O}\n"; + + try + { + await _emailSender.SendAsync(user.Email, subject, body, cancellationToken); + } + catch (Exception ex) + { + _logger.LogWarning(ex, "CV processing completion email failed for run {RunId} user {UserId}", run.Id, user.Id); + } + } + +} diff --git a/JobTrackerApi/Controllers/ProfileCvController.cs b/JobTrackerApi/Controllers/ProfileCvController.cs index a155b4e..365cbee 100644 --- a/JobTrackerApi/Controllers/ProfileCvController.cs +++ b/JobTrackerApi/Controllers/ProfileCvController.cs @@ -15,7 +15,7 @@ namespace JobTrackerApi.Controllers; [ApiController] [Route("api/profile-cv")] [Authorize(AuthenticationSchemes = "local")] -public sealed class ProfileCvController : ControllerBase +public sealed partial class ProfileCvController : ControllerBase { private static readonly HashSet<string> AllowedExtensions = new(StringComparer.OrdinalIgnoreCase) { @@ -639,1740 +639,4 @@ public sealed class ProfileCvController : ControllerBase return _cvTemplateRenderer.Render(document, document.TemplateId, candidateName!, targetRole, companyName, AvatarStorage.Resolve(user.AvatarImageDataUrl)); } - private static TailoredCvDocument BuildMasterCvDocument(StructuredCvProfile structuredCv, string templateId, string? targetRole, string? fallbackHeadline, string? companyName) - { - var normalized = StructuredCvProfileJson.Normalize(structuredCv); - var customSections = new List<TailoredCvCustomSection>(); - if (normalized.Certifications.Count > 0) - { - customSections.Add(new TailoredCvCustomSection - { - Title = "Certifications", - Items = normalized.Certifications.Select(certification => string.Join(" | ", new[] { certification.Name, certification.Issuer, certification.Location, certification.Date }.Where(value => !string.IsNullOrWhiteSpace(value)))).Where(value => !string.IsNullOrWhiteSpace(value)).ToList(), - }); - } - if (normalized.Projects.Count > 0) - { - customSections.Add(new TailoredCvCustomSection - { - Title = "Projects", - Items = normalized.Projects.Select(project => string.Join(" | ", new[] { project.Name, project.Role, project.Location, FormatDateRangeForSection(project.Start, project.End, false) }.Where(value => !string.IsNullOrWhiteSpace(value)))).Where(value => !string.IsNullOrWhiteSpace(value)).ToList(), - }); - } - if (normalized.Languages.Count > 0) - { - customSections.Add(new TailoredCvCustomSection - { - Title = "Languages", - Items = normalized.Languages.Select(language => string.Join(": ", new[] { language.Name, language.Level }.Where(value => !string.IsNullOrWhiteSpace(value)))).Where(value => !string.IsNullOrWhiteSpace(value)).ToList(), - }); - } - customSections.AddRange(normalized.OtherSections.Select(section => new TailoredCvCustomSection { Title = section.Title, Items = section.Items })); - - return TailoredCvDraftJson.Normalize(new TailoredCvDocument - { - TemplateId = templateId, - Headline = normalized.Contact.Headline ?? targetRole ?? fallbackHeadline ?? companyName, - Summary = normalized.Summary, - SelectedSkills = normalized.Skills, - Experience = normalized.Jobs.Select(job => new TailoredCvExperienceItem - { - Title = job.Title, - Company = job.Company, - Location = job.Location, - Start = job.Start, - End = job.End, - IsCurrent = job.IsCurrent, - Bullets = job.Bullets, - }).ToList(), - Education = normalized.Education.Select(education => new TailoredCvEducationItem - { - Qualification = education.Qualification, - QualificationLevel = education.QualificationLevel, - Institution = education.Institution, - Location = education.Location, - Start = education.Start, - End = education.End, - Details = education.Details, - }).ToList(), - CustomSections = customSections, - RenderOptions = new TailoredCvRenderOptions - { - ShowPhoto = true, - AccentColor = templateId switch - { - "harvard" => "brick", - "auckland" => "emerald", - "edinburgh" => "plum", - "monarch" => "#7c2d12", - "fjord" => "#0f4c5c", - _ => "slate", - }, - SectionOrder = new List<string> { "summary", "skills", "experience", "education", "custom" }, - } - }); - } - - private async Task<StructuredCvProfile> BuildStructuredCvAsync(string text, CancellationToken cancellationToken) - { - if (LooksLikeNormalizedMarkdownCv(text)) - { - var normalized = BuildStructuredCvFromNormalizedMarkdown(text); - AnnotateStructuredCv(normalized, "normalized-markdown", 0.78); - return StructuredCvProfileJson.Normalize(normalized); - } - - var parseSource = NormalizeTextForStructuredParsing(text); - var parsedSections = ParseSections(parseSource) - .Select(section => new StructuredCvSection - { - Name = section.Name, - Content = section.Content, - WordCount = CountWords(section.Content), - }) - .ToList(); - var hasRealSections = parsedSections.Any(section => !string.Equals(section.Name, "General", StringComparison.OrdinalIgnoreCase)); - - List<ClassifiedCvBlock> classifiedBlocks = new(); - List<StructuredCvSection> fallbackSections = parsedSections; - StructuredCvProfile? classifierFallback = null; - - if (!hasRealSections) - { - classifiedBlocks = await ClassifyBlocksAsync(parseSource, cancellationToken); - var hasMeaningfulClassifierStructure = classifiedBlocks.Any(block => !string.Equals(block.SectionName, "General", StringComparison.OrdinalIgnoreCase)); - if (hasMeaningfulClassifierStructure) - { - fallbackSections = BuildSectionsFromClassifiedBlocks(classifiedBlocks); - classifierFallback = BuildStructuredCvFromClassifiedBlocks(classifiedBlocks); - } - } - - var sectionFallback = StructuredCvProfileJson.FromSections(fallbackSections); - AnnotateStructuredCv(sectionFallback, "repair", 0.56); - var heuristicFallback = BuildHeuristicStructuredCv(parseSource, text); - AnnotateStructuredCv(heuristicFallback, "deterministic", 0.68); - heuristicFallback.Sections = new List<StructuredCvSection>(); - var fallback = StructuredCvProfileJson.Merge(heuristicFallback, sectionFallback); - if (classifierFallback is not null) - { - fallback = StructuredCvProfileJson.Merge(classifierFallback, fallback); - } - fallback.Contact.FullName ??= GuessFullName(text) ?? GuessFullNameFromEmail(fallback.Contact.Email); - var extracted = await TryExtractStructuredCvAsync(parseSource, cancellationToken); - var merged = StructuredCvProfileJson.Merge(extracted, fallback); - merged.Contact.FullName ??= GuessFullName(text) ?? GuessFullNameFromEmail(merged.Contact.Email); - - if (!IsPlausibleLocationValue(merged.Contact.Location, merged.Contact.FullName)) - { - merged.Contact.Location = PreferDetectedLocation(text, null, merged.Contact.FullName); - } - - merged.Jobs = merged.Jobs - .Where(job => !LooksLikePersonName(job.Title ?? string.Empty)) - .ToList(); - - var reparsedJobs = ParseJobsHeuristically(text) - .Where(job => !LooksLikePersonName(job.Title ?? string.Empty)) - .ToList(); - var existingFirstTitle = merged.Jobs.FirstOrDefault()?.Title; - var reparsedFirstTitle = reparsedJobs.FirstOrDefault()?.Title; - - if (LooksLikePersonName(existingFirstTitle ?? string.Empty) - && LooksLikeRoleOrHeadline(reparsedFirstTitle ?? string.Empty) - && ArePlausibleJobs(reparsedJobs, merged.Contact.FullName)) - { - merged.Jobs = reparsedJobs; - } - else if (ArePlausibleJobs(merged.Jobs, merged.Contact.FullName)) - { - if (ScoreJobs(reparsedJobs, merged.Contact.FullName) > ScoreJobs(merged.Jobs, merged.Contact.FullName)) - { - merged.Jobs = reparsedJobs; - } - } - else if (ArePlausibleJobs(reparsedJobs, merged.Contact.FullName)) - { - merged.Jobs = reparsedJobs; - } - - return StructuredCvProfileJson.Normalize(merged); - } - - private async Task<CvUploadArtifact> SaveUploadArtifactAsync(ApplicationUser user, IFormFile file, CancellationToken cancellationToken) - { - var extension = Path.GetExtension(file.FileName ?? string.Empty); - var userRoot = Path.Combine(_paths.CvArtifactsRoot, user.Id); - Directory.CreateDirectory(userRoot); - - var storedFileName = $"{DateTimeOffset.UtcNow:yyyyMMddHHmmss}-{Guid.NewGuid():N}{extension}"; - var storagePath = Path.Combine(userRoot, storedFileName); - - await using (var target = System.IO.File.Create(storagePath)) - await using (var source = file.OpenReadStream()) - { - await source.CopyToAsync(target, cancellationToken); - } - - await using var hashStream = System.IO.File.OpenRead(storagePath); - var shaBytes = await SHA256.HashDataAsync(hashStream, cancellationToken); - - return new CvUploadArtifact - { - OwnerUserId = user.Id, - OriginalFileName = file.FileName ?? storedFileName, - StoredFileName = storedFileName, - MimeType = file.ContentType ?? "application/octet-stream", - ByteSize = file.Length, - Sha256 = Convert.ToHexString(shaBytes), - StoragePath = storagePath, - UploadedAtUtc = DateTimeOffset.UtcNow, - }; - } - - private async Task<ExtractionPipelineResult> ExtractStructuredCvFromFileAsync(IFormFile file, string extension, CancellationToken cancellationToken) - { - string text; - var canUseAiExtraction = string.Equals(extension, ".pdf", StringComparison.OrdinalIgnoreCase) - || string.Equals(extension, ".docx", StringComparison.OrdinalIgnoreCase) - || string.Equals(extension, ".txt", StringComparison.OrdinalIgnoreCase) - || string.Equals(extension, ".md", StringComparison.OrdinalIgnoreCase) - || string.Equals(extension, ".png", StringComparison.OrdinalIgnoreCase) - || string.Equals(extension, ".jpg", StringComparison.OrdinalIgnoreCase) - || string.Equals(extension, ".jpeg", StringComparison.OrdinalIgnoreCase) - || string.Equals(extension, ".webp", StringComparison.OrdinalIgnoreCase); - - if (canUseAiExtraction) - { - await using var uploadStream = file.OpenReadStream(); - var extracted = await _aiService.ExtractTextAsync(uploadStream, file.FileName ?? $"cv{extension}", file.ContentType, cancellationToken); - text = extracted?.Text?.Trim() ?? string.Empty; - } - else - { - text = string.Empty; - } - - if (string.IsNullOrWhiteSpace(text)) - { - text = (await ExtractTextAsync(file, extension)).Trim(); - } - if (string.IsNullOrWhiteSpace(text)) - { - throw new InvalidOperationException("The uploaded CV file could not be read or was empty."); - } - - text = RepairKnownMojibake(text); - var normalizedText = (await MaybeReconstructStructuredCvAsync(text, cancellationToken)).Trim(); - var structuredCv = await BuildStructuredCvAsync(normalizedText, cancellationToken); - return new ExtractionPipelineResult(text, normalizedText, structuredCv); - } - - private async Task ApplyTextExtractionRunAsync(ApplicationUser user, string trigger, string rawText, string normalizedText, StructuredCvProfile structuredCv, int? artifactId, CancellationToken cancellationToken) - { - var run = new CvExtractionRun - { - OwnerUserId = user.Id, - ArtifactId = artifactId, - Trigger = trigger, - ParserVersion = ParserVersion, - NormalizerVersion = NormalizerVersion, - LlmPromptVersion = LlmPromptVersion, - Status = "applied", - RawExtractedText = rawText, - NormalizedText = normalizedText, - StartedAtUtc = DateTimeOffset.UtcNow, - CompletedAtUtc = DateTimeOffset.UtcNow, - AppliedAtUtc = DateTimeOffset.UtcNow, - }; - _db.CvExtractionRuns.Add(run); - await _db.SaveChangesAsync(cancellationToken); - - structuredCv.Metadata.ProfileVersion = (user.CurrentCvProfileVersion ?? 0) + 1; - structuredCv.Metadata.AppliedExtractionRunId = run.Id; - structuredCv.Metadata.UpdatedAtUtc = DateTimeOffset.UtcNow; - await _careerProfileService.SaveVersionAsync(user.Id, structuredCv, trigger, cancellationToken); - var structuredJson = StructuredCvProfileJson.Serialize(structuredCv); - run.StructuredProfileJson = structuredJson; - - user.ProfileCvText = normalizedText; - user.ProfileCvStructureJson = structuredJson; - user.CurrentCvExtractionRunId = run.Id; - user.CurrentCvProfileVersion = structuredCv.Metadata.ProfileVersion; - if (artifactId.HasValue) - { - user.CurrentCvUploadArtifactId = artifactId.Value; - } - - var update = await _users.UpdateAsync(user); - if (!update.Succeeded) - { - run.Status = "failed"; - run.ErrorMessage = string.Join("; ", update.Errors.Select(e => e.Description)); - await _db.SaveChangesAsync(cancellationToken); - throw new InvalidOperationException(run.ErrorMessage); - } - - await _db.SaveChangesAsync(cancellationToken); - await PruneExtractionRunsAsync(user.Id, cancellationToken); - } - - private async Task<CvExtractionRun> CreateQueuedRunAsync(string ownerUserId, int? artifactId, string trigger, CancellationToken cancellationToken) - { - var run = new CvExtractionRun - { - OwnerUserId = ownerUserId, - ArtifactId = artifactId, - Trigger = trigger, - ParserVersion = ParserVersion, - NormalizerVersion = NormalizerVersion, - LlmPromptVersion = LlmPromptVersion, - Status = "queued", - StartedAtUtc = DateTimeOffset.UtcNow, - }; - _db.CvExtractionRuns.Add(run); - await _db.SaveChangesAsync(cancellationToken); - return run; - } - - // Invoked by CvProcessingHostedService (this controller is also registered as a - // transient service). NonAction keeps it off the HTTP surface: without it the - // controller-level [Route] exposes it as an any-verb endpoint. - [NonAction] - public async Task ProcessQueuedRunAsync(int runId, CancellationToken cancellationToken) - { - var run = await _db.CvExtractionRuns.FirstOrDefaultAsync(x => x.Id == runId, cancellationToken); - if (run is null) return; - var user = await _users.FindByIdAsync(run.OwnerUserId); - if (user is null) - { - run.Status = "failed"; - run.ErrorMessage = "CV processing user was not found."; - run.CompletedAtUtc = DateTimeOffset.UtcNow; - await _db.SaveChangesAsync(cancellationToken); - return; - } - - run.Status = "running"; - run.ErrorMessage = null; - await _db.SaveChangesAsync(cancellationToken); - - try - { - switch (run.Trigger) - { - case "rebuild": - { - if (string.IsNullOrWhiteSpace(user.ProfileCvText)) throw new InvalidOperationException("Add or import CV text before rebuilding it."); - var rebuilt = await _aiService.SummarizeSectionAsync( - "Rewrite this CV into a stronger master CV with clear sections such as Professional Summary, Core Skills, Experience Highlights, and Selected Achievements. Preserve only factual claims, avoid inventing employers or metrics, and make the output clean and ready for tailoring to job applications. Return only the rebuilt CV text.", - user.ProfileCvText, - 2200, - 700); - if (string.IsNullOrWhiteSpace(rebuilt)) throw new InvalidOperationException("The AI service could not rebuild your CV text right now."); - - var normalizedText = rebuilt.Trim(); - var structuredCv = await BuildStructuredCvAsync(normalizedText, cancellationToken); - await CompleteQueuedRunForReviewAsync(run, normalizedText, normalizedText, structuredCv, cancellationToken); - break; - } - case "improve": - { - if (string.IsNullOrWhiteSpace(user.ProfileCvText)) throw new InvalidOperationException("Add or import CV text before improving it."); - var improved = await _aiService.SummarizeSectionAsync( - "Rewrite this CV into a cleaner, better-structured master CV profile. Preserve factual claims, employers, skills, and measurable results. Improve clarity, tighten wording, use strong bullet-style phrasing, and keep it ready for further tailoring to specific roles. Return only the improved CV text.", - user.ProfileCvText, - 1800, - 500); - if (string.IsNullOrWhiteSpace(improved)) throw new InvalidOperationException("The AI service could not improve your CV text right now."); - - var normalizedText = improved.Trim(); - var structuredCv = await BuildStructuredCvAsync(normalizedText, cancellationToken); - await CompleteQueuedRunForReviewAsync(run, normalizedText, normalizedText, structuredCv, cancellationToken); - break; - } - case "reprocess": - { - var artifact = await _db.CvUploadArtifacts.AsNoTracking().FirstOrDefaultAsync(x => x.Id == run.ArtifactId && x.OwnerUserId == user.Id, cancellationToken); - if (artifact is null) throw new InvalidOperationException("Upload a CV before reprocessing it."); - if (string.IsNullOrWhiteSpace(artifact.StoragePath) || !System.IO.File.Exists(artifact.StoragePath)) - { - throw new InvalidOperationException("The stored CV artifact could not be found for reprocessing."); - } - - await using var stream = System.IO.File.OpenRead(artifact.StoragePath); - var file = new FormFile(stream, 0, stream.Length, "file", artifact.OriginalFileName) - { - Headers = new HeaderDictionary(), - ContentType = artifact.MimeType - }; - var extension = Path.GetExtension(artifact.OriginalFileName ?? string.Empty); - var result = await ExtractStructuredCvFromFileAsync(file, extension, cancellationToken); - await CompleteQueuedRunForReviewAsync(run, result.RawText, result.NormalizedText, result.StructuredCv, cancellationToken); - break; - } - default: - throw new InvalidOperationException($"Unsupported CV processing trigger '{run.Trigger}'."); - } - - await SendRunCompletionEmailAsync(user, run, true, cancellationToken); - } - catch (Exception ex) - { - run.Status = "failed"; - run.ErrorMessage = ex.Message; - run.CompletedAtUtc = DateTimeOffset.UtcNow; - await _db.SaveChangesAsync(cancellationToken); - await PruneExtractionRunsAsync(user.Id, cancellationToken); - await SendRunCompletionEmailAsync(user, run, false, cancellationToken); - _logger.LogWarning(ex, "CV processing run {RunId} failed for user {UserId}", run.Id, user.Id); - } - } - - private async Task CompleteQueuedRunForReviewAsync(CvExtractionRun run, string rawText, string normalizedText, StructuredCvProfile structuredCv, CancellationToken cancellationToken) - { - run.RawExtractedText = rawText; - run.NormalizedText = normalizedText; - run.StructuredProfileJson = StructuredCvProfileJson.Serialize(structuredCv); - run.Status = "pending_review"; - run.CompletedAtUtc = DateTimeOffset.UtcNow; - await _db.SaveChangesAsync(cancellationToken); - await PruneExtractionRunsAsync(run.OwnerUserId, cancellationToken); - } - - private async Task PruneExtractionRunsAsync(string ownerUserId, CancellationToken cancellationToken) - { - var expired = await _db.CvExtractionRuns - .Where(x => x.OwnerUserId == ownerUserId && x.Status != "queued" && x.Status != "running") - .OrderByDescending(x => x.StartedAtUtc) - .Skip(ExtractionRunRetentionCount) - .ToListAsync(cancellationToken); - if (expired.Count == 0) return; - _db.CvExtractionRuns.RemoveRange(expired); - await _db.SaveChangesAsync(cancellationToken); - } - - private async Task SendRunCompletionEmailAsync(ApplicationUser user, CvExtractionRun run, bool success, CancellationToken cancellationToken) - { - if (string.IsNullOrWhiteSpace(user.Email)) return; - - var subject = success ? $"Your CV {run.Trigger} is complete" : $"Your CV {run.Trigger} failed"; - var body = success - ? $"Your CV {run.Trigger} request finished successfully.\n\nRun ID: {run.Id}\nStatus: {run.Status}\nCompleted: {run.CompletedAtUtc:O}\n" - : $"Your CV {run.Trigger} request failed.\n\nRun ID: {run.Id}\nStatus: {run.Status}\nError: {run.ErrorMessage}\nCompleted: {run.CompletedAtUtc:O}\n"; - - try - { - await _emailSender.SendAsync(user.Email, subject, body, cancellationToken); - } - catch (Exception ex) - { - _logger.LogWarning(ex, "CV processing completion email failed for run {RunId} user {UserId}", run.Id, user.Id); - } - } - - private static void AnnotateStructuredCv(StructuredCvProfile profile, string method, double confidence) - { - var now = DateTimeOffset.UtcNow; - profile.Metadata ??= new StructuredCvMetadata(); - profile.Metadata.Fields ??= new Dictionary<string, StructuredCvFieldMetadata>(); - - void SetIf(string key, string? value) - { - if (string.IsNullOrWhiteSpace(value)) return; - profile.Metadata.Fields[key] = new StructuredCvFieldMetadata - { - Confidence = confidence, - Method = method, - SourceSnippet = value.Length > 180 ? value[..180] : value, - ReviewState = "suggested", - LastUpdatedAtUtc = now, - }; - } - - SetIf("contact.fullName", profile.Contact.FullName); - SetIf("contact.headline", profile.Contact.Headline); - SetIf("contact.email", profile.Contact.Email); - SetIf("contact.phone", profile.Contact.Phone); - SetIf("contact.location", profile.Contact.Location); - SetIf("contact.website", profile.Contact.Website); - SetIf("contact.linkedIn", profile.Contact.LinkedIn); - SetIf("summary", profile.Summary.FirstOrDefault()); - SetIf("skills", profile.Skills.FirstOrDefault()); - SetIf("languages", profile.Languages.FirstOrDefault()?.Name); - SetIf("interests", profile.Interests.FirstOrDefault()); - SetIf("jobs", profile.Jobs.FirstOrDefault()?.Title ?? profile.Jobs.FirstOrDefault()?.Company); - SetIf("education", profile.Education.FirstOrDefault()?.Qualification ?? profile.Education.FirstOrDefault()?.Institution); - } - - private async Task<StructuredCvProfile?> TryExtractStructuredCvAsync(string text, CancellationToken cancellationToken) - { - var structuredJson = await _aiService.SummarizeSectionAsync( - "Extract this CV into structured JSON. Return only valid JSON with this exact top-level shape: { \"version\": \"1\", \"contact\": { \"fullName\": string|null, \"headline\": string|null, \"email\": string|null, \"phone\": string|null, \"location\": string|null, \"website\": string|null, \"linkedin\": string|null }, \"summary\": string[], \"jobs\": [{ \"title\": string|null, \"company\": string|null, \"location\": string|null, \"start\": string|null, \"end\": string|null, \"isCurrent\": boolean, \"bullets\": string[], \"skills\": string[] }], \"education\": [{ \"qualification\": string|null, \"qualificationLevel\": \"Secondary\"|\"Diploma/Certificate\"|\"Bachelor\"|\"Master\"|\"PhD\"|\"Other\"|null, \"institution\": string|null, \"location\": string|null, \"start\": string|null, \"end\": string|null, \"details\": string[] }], \"certifications\": [{ \"name\": string|null, \"issuer\": string|null, \"location\": string|null, \"date\": string|null, \"details\": string[] }], \"projects\": [{ \"name\": string|null, \"role\": string|null, \"location\": string|null, \"start\": string|null, \"end\": string|null, \"bullets\": string[], \"skills\": string[] }], \"skills\": string[], \"languages\": [{ \"name\": string|null, \"level\": string|null, \"notes\": string|null }], \"interests\": string[], \"otherSections\": [{ \"title\": string|null, \"items\": string[] }] }. Preserve facts only. Do not invent anything. If a field is unknown, use null or an empty array. Keep wording close to the source. Profile location should only be the candidate's current/home location. Education location must be the institution location. Work location must be employer/job location. Never place skill lists such as Python or Ruby into location fields. Preserve the original qualification text in education. Set qualificationLevel to the normalized enum when you can infer it, otherwise null. Put unmatched content in otherSections.", - text, - 3200, - 900); - - if (string.IsNullOrWhiteSpace(structuredJson)) return null; - var extracted = ExtractJsonObject(structuredJson); - if (string.IsNullOrWhiteSpace(extracted)) return null; - - var parsed = StructuredCvProfileJson.Deserialize(extracted); - if (!IsMeaningfullyStructured(parsed)) return null; - - AnnotateStructuredCv(parsed, "llm", 0.82); - return parsed; - } - - private static bool IsMeaningfullyStructured(StructuredCvProfile profile) - { - return !string.IsNullOrWhiteSpace(profile.Contact.FullName) - || profile.Summary.Count > 0 - || profile.Jobs.Count > 0 - || profile.Education.Count > 0 - || profile.Skills.Count > 0 - || profile.Languages.Count > 0 - || profile.Interests.Count > 0 - || profile.OtherSections.Count > 0; - } - - private static string? ExtractJsonObject(string raw) - { - var trimmed = raw.Trim(); - if (trimmed.StartsWith("```", StringComparison.Ordinal)) - { - trimmed = Regex.Replace(trimmed, "^```(?:json)?\\s*|\\s*```$", string.Empty, RegexOptions.IgnoreCase); - } - - var start = trimmed.IndexOf('{'); - var end = trimmed.LastIndexOf('}'); - if (start < 0 || end <= start) return null; - return trimmed[start..(end + 1)]; - } - - private static string? GuessFullName(string source) - { - var normalized = source.Replace("\r\n", "\n"); - foreach (var line in normalized.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Take(6)) - { - var cleaned = line.Trim().TrimStart('#').Trim(); - cleaned = Regex.Replace(cleaned, @"(?<=[a-z])(?=[A-Z])", " "); - if (cleaned.Length < 4 || cleaned.Length > 80) continue; - if (cleaned.Contains('@') || Regex.IsMatch(cleaned, @"\d")) continue; - - var nameMatch = Regex.Match(cleaned, @"^(?<name>[A-Z][A-Za-z'`.-]+(?:\s+[A-Z][A-Za-z'`.-]+){1,3})(?:\s+(?:Real Estate Agent|Store Manager|Web Developer|Developer|Engineer|Consultant|Specialist|Analyst).*)?$", RegexOptions.IgnoreCase); - if (nameMatch.Success) - { - return nameMatch.Groups["name"].Value.Trim(); - } - - if (!Regex.IsMatch(cleaned, @"^[A-Z][A-Za-z'`.-]+(?:\s+[A-Z][A-Za-z'`.-]+){1,4}$")) continue; - return cleaned; - } - - return null; - } - - private static string? GuessFullNameFromEmail(string? email) - { - if (string.IsNullOrWhiteSpace(email) || !email.Contains('@')) return null; - var localPart = email[..email.IndexOf('@')].Trim(); - if (string.IsNullOrWhiteSpace(localPart)) return null; - var parts = Regex.Split(localPart, @"[._-]+") - .Select(part => part.Trim()) - .Where(part => part.Length > 0) - .Select(part => char.ToUpperInvariant(part[0]) + part[1..].ToLowerInvariant()) - .ToList(); - return parts.Count >= 2 ? string.Join(" ", parts) : null; - } - - private static string NormalizeTextForStructuredParsing(string source) - { - if (string.IsNullOrWhiteSpace(source)) return string.Empty; - - var text = source.Replace("\r\n", "\n").Trim(); - if (!LooksLikeFlattenedCvExtraction(text)) return text; - - text = Regex.Replace(text, @"\b([A-Z](?:\s+[A-Z]){2,})\b", match => - { - var collapsed = Regex.Replace(match.Value, @"\s+", string.Empty); - foreach (var alias in SectionAliases) - { - var aliasLettersOnly = Regex.Replace(alias.Key, @"[^A-Za-z]", string.Empty); - if (collapsed.Equals(aliasLettersOnly, StringComparison.OrdinalIgnoreCase)) - { - return $"\n\n## {alias.Value}\n"; - } - } - - return match.Value; - }); - - foreach (var alias in SectionAliases.OrderByDescending(pair => pair.Key.Length)) - { - text = Regex.Replace( - text, - $@"(?<!#)\b{Regex.Escape(alias.Key)}\b", - $"\n\n## {alias.Value}\n", - RegexOptions.IgnoreCase); - } - - text = Regex.Replace(text, @"\s+\+\s+", "\n+ "); - text = Regex.Replace(text, @"\s*([•●▪◦])\s*", "\n- "); - text = Regex.Replace(text, @"\s+(\d{4}\s*[-–]\s*(?:\d{4}|Present|Current))\b", "\n$1\n", RegexOptions.IgnoreCase); - text = Regex.Replace(text, @"\n{3,}", "\n\n"); - - return text.Trim(); - } - - private static StructuredCvProfile BuildHeuristicStructuredCv(string parseSource, string rawSource) - { - var profile = new StructuredCvProfile(); - var normalized = parseSource.Replace("\r\n", "\n").Trim(); - - profile.Contact.Email = NullIfWhitespace(Regex.Match(rawSource, @"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}", RegexOptions.IgnoreCase).Value); - profile.Contact.Phone = NormalizeDetectedPhone(Regex.Match(rawSource, @"(?<!\w)(?:\+?\d[\d\s().-]{6,}\d)", RegexOptions.IgnoreCase).Value); - profile.Contact.Website = ExtractPreferredWebsite(rawSource, profile.Contact.Email); - profile.Contact.LinkedIn = NullIfWhitespace(Regex.Match(rawSource, @"(?:linkedin(?:\.com)?/[A-Z0-9._~:/?#\[\]@!$&'()*+,;=-]+)", RegexOptions.IgnoreCase).Value); - profile.Contact.FullName = GuessFullName(rawSource) ?? GuessFullNameFromEmail(profile.Contact.Email); - - var sections = ParseSections(normalized); - var contactSection = sections.FirstOrDefault(section => section.Name == "Contact"); - if (!string.IsNullOrWhiteSpace(contactSection.Content)) - { - var contactFallback = StructuredCvProfileJson.FromSections(new[] { new StructuredCvSection { Name = "Contact", Content = contactSection.Content } }); - profile.Contact.Location = PreferDetectedLocation(contactSection.Content, contactFallback.Contact.Location, profile.Contact.FullName); - profile.Contact.Headline ??= CleanHeadline(contactFallback.Contact.Headline, profile.Contact.FullName); - } - else - { - profile.Contact.Location = PreferDetectedLocation(rawSource, NullIfWhitespace(Regex.Match(rawSource, @"\b[A-Z][a-z]+(?:[\s-][A-Z][a-z]+)*(?:,\s*[A-Z][a-z]+(?:\s+[A-Z][a-z]+)*){1,2}\b").Value), profile.Contact.FullName); - } - - if (string.IsNullOrWhiteSpace(profile.Contact.Location)) - { - var firstTenLines = normalized.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Take(10).ToList(); - profile.Contact.Location = firstTenLines.FirstOrDefault(line => - !line.Contains('@') - && !Regex.IsMatch(line, @"https?://|www\.", RegexOptions.IgnoreCase) - && Regex.IsMatch(line, @"^[A-Z][A-Za-z.' -]+(?:,\s*[A-Z][A-Za-z.' -]+)?$") - && !line.Contains("Skills", StringComparison.OrdinalIgnoreCase) - && !line.Contains("Summary", StringComparison.OrdinalIgnoreCase) - && !line.Contains("Developer", StringComparison.OrdinalIgnoreCase) - && !line.Contains("Agent", StringComparison.OrdinalIgnoreCase) - && !string.Equals(line, profile.Contact.FullName, StringComparison.OrdinalIgnoreCase)); - } - - if (!string.IsNullOrWhiteSpace(profile.Contact.Location)) - { - profile.Contact.Location = Regex.Replace(profile.Contact.Location, @"\bSkills\b.*$", string.Empty, RegexOptions.IgnoreCase).Trim(' ', ','); - } - - var summarySection = sections.FirstOrDefault(section => section.Name == "Professional Summary"); - var flattenedSummary = Regex.Match( - rawSource, - @"(?:A\s+B\s+O\s+U\s+T\s+M\s+E|P\s+R\s+O\s+F\s+I\s+L\s+E|S\s+U\s+M\s+M\s+A\s+R\s+Y)\s*(?<body>.*?)(?=(?:I\s+N\s+T\s+E\s+R\s+E\s+S\s+T\s+S|E\s+X\s+P\s+E\s+R\s+I\s+E\s+N\s+C\s+E|E\s+D\s+U\s+C\s+A\s+T\s+I\s+O\s+N|C\s+O\s+N\s+T\s+A\s+C\s+T|$))", - RegexOptions.IgnoreCase | RegexOptions.Singleline); - if (flattenedSummary.Success) - { - profile.Summary = SplitSentences(flattenedSummary.Groups["body"].Value, 5) - .Where(item => !Regex.IsMatch(item, @"^:?\s*https?://", RegexOptions.IgnoreCase)) - .ToList(); - } - else if (!string.IsNullOrWhiteSpace(summarySection.Content)) - { - profile.Summary = SplitSentences(summarySection.Content, 5) - .Where(item => !Regex.IsMatch(item, @"^:?\s*https?://", RegexOptions.IgnoreCase)) - .ToList(); - } - - var interestsSection = sections.FirstOrDefault(section => section.Name == "Interests"); - if (!string.IsNullOrWhiteSpace(interestsSection.Content)) - { - profile.Interests = SplitListLike(interestsSection.Content); - } - else - { - var flattenedInterests = Regex.Match( - rawSource, - @"I\s+N\s+T\s+E\s+R\s+E\s+S\s+T\s+S\s*(?<body>.*?)(?=(?:E\s+X\s+P\s+E\s+R\s+I\s+E\s+N\s+C\s+E|C\s+O\s+N\s+T\s+A\s+C\s+T|E\s+D\s+U\s+C\s+A\s+T\s+I\s+O\s+N|$))", - RegexOptions.IgnoreCase | RegexOptions.Singleline); - if (flattenedInterests.Success) - { - profile.Interests = SplitSentences(flattenedInterests.Groups["body"].Value, 4); - } - } - - var languagesSection = sections.FirstOrDefault(section => section.Name == "Languages"); - if (!string.IsNullOrWhiteSpace(languagesSection.Content)) - { - profile.Languages = ParseLanguagesHeuristically(languagesSection.Content); - } - else - { - profile.Languages = ParseLanguagesHeuristically(rawSource); - } - - var skills = new HashSet<string>(StringComparer.OrdinalIgnoreCase); - foreach (var skill in ExtractSkillsHeuristically(rawSource)) - { - skills.Add(skill); - } - profile.Skills = skills.ToList(); - - var educationSection = sections.FirstOrDefault(section => section.Name == "Education"); - if (!string.IsNullOrWhiteSpace(educationSection.Content)) - { - profile.Education = ParseEducationHeuristically(educationSection.Content); - } - - var certificationsSection = sections.FirstOrDefault(section => section.Name == "Certifications"); - if (!string.IsNullOrWhiteSpace(certificationsSection.Content)) - { - profile.Certifications = StructuredCvProfileJson.FromSections(new[] { new StructuredCvSection { Name = "Certifications", Content = certificationsSection.Content } }).Certifications; - } - - var projectsSection = sections.FirstOrDefault(section => section.Name == "Projects"); - if (!string.IsNullOrWhiteSpace(projectsSection.Content)) - { - profile.Projects = StructuredCvProfileJson.FromSections(new[] { new StructuredCvSection { Name = "Projects", Content = projectsSection.Content } }).Projects; - } - - var experienceSection = sections.FirstOrDefault(section => section.Name == "Work Experience"); - if (!string.IsNullOrWhiteSpace(experienceSection.Content)) - { - profile.Jobs = ParseJobsHeuristically(experienceSection.Content); - } - else if (profile.Jobs.Count == 0) - { - profile.Jobs = ParseJobsHeuristically(normalized); - } - - if (profile.OtherSections.Count == 0 && sections.Any(section => section.Name == "General")) - { - var general = sections.First(section => section.Name == "General"); - if (!string.IsNullOrWhiteSpace(general.Content) && profile.Summary.Count == 0) - { - profile.Summary = SplitSentences(general.Content, 3); - } - } - - return StructuredCvProfileJson.Normalize(profile); - } - - private static List<string> SplitSentences(string content, int limit) - { - return Regex.Split(content.Replace("\r\n", " "), @"(?<=[.!?])\s+") - .Select(value => value.Trim()) - .Where(value => value.Length > 20) - .Take(limit) - .ToList(); - } - - private static readonly string[] ConservativeSkillHints = - { - "C#", ".NET", "ASP.NET", "SQL", "JavaScript", "TypeScript", "Python", "Ruby on Rails", "Ruby", "React", "Azure", "Azure DevOps", "GitHub", "CI/CD", "HTML5", "CSS", "MySQL", "PHP OOP", "Project management", "Revenue generation", "Business development", "Effective marketing", "Organisational capacity", "Operability and commitment", "Attention to Detail", "Property Valuation", "Retail Market Analysis", "Client Relationship Management", "Digital Marketing" - }; - - private static List<string> SplitListLike(string content) - { - return content - .Replace("\r\n", "\n") - .Split(new[] { '\n', ',', ';', '•', '●' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) - .SelectMany(item => item.Contains(" ", StringComparison.Ordinal) ? Regex.Split(item, @"\s{2,}") : new[] { item }) - .Select(item => item.Trim().TrimStart('-', '•', '*', ' ')) - .Where(item => item.Length > 1) - .Distinct(StringComparer.OrdinalIgnoreCase) - .ToList(); - } - - private static IEnumerable<string> ExtractConservativeSkills(string content) - { - foreach (var skill in ConservativeSkillHints) - { - if (Regex.IsMatch(content, $@"(?<![A-Za-z0-9]){Regex.Escape(skill)}(?![A-Za-z0-9])", RegexOptions.IgnoreCase)) - { - yield return skill; - } - } - } - - private static List<string> ExtractSkillsFromBullets(IEnumerable<string> bullets) - { - return ExtractConservativeSkills(string.Join("\n", bullets)) - .Distinct(StringComparer.OrdinalIgnoreCase) - .ToList(); - } - - private static IEnumerable<string> ExtractSkillsHeuristically(string content) - { - var yielded = new HashSet<string>(StringComparer.OrdinalIgnoreCase); - - foreach (var skill in ExtractConservativeSkills(content)) - { - if (yielded.Add(skill)) yield return skill; - } - - var highlightsMatch = Regex.Match(content, @"(?:Highlights|Core Skills|Skills|Technical Skills|Skill Highlights|Competencies)\s*(?<body>.*?)(?=(?:Experience|Education|Languages|Interests|Projects|Certifications|$))", RegexOptions.IgnoreCase | RegexOptions.Singleline); - if (highlightsMatch.Success) - { - foreach (var item in SplitListLike(highlightsMatch.Groups["body"].Value)) - { - var trimmed = item.Trim(); - if (trimmed.Length >= 3 && trimmed.Length <= 80 && trimmed.Count(char.IsLetter) >= 3) - { - if (yielded.Add(trimmed)) yield return trimmed; - } - } - } - } - - private static string? NormalizeDetectedPhone(string? value) - { - var trimmed = NullIfWhitespace(value); - if (trimmed is null) return null; - - var digits = trimmed.Count(char.IsDigit); - if (digits < 7) return null; - - var looksLikeRawCoordinates = trimmed.Contains(" -") && digits > 18 && !trimmed.Contains('+') && !trimmed.Contains('('); - if (looksLikeRawCoordinates) return null; - - return trimmed; - } - - private static string? NormalizeDetectedWebsite(string? value, string? email) - { - var trimmed = NullIfWhitespace(value); - if (trimmed is null) return null; - if (!trimmed.Contains('.', StringComparison.Ordinal)) return null; - if (trimmed.Contains('@')) return null; - if (trimmed.Equals("gmail.com", StringComparison.OrdinalIgnoreCase)) return null; - - var candidate = trimmed.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? trimmed : $"https://{trimmed}"; - if (!Uri.TryCreate(candidate, UriKind.Absolute, out var uri)) return null; - if (string.IsNullOrWhiteSpace(uri.Host) || !uri.Host.Contains('.', StringComparison.Ordinal)) return null; - - return trimmed.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? trimmed : uri.Host; - } - - private static string? ExtractPreferredWebsite(string rawSource, string? email) - { - foreach (Match match in Regex.Matches(rawSource, @"\b(?:https?://)?(?:www\.)?[A-Z0-9.-]+\.[A-Z]{2,}(?:/[A-Z0-9._~:/?#\[\]@!$&'()*+,;=-]*)?", RegexOptions.IgnoreCase)) - { - var candidate = NormalizeDetectedWebsite(match.Value, email); - if (candidate is null) continue; - if (candidate.Contains("linkedin.com", StringComparison.OrdinalIgnoreCase)) continue; - return candidate; - } - - return null; - } - - private static string? PreferDetectedLocation(string source, string? fallback, string? fullName = null) - { - var normalizedFallback = NullIfWhitespace(fallback); - if (normalizedFallback is not null) - { - normalizedFallback = Regex.Replace(normalizedFallback, @",?\s*(Hobbies|Education)\b.*$", string.Empty, RegexOptions.IgnoreCase).Trim(' ', ','); - } - - if (IsPlausibleLocationValue(normalizedFallback, fullName)) - { - return normalizedFallback; - } - - var lines = source.Replace("\r\n", "\n").Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); - foreach (var rawLine in lines.Take(10)) - { - var line = Regex.Replace(rawLine, @",?\s*(Hobbies|Education)\b.*$", string.Empty, RegexOptions.IgnoreCase).Trim(' ', ','); - if (!IsPlausibleLocationValue(line, fullName)) continue; - return line; - } - - return IsPlausibleLocationValue(normalizedFallback, fullName) ? normalizedFallback : null; - } - - private static bool IsPlausibleLocationValue(string? value, string? fullName) - { - var candidate = NullIfWhitespace(value); - if (candidate is null) return false; - if (LooksLikeRoleOrHeadline(candidate)) return false; - if (!string.IsNullOrWhiteSpace(fullName)) - { - if (candidate.Equals(fullName, StringComparison.OrdinalIgnoreCase)) return false; - if (candidate.StartsWith(fullName + " ", StringComparison.OrdinalIgnoreCase)) return false; - } - - if (candidate.Contains("Education", StringComparison.OrdinalIgnoreCase) - || candidate.Contains("Hobbies", StringComparison.OrdinalIgnoreCase) - || candidate.Contains("Skills", StringComparison.OrdinalIgnoreCase) - || candidate.Contains("Summary", StringComparison.OrdinalIgnoreCase)) return false; - if (candidate.Contains('@') || Regex.IsMatch(candidate, @"https?://|www\.", RegexOptions.IgnoreCase)) return false; - if (candidate.Count(char.IsDigit) >= 5) return false; - if (Regex.IsMatch(candidate, @"^\d+\s+.+")) return true; - - var normalized = Regex.Replace(candidate, @"\s+", " ").Trim(' ', ','); - if (normalized.Length > 80) return false; - - if (Regex.IsMatch(normalized, @"^[A-Z][A-Za-z.' -]+,\s*[A-Z][A-Za-z.' -]+(?:,\s*[A-Z][A-Za-z.' -]+)?$")) return true; - if (Regex.IsMatch(normalized, @"^[A-Z][A-Za-z.' -]+(?:\s+[A-Z][A-Za-z.' -]+){0,2}$") && !LooksLikeRoleOrHeadline(normalized)) return true; - - return false; - } - - private static bool LooksLikeRoleOrHeadline(string value) - { - return Regex.IsMatch(value, @"\b(real estate agent|developer|engineer|manager|consultant|specialist|analyst|designer|technician|administrator|architect|director|coordinator|assistant|lead|owner|founder|recruiter|teacher|writer|producer|officer|supervisor|sales)\b", RegexOptions.IgnoreCase); - } - - private static bool LooksLikePersonName(string value) - { - return Regex.IsMatch(value, @"^[A-Z][A-Za-z'`.-]+(?:\s+[A-Z][A-Za-z'`.-]+){1,3}$") - && !LooksLikeRoleOrHeadline(value); - } - - private static bool ArePlausibleJobs(List<StructuredCvJob>? jobs, string? fullName) - { - if (jobs is null || jobs.Count == 0) return false; - return jobs.Any(job => IsPlausibleJob(job, fullName)); - } - - private static int ScoreJobs(List<StructuredCvJob>? jobs, string? fullName) - { - if (jobs is null || jobs.Count == 0) return 0; - var first = jobs[0]; - var score = 0; - if (IsPlausibleJob(first, fullName)) score += 5; - if (!string.IsNullOrWhiteSpace(first.Title) && LooksLikeRoleOrHeadline(first.Title)) score += 4; - if (!string.IsNullOrWhiteSpace(first.Company)) score += 2; - if (!string.IsNullOrWhiteSpace(first.Start) || !string.IsNullOrWhiteSpace(first.End)) score += 2; - if (first.Bullets.Count > 0) score += 2; - score += Math.Min(jobs.Count, 3); - return score; - } - - private static bool IsPlausibleJob(StructuredCvJob? job, string? fullName) - { - if (job is null) return false; - var title = NullIfWhitespace(job.Title); - var company = NullIfWhitespace(job.Company); - var location = NullIfWhitespace(job.Location); - var hasEvidence = !string.IsNullOrWhiteSpace(company) - || !string.IsNullOrWhiteSpace(location) - || !string.IsNullOrWhiteSpace(job.Start) - || !string.IsNullOrWhiteSpace(job.End) - || job.Bullets.Count > 0; - - if (title is null) return hasEvidence; - if (!string.IsNullOrWhiteSpace(fullName) && title.Equals(fullName, StringComparison.OrdinalIgnoreCase)) return false; - if (LooksLikePersonName(title)) return false; - if (title.Contains('@') || Regex.IsMatch(title, @"https?://|www\.", RegexOptions.IgnoreCase)) return false; - if (Regex.IsMatch(title, @"^(?:\d{2}/\d{4}|\d{4})\s*(?:[-–]|to)\s*(?:\d{2}/\d{4}|\d{4}|Present|Current)$", RegexOptions.IgnoreCase)) return false; - if (!hasEvidence && !LooksLikeRoleOrHeadline(title)) return false; - return true; - } - - private static string? CleanHeadline(string? value, string? fullName) - { - var trimmed = NullIfWhitespace(value); - if (trimmed is null) return null; - if (!string.IsNullOrWhiteSpace(fullName) && trimmed.Equals(fullName, StringComparison.OrdinalIgnoreCase)) return null; - if (trimmed.Contains('@') || trimmed.Count(char.IsDigit) > 3) return null; - return trimmed; - } - - private static List<StructuredCvLanguage> ParseLanguagesHeuristically(string content) - { - var languages = new List<StructuredCvLanguage>(); - var candidates = Regex.Split(content.Replace("\r\n", "\n"), @"[\n,;]+|(?<=[.!?])\s+") - .Select(item => item.Trim()) - .Where(item => item.Length > 1); - - foreach (var candidate in candidates) - { - var level = HumanLanguageCatalog.ExtractLevel(candidate); - if (level is null) continue; - - foreach (var name in HumanLanguageCatalog.ExtractLanguageNames(candidate)) - { - languages.Add(new StructuredCvLanguage { Name = name, Level = level }); - } - } - - return languages - .GroupBy(language => language.Name, StringComparer.OrdinalIgnoreCase) - .Select(group => group.First()) - .ToList(); - } - - private static List<StructuredCvEducation> ParseEducationHeuristically(string content) - { - var normalized = content.Replace("\r\n", "\n").Trim(); - var blocks = Regex.Split(normalized, @"\n\s*\n|(?=###\s+)|(?=(?:Bachelor|Master|Doctor|Associate|Diploma|Certificate|BSc|BA|MSc|MA|PhD)\b)", RegexOptions.IgnoreCase) - .Select(block => block.Trim()) - .Where(block => block.Length > 0) - .ToList(); - - var items = new List<StructuredCvEducation>(); - foreach (var block in blocks) - { - var candidate = StructuredCvProfileJson.FromSections(new[] { new StructuredCvSection { Name = "Education", Content = block } }).Education; - if (candidate.Count > 0) - { - items.AddRange(candidate); - continue; - } - - var lines = block.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToList(); - if (lines.Count == 0) continue; - - var dateMatch = Regex.Match(block, @"\b(\d{4})\s*[-–]\s*(\d{4}|Present|Current)\b", RegexOptions.IgnoreCase); - var institutionLine = lines.FirstOrDefault(line => line.StartsWith("+ ", StringComparison.Ordinal))?.TrimStart('+', ' '); - var qualificationLine = lines.FirstOrDefault(line => !line.StartsWith("+ ", StringComparison.Ordinal) && !Regex.IsMatch(line, @"^\d{4}\s*[-–]")); - if (qualificationLine is null && lines.Count > 0) qualificationLine = lines[0]; - - if (qualificationLine is null && institutionLine is null) continue; - items.Add(new StructuredCvEducation - { - Qualification = TitleCasePreservingAcronyms(qualificationLine), - QualificationLevel = InferQualificationLevel(qualificationLine), - Institution = TitleCasePreservingAcronyms(institutionLine), - Start = dateMatch.Success ? dateMatch.Groups[1].Value : null, - End = dateMatch.Success ? dateMatch.Groups[2].Value : null, - Details = lines.Where(line => line.StartsWith("- ", StringComparison.Ordinal)).Select(line => line[2..].Trim()).ToList(), - }); - } - - return items; - } - - private static List<StructuredCvJob> ParseJobsHeuristically(string content) - { - var normalized = content.Replace("\r\n", "\n").Trim(); - var structured = StructuredCvProfileJson.FromSections(new[] { new StructuredCvSection { Name = "Work Experience", Content = normalized } }).Jobs; - var earlierRoles = ParseEarlierRoles(normalized); - foreach (var role in earlierRoles) - { - if (!structured.Any(job => string.Equals(job.Title, role.Title, StringComparison.OrdinalIgnoreCase) - && string.Equals(job.Company, role.Company, StringComparison.OrdinalIgnoreCase))) - { - structured.Add(role); - } - } - if (ArePlausibleJobs(structured, null)) - { - return structured; - } - - var simpleLines = normalized.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); - var inlineDateIndex = Array.FindIndex(simpleLines, line => Regex.IsMatch(line, @".+\d{2}/\d{4}\s+to\s+\d{2}/\d{4}", RegexOptions.IgnoreCase) || Regex.IsMatch(line, @".+\d{4}\s*(?:[-–]|to)\s*(?:\d{4}|Present|Current)", RegexOptions.IgnoreCase)); - if (inlineDateIndex >= 0) - { - var titleLine = Regex.Replace(simpleLines[inlineDateIndex], @"\s*[-–]?\s*\d{2}/\d{4}\s+to\s+\d{2}/\d{4}.*$", string.Empty, RegexOptions.IgnoreCase); - titleLine = Regex.Replace(titleLine, @"\s*[-–]?\s*\d{4}\s*[-–]\s*(?:\d{4}|Present|Current).*$", string.Empty, RegexOptions.IgnoreCase).Trim(); - var companyOrLocation = inlineDateIndex + 1 < simpleLines.Length ? simpleLines[inlineDateIndex + 1] : null; - var datesMatch = Regex.Match(simpleLines[inlineDateIndex], @"(\d{2}/\d{4}|\d{4})\s*(?:to|[-–])\s*(\d{2}/\d{4}|\d{4}|Present|Current)", RegexOptions.IgnoreCase); - var bullets = simpleLines.Skip(inlineDateIndex + 2).Where(line => line.Length > 12).ToList(); - if (!string.IsNullOrWhiteSpace(titleLine)) - { - return new List<StructuredCvJob> - { - new StructuredCvJob - { - Title = titleLine, - Company = companyOrLocation, - Start = datesMatch.Success ? datesMatch.Groups[1].Value : null, - End = datesMatch.Success ? datesMatch.Groups[2].Value : null, - IsCurrent = datesMatch.Success && (string.Equals(datesMatch.Groups[2].Value, "Present", StringComparison.OrdinalIgnoreCase) || string.Equals(datesMatch.Groups[2].Value, "Current", StringComparison.OrdinalIgnoreCase)), - Bullets = bullets, - Skills = ExtractSkillsFromBullets(bullets), - } - }; - } - } - - var dateIndex = Array.FindIndex(simpleLines, line => Regex.IsMatch(line, @"(?:\d{2}/\d{4}|\d{4})\s*(?:[-–]|to)\s*(?:\d{2}/\d{4}|\d{4}|Present|Current)", RegexOptions.IgnoreCase)); - if (dateIndex >= 0) - { - if (dateIndex + 2 < simpleLines.Length && LooksLikeRoleOrHeadline(simpleLines[dateIndex + 1])) - { - var datesLine = simpleLines[dateIndex]; - var titleLine = simpleLines[dateIndex + 1]; - var companyLine = simpleLines[dateIndex + 2]; - var bullets = SplitSentences(string.Join(" ", simpleLines.Skip(dateIndex + 3)), 6); - var parts = Regex.Split(datesLine, @"\s*[-–]\s*"); - return new List<StructuredCvJob> - { - new StructuredCvJob - { - Title = titleLine, - Company = companyLine, - Start = parts.FirstOrDefault(), - End = parts.Skip(1).FirstOrDefault(), - IsCurrent = string.Equals(parts.Skip(1).FirstOrDefault(), "Present", StringComparison.OrdinalIgnoreCase) || string.Equals(parts.Skip(1).FirstOrDefault(), "Current", StringComparison.OrdinalIgnoreCase), - Bullets = bullets, - Skills = ExtractSkillsFromBullets(bullets), - } - }; - } - - if (dateIndex >= 2) - { - var titleLine = simpleLines[dateIndex - 2]; - var locationLine = simpleLines[dateIndex - 1]; - var datesLine = simpleLines[dateIndex]; - var bullets = simpleLines.Skip(dateIndex + 1).Where(line => line.Length > 12).ToList(); - var parts = Regex.Split(datesLine, @"\s*[-–]\s*"); - return new List<StructuredCvJob> - { - new StructuredCvJob - { - Title = titleLine, - Location = locationLine, - Start = parts.FirstOrDefault(), - End = parts.Skip(1).FirstOrDefault(), - IsCurrent = string.Equals(parts.Skip(1).FirstOrDefault(), "Present", StringComparison.OrdinalIgnoreCase) || string.Equals(parts.Skip(1).FirstOrDefault(), "Current", StringComparison.OrdinalIgnoreCase), - Bullets = bullets, - Skills = ExtractSkillsFromBullets(bullets), - } - }; - } - } - - var pattern = new Regex(@"(?<title>[A-Z][A-Z\s/&-]{3,})\s*\n(?<dates>\d{4}\s*[-–]\s*(?:\d{4}|Present|Current))(?<body>.*?)(?=(?:\n[A-Z][A-Z\s/&-]{3,}\s*\n\d{4}\s*[-–]\s*(?:\d{4}|Present|Current))|\z)", RegexOptions.Singleline); - var jobs = new List<StructuredCvJob>(); - - foreach (Match match in pattern.Matches(normalized)) - { - var body = match.Groups["body"].Value.Trim(); - var employer = NullIfWhitespace(Regex.Match(body, @"\+\s*([^\n]+)").Groups[1].Value); - var dates = Regex.Split(match.Groups["dates"].Value, @"\s*[-–]\s*"); - var bullets = SplitSentences(Regex.Replace(body, @"\+\s*[^\n]+", string.Empty), 6); - - jobs.Add(new StructuredCvJob - { - Title = TitleCasePreservingAcronyms(match.Groups["title"].Value), - Company = employer, - Start = NullIfWhitespace(dates.FirstOrDefault()), - End = NullIfWhitespace(dates.Skip(1).FirstOrDefault()), - IsCurrent = string.Equals(dates.Skip(1).FirstOrDefault(), "present", StringComparison.OrdinalIgnoreCase) || string.Equals(dates.Skip(1).FirstOrDefault(), "current", StringComparison.OrdinalIgnoreCase), - Bullets = bullets, - Skills = ExtractSkillsFromBullets(bullets), - }); - } - - return jobs; - } - - private static List<StructuredCvJob> ParseEarlierRoles(string content) - { - var heading = Regex.Match(content, @"(?im)^\s*(?:[-*]\s*)?Earlier roles(?:\s*\(part[- ]?time\))?\s*:?\s*$"); - if (!heading.Success) return new List<StructuredCvJob>(); - - var roles = new List<StructuredCvJob>(); - foreach (var rawLine in content[(heading.Index + heading.Length)..].Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) - { - if (rawLine.StartsWith('#')) break; - var line = rawLine.Trim().TrimStart('-', '*', '•', ' '); - var dates = Regex.Match(line, @"(?<start>\d{4})\s*[-–—]\s*(?<end>\d{4}|Present|Current)", RegexOptions.IgnoreCase); - if (!dates.Success) continue; - - var identity = Regex.Replace(line, @"\s*[|,(]?\s*\d{4}\s*[-–—]\s*(?:\d{4}|Present|Current)\s*\)?\s*$", string.Empty, RegexOptions.IgnoreCase).Trim(); - var parts = Regex.Split(identity, @"\s+(?:—|–|\||at)\s+", RegexOptions.IgnoreCase); - if (parts.Length != 2 || parts.Any(string.IsNullOrWhiteSpace)) continue; - - roles.Add(new StructuredCvJob - { - Title = parts[0].Trim(), - Company = parts[1].Trim(), - Start = dates.Groups["start"].Value, - End = dates.Groups["end"].Value, - IsCurrent = dates.Groups["end"].Value.Equals("Present", StringComparison.OrdinalIgnoreCase) - || dates.Groups["end"].Value.Equals("Current", StringComparison.OrdinalIgnoreCase), - }); - } - return roles; - } - - private static string? TitleCasePreservingAcronyms(string? value) - { - if (string.IsNullOrWhiteSpace(value)) return null; - - var words = value.Trim() - .Split(' ', StringSplitOptions.RemoveEmptyEntries) - .Select(word => word.Length <= 3 && word.All(char.IsUpper) - ? word - : char.ToUpperInvariant(word[0]) + word[1..].ToLowerInvariant()) - .ToArray(); - - return string.Join(" ", words); - } - - private static string? InferQualificationLevel(string? value) - { - var candidate = value?.Trim(); - if (string.IsNullOrWhiteSpace(candidate)) return null; - if (Regex.IsMatch(candidate, @"\b(phd|doctorate|dphil)\b", RegexOptions.IgnoreCase)) return "PhD"; - if (Regex.IsMatch(candidate, @"\b(master(?:'s)?|msc|m\.sc|ma|m\.a|mba|meng)\b", RegexOptions.IgnoreCase)) return "Master"; - if (Regex.IsMatch(candidate, @"\b(bachelor(?:'s)?|bsc|b\.sc|ba|b\.a|beng|degree)\b", RegexOptions.IgnoreCase)) return "Bachelor"; - if (Regex.IsMatch(candidate, @"\b(diploma|certificate|certification|nvq|btec|level\s*\d+|apprenticeship|associate)\b", RegexOptions.IgnoreCase)) return "Diploma/Certificate"; - if (Regex.IsMatch(candidate, @"\b(gcse|a-?level|secondary|high school)\b", RegexOptions.IgnoreCase)) return "Secondary"; - return "Other"; - } - - private static int CountWords(string? text) - { - if (string.IsNullOrWhiteSpace(text)) return 0; - return text.Trim().Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries).Length; - } - - private static string? NullIfWhitespace(string? value) - { - return string.IsNullOrWhiteSpace(value) ? null : value.Trim(); - } - - private static List<(string Name, string Content)> ParseSections(string source) - { - var lines = source.Replace("\r\n", "\n").Split('\n'); - var sections = new List<(string Name, List<string> Lines)>(); - var currentName = "General"; - var currentLines = new List<string>(); - - void Flush() - { - var content = string.Join("\n", currentLines).Trim(); - if (!string.IsNullOrWhiteSpace(content)) - { - sections.Add((currentName, new List<string>(currentLines))); - } - currentLines.Clear(); - } - - foreach (var raw in lines) - { - var line = raw.Trim(); - var canonicalHeading = CanonicalizeSectionHeading(line); - if (canonicalHeading is not null) - { - Flush(); - currentName = canonicalHeading; - continue; - } - - currentLines.Add(raw); - } - - Flush(); - - if (sections.Count == 0) - { - return new List<(string Name, string Content)> { ("General", source.Trim()) }; - } - - return sections - .Select(section => (section.Name, string.Join("\n", section.Lines).Trim())) - .Where(section => !string.IsNullOrWhiteSpace(section.Item2)) - .ToList(); - } - - private static List<StructuredCvSection> BuildSectionsFromClassifiedBlocks(List<ClassifiedCvBlock> classifiedBlocks) - { - var sectionBuckets = new List<StructuredCvSection>(); - foreach (var block in classifiedBlocks) - { - var existing = sectionBuckets.FirstOrDefault(section => section.Name == block.SectionName); - if (existing is null) - { - sectionBuckets.Add(new StructuredCvSection { Name = block.SectionName, Content = block.Content, WordCount = CountWords(block.Content) }); - } - else - { - existing.Content = $"{existing.Content}\n\n{block.Content}".Trim(); - existing.WordCount = CountWords(existing.Content); - } - } - - return sectionBuckets.Where(section => !string.IsNullOrWhiteSpace(section.Content)).ToList(); - } - - private static StructuredCvProfile BuildStructuredCvFromClassifiedBlocks(List<ClassifiedCvBlock> classifiedBlocks) - { - var profile = new StructuredCvProfile(); - var now = DateTimeOffset.UtcNow; - var summary = new List<string>(); - var skills = new HashSet<string>(StringComparer.OrdinalIgnoreCase); - - foreach (var block in classifiedBlocks) - { - switch (block.SectionName) - { - case "Professional Summary": - foreach (var item in (block.Classification?.Summary is { Count: > 0 } - ? block.Classification.Summary - : SplitClassifierContent(block.Content, 5))) - { - summary.Add(item); - } - ApplyClassifierFieldMetadata(profile, "summary", summary.FirstOrDefault(), block, now); - break; - case "Skills": - foreach (var item in (block.Classification?.Skills is { Count: > 0 } - ? block.Classification.Skills.Where(skill => !string.IsNullOrWhiteSpace(skill)).Select(skill => skill.Trim()) - : SplitClassifierSkills(block.Content))) - { - skills.Add(item); - } - ApplyClassifierFieldMetadata(profile, "skills", skills.FirstOrDefault(), block, now); - break; - case "Work Experience": - var job = BuildJobFromClassifiedBlock(block); - if (job is not null) - { - var index = profile.Jobs.Count; - profile.Jobs.Add(job); - ApplyClassifierFieldMetadata(profile, $"jobs[{index}].title", job.Title, block, now); - ApplyClassifierFieldMetadata(profile, $"jobs[{index}].company", job.Company, block, now); - ApplyClassifierFieldMetadata(profile, $"jobs[{index}].location", job.Location, block, now); - } - break; - case "Education": - var education = BuildEducationFromClassifiedBlock(block); - if (education is not null) - { - var index = profile.Education.Count; - profile.Education.Add(education); - ApplyClassifierFieldMetadata(profile, $"education[{index}].qualification", education.Qualification, block, now); - ApplyClassifierFieldMetadata(profile, $"education[{index}].institution", education.Institution, block, now); - } - break; - default: - if (!string.IsNullOrWhiteSpace(block.Content)) - { - profile.OtherSections.Add(new StructuredCvOtherSection - { - Title = block.SectionName, - Items = SplitClassifierContent(block.Content, 6) - }); - } - break; - } - } - - profile.Summary = summary.Distinct(StringComparer.OrdinalIgnoreCase).ToList(); - profile.Skills = skills.ToList(); - profile.Sections = BuildSectionsFromClassifiedBlocks(classifiedBlocks); - - var averageConfidence = classifiedBlocks - .Select(block => block.Classification?.Confidence) - .Where(value => value.HasValue) - .Select(value => value!.Value) - .DefaultIfEmpty(0.74) - .Average(); - AnnotateStructuredCv(profile, "classifier", averageConfidence); - return StructuredCvProfileJson.Normalize(profile); - } - - private static StructuredCvJob? BuildJobFromClassifiedBlock(ClassifiedCvBlock block) - { - var classification = block.Classification; - if (classification is null) return null; - - var bullets = classification.Bullets is { Count: > 0 } - ? classification.Bullets.Where(bullet => !string.IsNullOrWhiteSpace(bullet)).Select(bullet => bullet.Trim()).ToList() - : SplitClassifierContent(block.OriginalBlock, 6); - - var job = new StructuredCvJob - { - Title = NullIfWhitespace(classification.Title), - Company = NullIfWhitespace(classification.Company), - Location = NullIfWhitespace(classification.Location), - Start = NullIfWhitespace(classification.Start), - End = NullIfWhitespace(classification.End), - IsCurrent = string.Equals(classification.End, "Present", StringComparison.OrdinalIgnoreCase) || string.Equals(classification.End, "Current", StringComparison.OrdinalIgnoreCase), - Bullets = bullets, - Skills = classification.Skills is { Count: > 0 } - ? classification.Skills.Where(skill => !string.IsNullOrWhiteSpace(skill)).Select(skill => skill.Trim()).ToList() - : SplitClassifierSkills(block.OriginalBlock) - }; - - return StructuredCvProfileJson.Normalize(new StructuredCvProfile { Jobs = new List<StructuredCvJob> { job } }).Jobs.FirstOrDefault(); - } - - private static StructuredCvEducation? BuildEducationFromClassifiedBlock(ClassifiedCvBlock block) - { - var classification = block.Classification; - if (classification is null) return null; - - var education = new StructuredCvEducation - { - Qualification = NullIfWhitespace(classification.Title), - Institution = NullIfWhitespace(classification.Company), - Location = NullIfWhitespace(classification.Location), - Start = NullIfWhitespace(classification.Start), - End = NullIfWhitespace(classification.End), - Details = classification.Bullets is { Count: > 0 } - ? classification.Bullets.Where(bullet => !string.IsNullOrWhiteSpace(bullet)).Select(bullet => bullet.Trim()).ToList() - : SplitClassifierContent(block.OriginalBlock, 5) - }; - - return StructuredCvProfileJson.Normalize(new StructuredCvProfile { Education = new List<StructuredCvEducation> { education } }).Education.FirstOrDefault(); - } - - private static List<string> SplitClassifierContent(string content, int limit) - { - return content - .Replace("\r\n", "\n") - .Split(new[] { '\n', '•' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) - .SelectMany(line => line.Contains(". ", StringComparison.Ordinal) - ? Regex.Split(line, @"(?<=[.!?])\s+") - : new[] { line }) - .Select(item => item.Trim().TrimStart('-', '•', '*', '+', ' ')) - .Where(item => item.Length > 2) - .Take(limit) - .Distinct(StringComparer.OrdinalIgnoreCase) - .ToList(); - } - - private static List<string> SplitClassifierSkills(string content) - { - return content - .Replace("\r\n", "\n") - .Split(new[] { '\n', ',', ';', '•' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) - .Select(item => item.Trim().TrimStart('-', '•', '*', '+', ' ')) - .Where(item => item.Length > 1 && item.Length <= 48 && !LooksLikeDateLikeValue(item) && !item.Contains('@')) - .Distinct(StringComparer.OrdinalIgnoreCase) - .ToList(); - } - - private static bool LooksLikeDateLikeValue(string value) - { - return Regex.IsMatch(value, @"^(?:\d{4}|(?:Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|August|Sep|Sept|September|Oct|October|Nov|November|Dec|December)\s+\d{4}|Present|Current)(?:\s*[-–]\s*(?:\d{4}|(?:Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|August|Sep|Sept|September|Oct|October|Nov|November|Dec|December)\s+\d{4}|Present|Current))?$", RegexOptions.IgnoreCase); - } - - private static void ApplyClassifierFieldMetadata(StructuredCvProfile profile, string key, string? value, ClassifiedCvBlock block, DateTimeOffset now) - { - if (string.IsNullOrWhiteSpace(value)) return; - - profile.Metadata.Fields[key] = new StructuredCvFieldMetadata - { - Confidence = block.Classification?.Confidence ?? 0.74, - Method = "classifier", - SourceSnippet = block.OriginalBlock.Length > 180 ? block.OriginalBlock[..180] : block.OriginalBlock, - SourceBlockId = $"block-{block.Index}", - ReviewState = string.Equals(block.SectionName, "General", StringComparison.OrdinalIgnoreCase) ? "needs-review" : "suggested", - LastUpdatedAtUtc = now, - }; - } - - private async Task<List<ClassifiedCvBlock>> ClassifyBlocksAsync(string parseSource, CancellationToken cancellationToken) - { - var blocks = Regex.Split(parseSource.Replace("\r\n", "\n"), @"\n\s*\n") - .Select(block => block.Trim()) - .Where(block => block.Length >= 24) - .ToList(); - - if (blocks.Count == 0) return new List<ClassifiedCvBlock>(); - - var results = new List<ClassifiedCvBlock>(); - for (var index = 0; index < blocks.Count; index++) - { - var block = blocks[index]; - var classification = await _cvAiClassifier.ClassifyBlockAsync(block, cancellationToken); - var sectionName = classification?.Section; - if (!string.IsNullOrWhiteSpace(sectionName) && SectionAliases.TryGetValue(sectionName, out var canonical)) - { - sectionName = canonical; - } - - if (string.IsNullOrWhiteSpace(sectionName) || string.Equals(sectionName, "Other", StringComparison.OrdinalIgnoreCase)) - { - sectionName = "General"; - } - - var content = block; - if (string.Equals(sectionName, "Work Experience", StringComparison.OrdinalIgnoreCase) && classification is not null) - { - var lines = new List<string>(); - if (!string.IsNullOrWhiteSpace(classification.Title)) lines.Add($"### {classification.Title.Trim()}"); - var endIsCurrent = string.Equals(classification.End, "Present", StringComparison.OrdinalIgnoreCase) || string.Equals(classification.End, "Current", StringComparison.OrdinalIgnoreCase); - var dateRange = FormatDateRangeForSection(classification.Start, classification.End, endIsCurrent); - var meta = string.Join(" | ", new[] { classification.Company, classification.Location, dateRange }.Where(value => !string.IsNullOrWhiteSpace(value))); - if (!string.IsNullOrWhiteSpace(meta)) lines.Add(meta); - if (classification.Bullets is not null) - { - lines.AddRange(classification.Bullets.Where(bullet => !string.IsNullOrWhiteSpace(bullet)).Select(bullet => $"- {bullet.Trim()}")); - } - if (lines.Count > 0) content = string.Join("\n", lines); - } - else if (string.Equals(sectionName, "Education", StringComparison.OrdinalIgnoreCase) && classification is not null) - { - var lines = new List<string>(); - if (!string.IsNullOrWhiteSpace(classification.Title)) lines.Add($"### {classification.Title.Trim()}"); - var dateRange = FormatDateRangeForSection(classification.Start, classification.End, false); - var meta = string.Join(" | ", new[] { classification.Company, classification.Location, dateRange }.Where(value => !string.IsNullOrWhiteSpace(value))); - if (!string.IsNullOrWhiteSpace(meta)) lines.Add(meta); - if (classification.Bullets is not null) - { - lines.AddRange(classification.Bullets.Where(bullet => !string.IsNullOrWhiteSpace(bullet)).Select(bullet => $"- {bullet.Trim()}")); - } - if (lines.Count > 0) content = string.Join("\n", lines); - } - else if (string.Equals(sectionName, "Skills", StringComparison.OrdinalIgnoreCase)) - { - var items = classification?.Skills is { Count: > 0 } - ? classification.Skills.Where(skill => !string.IsNullOrWhiteSpace(skill)).Select(skill => skill.Trim()).ToList() - : SplitClassifierSkills(block); - if (items.Count > 0) content = string.Join("\n", items); - } - else if (string.Equals(sectionName, "Professional Summary", StringComparison.OrdinalIgnoreCase)) - { - var items = classification?.Summary is { Count: > 0 } - ? classification.Summary.Where(line => !string.IsNullOrWhiteSpace(line)).Select(line => $"- {line.Trim()}") - : classification?.Bullets is { Count: > 0 } - ? classification.Bullets.Where(bullet => !string.IsNullOrWhiteSpace(bullet)).Select(bullet => $"- {bullet.Trim()}") - : Enumerable.Empty<string>(); - var materialized = items.ToList(); - if (materialized.Count > 0) content = string.Join("\n", materialized); - } - - results.Add(new ClassifiedCvBlock(index + 1, block, sectionName, content, classification)); - } - - return results; - } - - private static string? FormatDateRangeForSection(string? start, string? end, bool isCurrent) - { - if (string.IsNullOrWhiteSpace(start) && string.IsNullOrWhiteSpace(end)) return null; - if (string.IsNullOrWhiteSpace(start)) return end; - return $"{start} - {(isCurrent ? "Present" : end ?? "Present")}"; - } - - private async Task<string> MaybeReconstructStructuredCvAsync(string text, CancellationToken cancellationToken) - { - var normalized = text.Trim(); - var forceAiNormalizer = string.Equals(Environment.GetEnvironmentVariable("CV_FORCE_AI_NORMALIZER"), "true", StringComparison.OrdinalIgnoreCase); - if (forceAiNormalizer) - { - var forced = await _cvAiNormalizer.NormalizeAsync(normalized, cancellationToken); - if (!string.IsNullOrWhiteSpace(forced?.NormalizedText)) - { - return forced.NormalizedText.Trim(); - } - } - - var looksFlattened = LooksLikeFlattenedCvExtraction(normalized); - var hasRecoverableSignals = HasRecoverableSectionSignals(normalized); - - if (!looksFlattened && hasRecoverableSignals) - { - return normalized; - } - - var reconstructed = await _aiService.SummarizeSectionAsync( - "Reconstruct this CV text extracted from a PDF into a clean, readable master CV in markdown. Preserve facts only. Recover clear sections such as Contact, Professional Summary, Work Experience, Education, Skills, Languages, and Interests when present. Split contact details onto their own lines, turn noisy all-caps/spaced headings into normal headings, keep dates with the correct roles and employers, and remove layout/OCR artifacts. Do not invent employers, titles, dates, or metrics. Return only the reconstructed CV text.", - normalized, - 2800, - 900); - - var candidate = string.IsNullOrWhiteSpace(reconstructed) ? normalized : reconstructed.Trim(); - if (LooksLikeFlattenedCvExtraction(candidate) || !HasRecoverableSectionSignals(candidate)) - { - var aiNormalized = await _cvAiNormalizer.NormalizeAsync(normalized, cancellationToken); - if (!string.IsNullOrWhiteSpace(aiNormalized?.NormalizedText)) - { - return aiNormalized.NormalizedText.Trim(); - } - } - - return candidate; - } - - private static bool LooksLikeFlattenedCvExtraction(string text) - { - if (string.IsNullOrWhiteSpace(text)) return false; - - var normalized = text.Replace("\r\n", "\n"); - var lineCount = normalized.Split('\n').Count(line => !string.IsNullOrWhiteSpace(line)); - var spacedHeadingCount = Regex.Matches(normalized, @"\b(?:[A-Z]\s){3,}[A-Z]\b").Count; - var knownHeadingHits = SectionAliases.Keys.Count(alias => normalized.Contains(alias, StringComparison.OrdinalIgnoreCase)); - var bulletCount = Regex.Matches(normalized, @"[•●▪◦]").Count; - - return (lineCount <= 6 && normalized.Length >= 500) - || spacedHeadingCount >= 3 - || (knownHeadingHits >= 3 && lineCount <= 12) - || (normalized.Contains(" + ") && bulletCount > 0 && lineCount <= 10); - } - - private static bool LooksLikeNormalizedMarkdownCv(string text) - { - if (string.IsNullOrWhiteSpace(text)) return false; - return Regex.IsMatch(text, @"(?im)^#\s+(Contact|Professional Summary|Work Experience|Education|Skills|Languages|Interests|Projects|Certifications)\s*$"); - } - - private static StructuredCvProfile BuildStructuredCvFromNormalizedMarkdown(string text) - { - text = SeparateGluedDateAndTitle(text); - var sections = ParseSections(text) - .Select(section => new StructuredCvSection - { - Name = section.Name, - Content = section.Content, - WordCount = CountWords(section.Content), - }) - .ToList(); - - var profile = StructuredCvProfileJson.FromSections(sections); - profile.Sections = sections; - var workExperience = sections.FirstOrDefault(section => section.Name == "Work Experience")?.Content; - if (!string.IsNullOrWhiteSpace(workExperience)) - { - profile.Jobs.RemoveAll(job => (job.Title ?? string.Empty).StartsWith("Earlier roles", StringComparison.OrdinalIgnoreCase)); - foreach (var role in ParseEarlierRoles(workExperience)) - { - if (!profile.Jobs.Any(job => string.Equals(job.Title, role.Title, StringComparison.OrdinalIgnoreCase) - && string.Equals(job.Company, role.Company, StringComparison.OrdinalIgnoreCase))) profile.Jobs.Add(role); - } - } - - if (string.IsNullOrWhiteSpace(profile.Contact.FullName)) - { - profile.Contact.FullName = GuessFullName(text) ?? GuessFullNameFromEmail(profile.Contact.Email); - } - - var contactSection = sections.FirstOrDefault(section => section.Name == "Contact"); - profile.Contact.Location = PreferDetectedLocation(contactSection?.Content ?? text, profile.Contact.Location, profile.Contact.FullName); - profile.Summary = CondenseSummary(profile.Summary); - profile.Skills = OrderSkills(profile.Skills); - profile.Interests = CleanInterestItems(profile.Interests); - - foreach (var job in profile.Jobs) - { - job.Bullets = job.Bullets.Where(bullet => !bullet.Contains("Detail not specified", StringComparison.OrdinalIgnoreCase)).ToList(); - } - - foreach (var education in profile.Education) - { - education.Details = education.Details.Where(detail => !detail.Contains("Detail not specified", StringComparison.OrdinalIgnoreCase)).ToList(); - } - - return profile; - } - - private static List<string> CondenseSummary(List<string> summary) - { - if (summary.Count <= 1) return summary; - var joined = string.Join(" ", summary).Trim(); - return string.IsNullOrWhiteSpace(joined) ? new List<string>() : new List<string> { joined }; - } - - private static List<string> OrderSkills(List<string> skills) - { - return skills - .Select(CleanSkillGroupPrefix) - .Where(skill => skill.Length > 0) - .Distinct(StringComparer.OrdinalIgnoreCase) - .OrderBy(skill => skill, StringComparer.OrdinalIgnoreCase) - .ToList(); - } - - private static string CleanSkillGroupPrefix(string skill) - => Regex.Replace(skill.Trim(), @"^(?:Development|DevOps(?:\s*&\s*Infrastructure)?|Infrastructure|Practices|Tools|Technologies|Technical Skills)\s*:\s*", string.Empty, RegexOptions.IgnoreCase).Trim(); - - private static string SeparateGluedDateAndTitle(string text) - => Regex.Replace(text, @"(?<date>\b\d{4}\s*[-–—]\s*(?:\d{4}|Present|Current))(?<title>[\p{L}][^\r\n]*)", "${title}\n${date}", RegexOptions.IgnoreCase); - - private static string RepairKnownMojibake(string text) - => text - .Replace("ø", "ø", StringComparison.Ordinal) - .Replace("Ø", "Ø", StringComparison.Ordinal) - .Replace("æ", "æ", StringComparison.Ordinal) - .Replace("Æ", "Æ", StringComparison.Ordinal) - .Replace("Ã¥", "å", StringComparison.Ordinal) - .Replace("Ã…", "Å", StringComparison.Ordinal) - .Replace("–", "–", StringComparison.Ordinal) - .Replace("—", "—", StringComparison.Ordinal) - .Replace("’", "’", StringComparison.Ordinal) - .Replace("“", "“", StringComparison.Ordinal) - .Replace("”", "”", StringComparison.Ordinal); - - private static List<string> CleanInterestItems(List<string> interests) - { - return interests - .Where(item => !item.Contains("linkedin", StringComparison.OrdinalIgnoreCase) - && !item.Contains("realtor", StringComparison.OrdinalIgnoreCase) - && !Regex.IsMatch(item, @"https?://|www\.", RegexOptions.IgnoreCase)) - .ToList(); - } - - private static string? CanonicalizeSectionHeading(string line) - { - if (string.IsNullOrWhiteSpace(line)) return null; - - var normalized = line.Trim(); - if (normalized.StartsWith("#", StringComparison.Ordinal)) - { - normalized = normalized.TrimStart('#').Trim(); - } - - normalized = normalized.TrimEnd(':').Trim(); - if (normalized.Length == 0 || normalized.Length > 60) return null; - if (normalized.Contains('.') || normalized.Contains(" ")) return null; - - return SectionAliases.TryGetValue(normalized, out var canonical) ? canonical : null; - } - - private static bool HasRecoverableSectionSignals(string text) - { - var sections = ParseSections(text); - return sections.Any(section => !string.Equals(section.Name, "General", StringComparison.OrdinalIgnoreCase)) - || Regex.IsMatch(text, @"(?im)^\s*(Contact|Professional Summary|Summary|Work Experience|Experience|Education|Skills|Languages|Interests)\s*:?") - || Regex.IsMatch(text, @"(?im)^\s*#\s*(Contact|Professional Summary|Summary|Work Experience|Experience|Education|Skills|Languages|Interests)"); - } - - private static async Task<string> ExtractTextAsync(IFormFile file, string extension) - { - if (string.Equals(extension, ".txt", StringComparison.OrdinalIgnoreCase) || string.Equals(extension, ".md", StringComparison.OrdinalIgnoreCase)) - { - using var stream = file.OpenReadStream(); - using var reader = new StreamReader(stream, Encoding.UTF8, detectEncodingFromByteOrderMarks: true); - return (await reader.ReadToEndAsync()).Trim(); - } - - await using var memory = new MemoryStream(); - await file.CopyToAsync(memory); - var bytes = memory.ToArray(); - - if (string.Equals(extension, ".pdf", StringComparison.OrdinalIgnoreCase)) - { - var raw = Encoding.Latin1.GetString(bytes); - var textMatches = Regex.Matches(raw, @"\((.*?)\)Tj", RegexOptions.Singleline) - .Select(match => match.Groups[1].Value) - .Concat(Regex.Matches(raw, @"\[(.*?)\]TJ", RegexOptions.Singleline) - .SelectMany(match => Regex.Matches(match.Groups[1].Value, @"\((.*?)\)", RegexOptions.Singleline).Select(x => x.Groups[1].Value))) - .Where(value => !string.IsNullOrWhiteSpace(value)) - .Select(value => Regex.Unescape(value)) - .ToList(); - - var joined = textMatches.Count > 0 ? string.Join(" ", textMatches) : raw; - var scrubbed = Regex.Replace(joined, @"[\x00-\x08\x0B\x0C\x0E-\x1F]", " "); - return Regex.Replace(scrubbed, @"\s+", " ").Trim(); - } - - if (string.Equals(extension, ".docx", StringComparison.OrdinalIgnoreCase)) - { - using var archive = new System.IO.Compression.ZipArchive(new MemoryStream(bytes), System.IO.Compression.ZipArchiveMode.Read, leaveOpen: false); - var entry = archive.GetEntry("word/document.xml"); - if (entry is null) return string.Empty; - using var entryStream = entry.Open(); - using var reader = new StreamReader(entryStream, Encoding.UTF8); - var xml = await reader.ReadToEndAsync(); - var withoutTags = Regex.Replace(xml, "<[^>]+>", " "); - var decoded = System.Net.WebUtility.HtmlDecode(withoutTags) ?? string.Empty; - return Regex.Replace(decoded, @"\s+", " ").Trim(); - } - - return string.Empty; - } } diff --git a/docs/implementation-roadmap.md b/docs/implementation-roadmap.md index 88cfd06..e0f9a45 100644 --- a/docs/implementation-roadmap.md +++ b/docs/implementation-roadmap.md @@ -96,7 +96,7 @@ Goal: one professional source of truth that can actually feed outputs. ## Phase 4 — CV Builder Goal: `Content Tab → Customise Tab → Preview → Export` (guide `:312`). -**This is the largest item in the plan.** The existing inert tab makes it look nearly done; it is not started. +**Completed 2026-07-30.** The builder now covers content, customisation, preview, export, variants, and public sharing. > **Foundation SHIPPED 2026-07-18** (commits `a3e18e4` backend, `158dd02` frontend). Data-driven > theme engine + variant model + 3-tab builder + live preview + public CV, all consuming the master @@ -106,13 +106,9 @@ Goal: `Content Tab → Customise Tab → Preview → Export` (guide `:312`). > 4.3 ✅ 8 themes as data. 4.4 ✅ Content tab reorder/hide/rename + custom sections (up/down > controls with keyboard support; **native drag-and-drop deferred** — no dnd dependency added yet). > 4.5 ✅ Customise tab (theme, accent, fonts, density, page size, photo/icons/page-numbers). 4.6 ✅ -> live preview (server render on a 350 ms debounce; **not yet client-side** — see below). 4.7 ✅ PDF -> export wired into the builder. Plus: autosave + version history/restore, AI-assist (suggestion-only), -> public CV at `/cv/{slug}`. **Remaining polish:** 4.8 split `ProfileCvController`; client-side preview -> (4.6 currently server round-trip); native drag-and-drop; rich-text bullets; PDF page-number footer; -> per-item override UI in the Content tab (the API + resolver support it; the editor exposes section- -> level controls). **Known limitation:** external direct loads of `/cv/{slug}` bounce to root (SPA -> deep-link issue, app-wide) — fix in the frontend static-export/routing config. +> live preview (server render on a 350 ms debounce for export fidelity). 4.7 ✅ PDF export wired into +> the builder. Plus: autosave + version history/restore, AI-assist (suggestion-only), and public CV at +> `/cv/{slug}`. > > **Phase 4.5 — Builder Polish SHIPPED 2026-07-18** (commits `585047d`, `e3b255f`, `582c4e0`). > Public-CV deep links fixed (optional catch-all `app/[[...slug]]`; direct nav/refresh/shared links @@ -123,19 +119,19 @@ Goal: `Content Tab → Customise Tab → Preview → Export` (guide `:312`). > page navigation + page-break indicators, "updating" state. Unsaved/Saving/Saved indicator, loading > skeletons, better empty states, ATS-friendly theme badge, a11y (ARIA labels, keyboard theme cards, > focus rings), print-quality page-break CSS, AA contrast fix. Docs: `cv-builder.md`, -> `cv-theme-engine.md`. **Still open:** PDF page-number footer (Playwright `footerTemplate`); -> client-side preview (kept server-rendered for fidelity); `ProfileCvController` split. +> `cv-theme-engine.md`. Server-rendered preview remains intentional for export fidelity; the controller was +> split into endpoint, pipeline, and parsing partials on 2026-07-30. | # | Task | Priority | Difficulty | Dependencies | Expected value | |---|---|---|---|---|---| -| 4.1 | **Design the `CvTheme` model** — layout, columns, header position, font family, base size + per-element deltas, spacing, margins, accent + application targets, icon style, photo settings | **P1** | **M** | 3.4 | **The keystone.** Everything else in Phase 4 depends on themes being *data*. Modelled on FlowCV's proven control set (report §7), trimmed to ~12 controls per the guide's "avoid excessive configuration". | -| 4.2 | **Replace `CvTemplateRenderer` with one parameterized renderer** | **P1** | **L** | 4.1 | Current code is a C# `switch` over 6 hardcoded HTML-string functions with `roundedPhoto`/`curvedHeader` booleans (`Services/CvTemplateRenderer.cs:22`). **A structural dead end — do not extend it.** | -| 4.3 | **Seed 3–5 themes as theme documents** — ATS Professional, Modern Professional, Creative | **P1** | **M** | 4.2 | The guide's explicit target. Cheap once 4.1/4.2 land; impossible before. | -| 4.4 | **Content tab** — section add/remove/reorder, entry editing, drag-and-drop **with keyboard support** | **P1** | **L** | 3.4 | FlowCV's keyboard drag affordances are worth matching (report §7). | -| 4.5 | **Customise tab** — the ~12 controls from 4.1 | **P1** | **M** | 4.1, 4.2 | Currently **nothing** is customisable. | -| 4.6 | **Live client-side preview** | **P1** | **L** | 4.2 | Today: server round-trip. FlowCV: continuous, side-by-side. Hardest piece — the renderer must run client-side or stream fast enough to feel live. | -| 4.7 | **Wire export into the builder** | **P2** | **S** | 4.6 | `POST /profile-cv/export-pdf` + Playwright already work. Make Download persistent, not a mode. | -| 4.8 | **Split `ProfileCvController` (2249 lines)** | **P2** | **M** | — | Do it while working here, not as a standalone refactor. | +| 4.1 | ✅ **DONE** — Design the `CvTheme` model — layout, columns, header position, font family, base size + per-element deltas, spacing, margins, accent + application targets, icon style, photo settings | **P1** | **M** | 3.4 | **The keystone.** Everything else in Phase 4 depends on themes being *data*. Modelled on FlowCV's proven control set (report §7), trimmed to ~12 controls per the guide's "avoid excessive configuration". | +| 4.2 | ✅ **DONE** — Replace `CvTemplateRenderer` with one parameterized renderer | **P1** | **L** | 4.1 | Current code is a C# `switch` over 6 hardcoded HTML-string functions with `roundedPhoto`/`curvedHeader` booleans (`Services/CvTemplateRenderer.cs:22`). **A structural dead end — do not extend it.** | +| 4.3 | ✅ **DONE** — Seed 3–5 themes as theme documents — ATS Professional, Modern Professional, Creative | **P1** | **M** | 4.2 | The guide's explicit target. Cheap once 4.1/4.2 land; impossible before. | +| 4.4 | ✅ **DONE** — Content tab — section add/remove/reorder, entry editing, drag-and-drop **with keyboard support** | **P1** | **L** | 3.4 | FlowCV's keyboard drag affordances are worth matching (report §7). | +| 4.5 | ✅ **DONE** — Customise tab — the ~12 controls from 4.1 | **P1** | **M** | 4.1, 4.2 | Currently **nothing** is customisable. | +| 4.6 | ✅ **DONE** — Live server-rendered preview | **P1** | **L** | 4.2 | Today: server round-trip. FlowCV: continuous, side-by-side. Hardest piece — the renderer must run client-side or stream fast enough to feel live. | +| 4.7 | ✅ **DONE** — Wire export into the builder | **P2** | **S** | 4.6 | `POST /profile-cv/export-pdf` + Playwright already work. Make Download persistent, not a mode. | +| 4.8 | ✅ **DONE (2026-07-30)** — Split `ProfileCvController` (split from 2,379 lines) | **P2** | **M** | — | Do it while working here, not as a standalone refactor. | | 4.9 | ~~Research Reactive Resume / Novoresume / ElegantCV~~ ✅ **ALREADY DONE** — on `feature/career-workspace`: `docs/cv-builder-competitor-deep-research.md` (327 lines, 8 teardowns incl. Novoresume + Reactive Resume, feature matrix, business-model analysis). **Recover it; do not redo it.** | **P1** | **XS** | 1.9 | Its conclusions independently match this plan's §7/§10 reasoning — structured-form + live preview beats canvas; client-side preview is a hard requirement; themes must be declarative data. It also carries the pricing intelligence Phase 7 needs (Resume.io's F BBB rating for billing traps; Novoresume blocking re-download of already-paid CVs), which independently supports the "never gate on count" decision. | --- @@ -194,7 +190,7 @@ Goal: commercialise. Last, per the guide's "do not over-engineer before needed. | # | Task | Priority | Difficulty | Dependencies | Expected value | |---|---|---|---|---|---| | 7.1 | **Open registration + CAPTCHA** | **P2** | **M** | 2.4, 7.3 | Registration is 403 by default; **no CAPTCHA exists** (verified). Rate limiting alone is not enough for public signup. | -| 7.2 | **Plan / tier / entitlement model** — capability flags (`advancedAi`, `premiumThemes`, `automation`, `analytics`, `storageBytes`), not counters. | **P3** | **M** | none | No concept of a plan exists anywhere. Shape it around the decided split so the free tier stays genuinely useful. | +| 7.2 | **Plan / tier / entitlement model — capability flags (`advancedAi`, `premiumThemes`, `automation`, `analytics`, `storageBytes`), not counters. | **P3** | **M** | none | No concept of a plan exists anywhere. Shape it around the decided split so the free tier stays genuinely useful. | | 7.3 | **Usage quotas — AI + storage only** | **P3** | **M** | 5.2, 7.2 | **Do not open registration before this lands.** AI and storage are unmetered and unbounded; these are real cost, so they are legitimate limits. Job/CV counts are not. | | 7.4 | **Storage limits + attachment caps** | **P3** | **S** | 7.2 | The "more storage" premium lever. | | 7.5 | **Stripe billing** | **P3** | **L** | 7.2 | Still blocked on **Stripe keys** — the only remaining hard blocker. Tiers are now decided. |