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

namespace Idoklad.Controllers;

/// <summary>
/// Received (incoming) invoices agenda. Requires iDoklad credentials (headers or environment
/// defaults).
/// </summary>
[ApiController]
[Route("received-invoices")]
[Produces("application/json")]
[Tags("ReceivedInvoices")]
public sealed class ReceivedInvoicesController : ControllerBase
{
    private readonly ReceivedInvoicesService _service;

    public ReceivedInvoicesController(ReceivedInvoicesService service) => _service = service;

    /// <summary>List received 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 a received 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 received invoice.</summary>
    [HttpGet("default")]
    public async Task<IActionResult> Default(CancellationToken ct)
        => Ok(await _service.DefaultAsync(ct));

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

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

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