using JobTrackerApi.Data; using JobTrackerApi.Models; using JobTrackerApi.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; namespace JobTrackerApi.Controllers; // Phase 3: the master career profile API. /career reads and writes the structured profile through // here. The relational children (via CareerProfileService) are the source of truth; the // ApplicationUser.ProfileCvStructureJson blob is kept in sync as a derived projection so the legacy // read paths (CV rendering, tailoring, match-score) keep working. See // docs/architecture/career-profile-model.md. [ApiController] [Route("api/career/profile")] [Authorize] public sealed class CareerProfileController : ControllerBase { private readonly UserManager _users; private readonly ICareerProfileService _career; private readonly JobTrackerContext _db; public CareerProfileController(UserManager users, ICareerProfileService career, JobTrackerContext db) { _users = users; _career = career; _db = db; } /// The master career profile, assembled from the relational children. [HttpGet] public async Task> Get(CancellationToken cancellationToken) { var user = await _users.GetUserAsync(User); if (user is null) return Unauthorized(); var profile = await _career.LoadStructuredAsync(user.Id, cancellationToken); return Ok(new CareerProfileDto(profile, CareerCompleteness.Evaluate(profile))); } /// /// Replaces the master career profile. Persists the relational children + an append-only /// version, then serializes the result back into the ProfileCvStructureJson projection so the /// legacy readers stay consistent. Identity fields are untouched (they belong to /profile). /// [HttpPut] [Authorize(AuthenticationSchemes = "local")] public async Task> Put([FromBody] StructuredCvProfile? request, CancellationToken cancellationToken) { var user = await _users.GetUserAsync(User); if (user is null) return StatusCode(501, "The career profile can only be edited on local accounts."); var profile = StructuredCvProfileJson.Normalize(request); var error = CareerProfileValidator.Validate(profile); if (error is not null) return BadRequest(error); var saved = await _career.SaveVersionAsync(user.Id, profile, "manual", cancellationToken); // Keep the derived projection in sync for legacy readers. user.ProfileCvStructureJson = StructuredCvProfileJson.Serialize(saved); var res = await _users.UpdateAsync(user); if (!res.Succeeded) return BadRequest(string.Join("; ", res.Errors.Select(e => e.Description))); return Ok(new CareerProfileDto(saved, CareerCompleteness.Evaluate(saved))); } /// Just the completeness scorecard, for the /career overview. [HttpGet("completeness")] public async Task> Completeness(CancellationToken cancellationToken) { var user = await _users.GetUserAsync(User); if (user is null) return Unauthorized(); var profile = await _career.LoadStructuredAsync(user.Id, cancellationToken); return Ok(CareerCompleteness.Evaluate(profile)); } } public sealed record CareerProfileDto(StructuredCvProfile Profile, CareerCompletenessDto Completeness); public sealed record CareerCompletenessDto(int Percent, List Missing, List Sections); public sealed record CareerSectionStatusDto(string Key, string Label, bool Complete, int Count);