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

namespace Idoklad.Controllers;

/// <summary>
/// Contacts (customers/suppliers) agenda.
///
/// All endpoints require iDoklad credentials. Provide them as request headers
/// (<c>X-ClientId</c>, <c>X-ClientSecret</c>, <c>X-ApplicationId</c>) or rely on the service
/// environment defaults. See the Swagger description for details.
/// </summary>
[ApiController]
[Route("contacts")]
[Produces("application/json")]
[Tags("Contacts")]
public sealed class ContactsController : ControllerBase
{
    private readonly ContactsService _service;

    public ContactsController(ContactsService service) => _service = service;

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

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

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

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