using IdokladSdk.Models.Contact;
using Microsoft.AspNetCore.Mvc;
using Idoklad.Services;
namespace Idoklad.Controllers;
///
/// Contacts (customers/suppliers) agenda.
///
/// All endpoints require iDoklad credentials. Provide them as request headers
/// (X-ClientId, X-ClientSecret, X-ApplicationId) or rely on the service
/// environment defaults. See the Swagger description for details.
///
[ApiController]
[Route("contacts")]
[Produces("application/json")]
[Tags("Contacts")]
public sealed class ContactsController : ControllerBase
{
private readonly ContactsService _service;
public ContactsController(ContactsService service) => _service = service;
///
/// List contacts (paged, optionally filtered and sorted).
///
///
/// Optional iDoklad-style filter, e.g. (IdentificationNumber~eq~12345678) or
/// (CompanyName~ct~s.r.o.). Operators: eq, neq, gt, gte, lt, lte, ct, nct.
///
/// How multiple conditions combine: and (default) or or.
/// Optional sort, e.g. CompanyName~asc.
[HttpGet]
public async Task List(
[FromQuery] int page = 1,
[FromQuery] int pageSize = 20,
[FromQuery] string? filter = null,
[FromQuery] string? filtertype = null,
[FromQuery] string? sort = null,
CancellationToken ct = default)
=> Ok(await _service.ListAsync(page, pageSize, filter, filtertype, sort, ct));
/// Get a contact detail by id.
[HttpGet("{id:int}")]
public async Task Detail(int id, CancellationToken ct)
=> Ok(await _service.DetailAsync(id, ct));
/// Get a pre-filled default contact model for creating a new contact.
[HttpGet("default")]
public async Task Default(CancellationToken ct)
=> Ok(await _service.DefaultAsync(ct));
/// Create a new contact.
[HttpPost]
public async Task Create([FromBody] ContactPostModel model, CancellationToken ct)
=> Ok(await _service.CreateAsync(model, ct));
/// Update an existing contact (the model id identifies the contact).
[HttpPatch]
public async Task Update([FromBody] ContactPatchModel model, CancellationToken ct)
=> Ok(await _service.UpdateAsync(model, ct));
/// Delete a contact by id.
[HttpDelete("{id:int}")]
public async Task Delete(int id, CancellationToken ct)
=> Ok(await _service.DeleteAsync(id, ct));
}