using JobTrackerApi.Models;
using JobTrackerApi.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace JobTrackerApi.Controllers;

// Phase 5 Milestone 1 — one aggregate read for the Application Workspace overview, so the page loads
// from a single call instead of fanning out. Read-only; owns no data.
// docs/architecture/application-workspace.md.
[ApiController]
[Route("api/jobapplications/{jobId:int}/workspace")]
[Authorize(AuthenticationSchemes = "local")]
public sealed class ApplicationWorkspaceController : ControllerBase
{
    private readonly UserManager<ApplicationUser> _users;
    private readonly IApplicationWorkspaceService _workspace;

    public ApplicationWorkspaceController(UserManager<ApplicationUser> users, IApplicationWorkspaceService workspace)
    {
        _users = users;
        _workspace = workspace;
    }

    [HttpGet]
    public async Task<ActionResult<WorkspaceOverviewDto>> GetOverview(int jobId, CancellationToken ct)
    {
        var user = await _users.GetUserAsync(User);
        if (user is null) return Unauthorized();
        var overview = await _workspace.GetOverviewAsync(user.Id, jobId, ct);
        return overview is null ? NotFound() : Ok(overview);
    }
}
