using FlatRender.ContentSvc.Application.Services;
using FlatRender.ContentSvc.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace FlatRender.ContentSvc.Controllers;

/// <summary>Import a scanned AE project structure into a project's scenes/elements/colours.</summary>
[ApiController]
[Route("v1/projects")]
public class AepImportController(AepImportService svc) : ControllerBase
{
    /// <summary>Dry run — returns the diff (added/changed/removed) without writing.</summary>
    [Authorize(Roles = "Admin")]
    [HttpPost("{id:guid}/scan/preview")]
    public async Task<IActionResult> Preview(Guid id, [FromBody] ScanImportRequest req)
    {
        if (req?.Scan is null) return BadRequest(new { message = "scan is required" });
        return Ok(await svc.PreviewAsync(id, req.Scan));
    }

    /// <summary>Apply the scan, merging into the project (matched items refreshed, new added).</summary>
    [Authorize(Roles = "Admin")]
    [HttpPost("{id:guid}/scan/apply")]
    public async Task<IActionResult> Apply(Guid id, [FromBody] ScanImportRequest req)
    {
        if (req?.Scan is null) return BadRequest(new { message = "scan is required" });
        return Ok(await svc.ApplyAsync(id, req.Scan, req.Options ?? new ScanApplyOptions()));
    }
}
