using Api.Services.Contracts; using CvMatcher.Models.Requests; using CvMatcher.Models.Responses; using Microsoft.AspNetCore.Mvc; using Common.Responses; using Swashbuckle.AspNetCore.Annotations; namespace Api.Controllers; /// /// Internal endpoints for managing one-click job-search tokens and sessions. /// Routes are prefixed with api/cv/job-search. Protected by the internal API key middleware — not reachable from the public internet. /// [ApiController] [Route("api/cv/job-search")] public sealed class JobSearchController : ControllerBase { private readonly IJobTokenService _tokenService; private readonly ILogger _logger; public JobSearchController(IJobTokenService tokenService, ILogger logger) { _tokenService = tokenService; _logger = logger; } /// /// Creates a one-time job-search token linked to a CV document and email address. /// Called by api immediately after a successful CV match when an email is provided. /// The token is embedded in the job-search link sent to the user's email. /// /// The CV document ID and the recipient email address. /// Cancellation token. /// /// 200 OK with a containing the generated token ID; /// 400 Bad Request if CvDocumentId or Email is missing; /// 500 Internal Server Error if token creation fails. /// [HttpPost("token")] [SwaggerOperation(Summary = "Create job search token", Description = "Creates a one-time token that lets the user start a background job search by clicking the link in their match email.")] [SwaggerResponse(StatusCodes.Status200OK, "Token created successfully", typeof(CreateJobSearchTokenResponse))] [SwaggerResponse(StatusCodes.Status400BadRequest, "CvDocumentId or Email missing", typeof(ErrorResponse))] [SwaggerResponse(StatusCodes.Status500InternalServerError, "Token creation failed", typeof(ErrorResponse))] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status500InternalServerError)] public async Task> CreateToken( [FromBody] CreateJobSearchTokenRequest request, CancellationToken ct) { try { if (string.IsNullOrWhiteSpace(request.CvDocumentId) || string.IsNullOrWhiteSpace(request.Email)) return BadRequest(new ErrorResponse { Error = "CvDocumentId and Email are required.", Code = "invalid_request" }); var tokenId = await _tokenService.CreateTokenAsync(request.CvDocumentId, request.Email, request.Language, request.Keywords, ct); return Ok(new CreateJobSearchTokenResponse { TokenId = tokenId }); } catch (Exception ex) { _logger.LogError(ex, "Failed to create job search token."); return StatusCode(StatusCodes.Status500InternalServerError, new ErrorResponse { Error = "Failed to create token.", Code = "token_create_failed" }); } } /// /// Validates the one-time token, marks it as used, and enqueues a JobSearchSession with status Pending. /// Called by api when the user clicks the job-search link in their match email. /// The cv-search-job worker picks up the pending session and runs the search. /// /// The UUID token extracted from the email link. /// Cancellation token. /// /// 200 OK with a whose Status is one of /// Started, AlreadyUsed, or Expired; /// 500 Internal Server Error if the session cannot be created. /// [HttpPost("token/{tokenId}/start")] [SwaggerOperation(Summary = "Start job search", Description = "Validates the one-time token and creates a Pending job search session for the cv-search-job worker to process.")] [SwaggerResponse(StatusCodes.Status200OK, "Search status returned (Started, AlreadyUsed, or Expired)", typeof(StartJobSearchResponse))] [SwaggerResponse(StatusCodes.Status500InternalServerError, "Session creation failed", typeof(ErrorResponse))] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status500InternalServerError)] public async Task> Start(string tokenId, CancellationToken ct) { try { var status = await _tokenService.TriggerStartAsync(tokenId, ct); return Ok(new StartJobSearchResponse { Status = status }); } catch (Exception ex) { _logger.LogError(ex, "Failed to start job search for token {TokenId}.", tokenId); return StatusCode(StatusCodes.Status500InternalServerError, new ErrorResponse { Error = "Failed to start search.", Code = "start_failed" }); } } }