#nullable enable
namespace Baya.Application.Models.Messaging;

/// <summary>A ticket row in a list (no messages). Used by <c>ListMyTickets</c> and the admin queue.
/// <c>LastMessageAt</c> (last non-internal activity) drives inbox sorting; <c>UnreadCount</c> is the caller's
/// unread non-internal messages from others (0 for the admin queue).</summary>
public record TicketSummaryDto(
    long Id,
    string ReferenceCode,
    string? Subject,
    string Status,
    string Category,
    long? BookingId,
    long? RefundId,
    DateTimeOffset CreatedAt,
    DateTimeOffset? LastMessageAt,
    int UnreadCount);

/// <summary>One message in a thread. <c>IsInternal</c> is only ever <c>true</c> in the admin view — the user
/// view never contains an internal message (it is stripped in the query projection).</summary>
public record TicketMessageDto(
    long Id,
    int SenderId,
    string Body,
    bool IsInternal,
    DateTimeOffset SentAt);

/// <summary>An active participant on a thread.</summary>
public record TicketParticipantDto(
    int UserId,
    string? RoleOnTicket);

/// <summary>The full ordered thread the caller may see. In the <b>user</b> view internal messages are absent;
/// the <b>admin</b> view returns them.</summary>
public record TicketThreadDto(
    long Id,
    string ReferenceCode,
    string? Subject,
    string Status,
    string Category,
    long? BookingId,
    long? RefundId,
    int OpenedById,
    DateTimeOffset? ClosedAt,
    IReadOnlyList<TicketParticipantDto> Participants,
    IReadOnlyList<TicketMessageDto> Messages);

/// <summary>The header facts of a ticket (no messages) — used for authorization + the thread response header.</summary>
public record TicketHeaderDto(
    long Id,
    string ReferenceCode,
    string? Subject,
    string Status,
    string Category,
    long? BookingId,
    long? RefundId,
    int OpenedById,
    DateTimeOffset? ClosedAt);

public record OpenTicketResult(long TicketId, string ReferenceCode, string Status, string Category);

public record PostMessageResult(long MessageId, long TicketId, DateTimeOffset SentAt, string? ClientMessageId);
