using IdokladSdk.Enums;
using IdokladSdk.Models.Attachment;
using IdokladSdk.Models.Webhook;
using Microsoft.AspNetCore.Mvc;
using Idoklad.Services;
namespace Idoklad.Controllers;
///
/// Integration / utility agendas: webhooks, notifications, change log, registered sales,
/// unpaired documents, attachments and system code books. Requires iDoklad credentials.
///
[ApiController]
[Produces("application/json")]
[Tags("Integration")]
public sealed class IntegrationController : ControllerBase
{
private readonly IntegrationService _service;
public IntegrationController(IntegrationService service) => _service = service;
// ---- Webhooks ----
/// List configured webhooks (paged).
[HttpGet("webhooks")]
public async Task ListWebhooks(int page = 1, int pageSize = 20, CancellationToken ct = default)
=> Ok(await _service.ListWebhooksAsync(page, pageSize, ct));
/// Get a webhook detail by id.
[HttpGet("webhooks/{id:int}")]
public async Task WebhookDetail(int id, CancellationToken ct)
=> Ok(await _service.WebhookDetailAsync(id, ct));
/// Create (register) a new webhook subscription.
[HttpPost("webhooks")]
public async Task CreateWebhook([FromBody] AgendaWebhookSettingsPostModel model, CancellationToken ct)
=> Ok(await _service.CreateWebhookAsync(model, ct));
/// Delete a webhook by id.
[HttpDelete("webhooks/{id:int}")]
public async Task DeleteWebhook(int id, CancellationToken ct)
=> Ok(await _service.DeleteWebhookAsync(id, ct));
// ---- Notifications ----
/// List notifications (paged).
[HttpGet("notifications")]
public async Task ListNotifications(int page = 1, int pageSize = 20, CancellationToken ct = default)
=> Ok(await _service.ListNotificationsAsync(page, pageSize, ct));
/// Delete (dismiss) a notification by id.
[HttpDelete("notifications/{id:int}")]
public async Task DeleteNotification(int id, CancellationToken ct)
=> Ok(await _service.DeleteNotificationAsync(id, ct));
// ---- Change log ----
/// List the agenda change log (paged).
[HttpGet("logs")]
public async Task ListLog(int page = 1, int pageSize = 20, CancellationToken ct = default)
=> Ok(await _service.ListLogAsync(page, pageSize, ct));
// ---- Registered sales (EET) ----
/// List registered sales / EET records (paged).
[HttpGet("registered-sales")]
public async Task ListRegisteredSales(int page = 1, int pageSize = 20, CancellationToken ct = default)
=> Ok(await _service.ListRegisteredSalesAsync(page, pageSize, ct));
/// Get a pre-filled default model for creating a new registered sale.
[HttpGet("registered-sales/default")]
public async Task RegisteredSaleDefault(CancellationToken ct)
=> Ok(await _service.RegisteredSaleDefaultAsync(ct));
// ---- Unpaired documents ----
/// List unpaired documents for a movement type and pairing document type.
[HttpGet("unpaired-documents/{movementType}/{pairingDocumentType}")]
public async Task ListUnpairedDocuments(
MovementType movementType, PairingDocumentType pairingDocumentType, int page = 1, int pageSize = 20, CancellationToken ct = default)
=> Ok(await _service.ListUnpairedDocumentsAsync(movementType, pairingDocumentType, page, pageSize, ct));
// ---- Attachments ----
/// Get a single attachment by its id.
[HttpGet("attachments/{attachmentId:int}")]
public async Task GetAttachment(int attachmentId, bool compressed = false, CancellationToken ct = default)
=> Ok(await _service.GetAttachmentAsync(attachmentId, compressed, ct));
/// Get all attachments of a document.
[HttpGet("attachments/document/{documentId:int}/{documentType}")]
public async Task GetDocumentAttachments(
int documentId, AttachmentDocumentType documentType, bool compressed = false, CancellationToken ct = default)
=> Ok(await _service.GetDocumentAttachmentsAsync(documentId, documentType, compressed, ct));
/// Upload an attachment for a document.
[HttpPost("attachments")]
public async Task UploadAttachment([FromBody] AttachmentUploadModel model, CancellationToken ct)
=> Ok(await _service.UploadAttachmentAsync(model, ct));
/// Delete a single attachment by its id.
[HttpDelete("attachments/{attachmentId:int}")]
public async Task DeleteAttachment(int attachmentId, CancellationToken ct)
=> Ok(await _service.DeleteAttachmentAsync(attachmentId, ct));
/// Delete all attachments of a document.
[HttpDelete("attachments/document/{documentId:int}/{documentType}")]
public async Task DeleteDocumentAttachments(int documentId, AttachmentDocumentType documentType, CancellationToken ct)
=> Ok(await _service.DeleteDocumentAttachmentsAsync(documentId, documentType, ct));
// ---- System code books ----
/// Get the iDoklad system code books.
[HttpGet("system/code-books")]
public async Task CodeBooks(CancellationToken ct)
=> Ok(await _service.CodeBooksAsync(ct));
/// Get changes to the code books since the given timestamp.
[HttpGet("system/code-books/changes")]
public async Task CodeBooksChanges([FromQuery] DateTime lastCheck, CancellationToken ct = default)
=> Ok(await _service.CodeBooksChangesAsync(lastCheck, ct));
}