using System.ComponentModel.DataAnnotations;
using System.Text.Json;
using Tiku.Application.Profile;

namespace Tiku.Api.Contracts;

/// <summary>
/// 资料查询参数。
/// </summary>
public sealed class ProfileQueryDto
{
    /// <summary>
    /// 最近记录数量上限。
    /// </summary>
    [Range(1, 50)]
    public int? RecentLimit { get; set; }

    /// <summary>
    /// 返回数量上限。
    /// </summary>
    [Range(1, 20)]
    public int? Limit { get; set; }

    /// <summary>
    /// 状态。
    /// </summary>
    [StringLength(32)]
    public string? Status { get; set; }

    /// <summary>
    /// 类型。
    /// </summary>
    [StringLength(50)]
    public string? Type { get; set; }

    /// <summary>
    /// 分类。
    /// </summary>
    [StringLength(50)]
    public string? Category { get; set; }

    /// <summary>
    /// 是否包含锁定资源。
    /// </summary>
    public bool IncludeLocked { get; set; }

    /// <summary>
    /// 来源类型。
    /// </summary>
    [StringLength(100)]
    public string? SourceType { get; set; }

    /// <summary>
    /// 开始时间。
    /// </summary>
    public DateTimeOffset? From { get; set; }

    /// <summary>
    /// 结束时间。
    /// </summary>
    public DateTimeOffset? To { get; set; }
}

/// <summary>
/// 更新资料请求 DTO。
/// </summary>
public sealed class UpdateProfileDto
{
    /// <summary>
    /// 名称。
    /// </summary>
    [StringLength(100)]
    public string? Name { get; set; }

    /// <summary>
    /// 头像预设。
    /// </summary>
    [StringLength(32)]
    public string? AvatarPreset { get; set; }

    /// <summary>
    /// 地区 ID。
    /// </summary>
    public Guid? RegionId { get; set; }
    /// <summary>
    /// 已选择院校 ID。
    /// </summary>
    public Guid? SelectedSchoolId { get; set; }
    /// <summary>
    /// 已选择专业 ID。
    /// </summary>
    public Guid? SelectedMajorId { get; set; }
    /// <summary>
    /// 统计数据。
    /// </summary>
    public JsonElement? Stats { get; set; }
    /// <summary>
    /// 进度数据。
    /// </summary>
    public JsonElement? Progress { get; set; }
    /// <summary>
    /// 模块选择数据。
    /// </summary>
    public JsonElement? ModuleSelections { get; set; }
    /// <summary>
    /// 最近活动数据。
    /// </summary>
    public JsonElement? RecentActivities { get; set; }

    public UpdateProfileCommand ToCommand()
    {
        return new UpdateProfileCommand(
            Name,
            AvatarPreset,
            RegionId,
            SelectedSchoolId,
            SelectedMajorId,
            Stats,
            Progress,
            ModuleSelections,
            RecentActivities);
    }
}

/// <summary>
/// 通知状态请求 DTO。
/// </summary>
public sealed class NotificationStatusDto
{
    /// <summary>
    /// 通知 ID 列表。
    /// </summary>
    [Required]
    [MinLength(1)]
    [MaxLength(100)]
    public IReadOnlyCollection<Guid> NotificationIds { get; set; } = [];

    /// <summary>
    /// 状态。
    /// </summary>
    [StringLength(32)]
    public string? Status { get; set; }

    public UpdateNotificationStatusCommand ToCommand()
    {
        return new UpdateNotificationStatusCommand(NotificationIds, Status);
    }
}

/// <summary>
/// 提交反馈请求 DTO。
/// </summary>
public sealed class SubmitFeedbackDto
{
    /// <summary>
    /// 题目 ID。
    /// </summary>
    public Guid? QuestionId { get; set; }

    /// <summary>
    /// 类型。
    /// </summary>
    [StringLength(50)]
    public string? Type { get; set; }

    /// <summary>
    /// 分类。
    /// </summary>
    [StringLength(100)]
    public string? Category { get; set; }

    /// <summary>
    /// 标题。
    /// </summary>
    [StringLength(200)]
    public string? Title { get; set; }

    /// <summary>
    /// 说明。
    /// </summary>
    [Required]
    [StringLength(5000)]
    public string Description { get; set; } = string.Empty;

    /// <summary>
    /// 优先级。
    /// </summary>
    [StringLength(32)]
    public string? Priority { get; set; }

    /// <summary>
    /// 联系方式。
    /// </summary>
    [StringLength(200)]
    public string? Contact { get; set; }

    /// <summary>
    /// 附件列表。
    /// </summary>
    public JsonElement Attachments { get; set; } = JsonSerializer.SerializeToElement(Array.Empty<object>());
    /// <summary>
    /// 扩展元数据。
    /// </summary>
    public JsonElement Metadata { get; set; } = JsonSerializer.SerializeToElement(new { });

    public SubmitFeedbackCommand ToCommand()
    {
        return new SubmitFeedbackCommand(
            QuestionId,
            Type,
            Category,
            Title,
            Description,
            Priority,
            Contact,
            Attachments,
            Metadata);
    }
}
