using IdokladSdk.Models.IssuedInvoice;
using Microsoft.AspNetCore.Mvc;
using Idoklad.Services;

namespace Idoklad.Controllers;

/// <summary>
/// Issued (outgoing) invoices agenda. Requires iDoklad credentials (headers or environment
/// defaults).
/// </summary>
[ApiController]
[Route("issued-invoices")]
[Produces("application/json")]
[Tags("IssuedInvoices")]
public sealed class IssuedInvoicesController : ControllerBase
{
    private readonly IssuedInvoicesService _service;

    public IssuedInvoicesController(IssuedInvoicesService service) => _service = service;

    /// <summary>List issued invoices (paged).</summary>
    [HttpGet]
    public async Task<IActionResult> List([FromQuery] int page = 1, [FromQuery] int pageSize = 20, CancellationToken ct = default)
        => Ok(await _service.ListAsync(page, pageSize, ct));

    /// <summary>Get an issued invoice detail by id.</summary>
    [HttpGet("{id:int}")]
    public async Task<IActionResult> Detail(int id, CancellationToken ct)
        => Ok(await _service.DetailAsync(id, ct));

    /// <summary>Get a pre-filled default model for creating a new issued invoice.</summary>
    [HttpGet("default")]
    public async Task<IActionResult> Default(CancellationToken ct)
        => Ok(await _service.DefaultAsync(ct));

    /// <summary>Create a new issued invoice.</summary>
    [HttpPost]
    public async Task<IActionResult> Create([FromBody] IssuedInvoicePostModel model, CancellationToken ct)
        => Ok(await _service.CreateAsync(model, ct));

    /// <summary>Update an existing issued invoice (the model id identifies the invoice).</summary>
    [HttpPatch]
    public async Task<IActionResult> Update([FromBody] IssuedInvoicePatchModel model, CancellationToken ct)
        => Ok(await _service.UpdateAsync(model, ct));

    /// <summary>Create a copy (draft) of an existing issued invoice.</summary>
    [HttpPost("{id:int}/copy")]
    public async Task<IActionResult> Copy(int id, CancellationToken ct)
        => Ok(await _service.CopyAsync(id, ct));

    /// <summary>Delete an issued invoice by id.</summary>
    [HttpDelete("{id:int}")]
    public async Task<IActionResult> Delete(int id, CancellationToken ct)
        => Ok(await _service.DeleteAsync(id, ct));
}
