using System;
using System.Collections.Generic;

namespace Knot.Contracts.Messaging.Domain;

public class PollMessage : Message
{
    public override string Type => "poll";
    public override string? Content { get; protected set; }
    public List<PollOption> Options { get; set; } = new();
    public List<PollVote> Votes { get; set; } = new();
    public bool IsMultipleChoice { get; set; }
    public bool IsAnonymous { get; set; }
    public DateTime? ExpiresAt { get; set; }
    public bool IsClosed { get; set; }

    public PollMessage() : base() { }

    public PollMessage(Guid id, Guid chatId, Guid senderId, string? question, List<PollOption>? options, bool isAnonymous, bool isMultiple, DateTime? expiresAt, Guid? replyToId, Guid? forwardedFromId, DateTime createdAt, bool isImported)
        : base(id, chatId, senderId, replyToId, forwardedFromId, createdAt, isImported)
    {
        Content = question ?? "Poll";
        Options = options ?? new List<PollOption>();
        IsAnonymous = isAnonymous;
        IsMultipleChoice = isMultiple;
        ExpiresAt = expiresAt;
    }

    public static PollMessage Create(Guid id, Guid chatId, Guid senderId, string? question, List<string> options, bool isAnonymous, bool isMultiple, DateTime? expiresAt, Guid? replyToId, Guid? forwardedFromId)
    {
        var poll = new PollMessage(id, chatId, senderId, question, null, isAnonymous, isMultiple, expiresAt, replyToId, forwardedFromId, DateTime.UtcNow, false);
        foreach (var opt in options)
        {
            poll.Options.Add(new PollOption { Text = opt });
        }
        return poll;
    }
}
