using System.ComponentModel.DataAnnotations;
using System.Text.Json;
using Tiku.Application.Learning;
using Tiku.Application.QuestionBanks;
using Tiku.Domain.Common;
using Tiku.Domain.Content;

namespace Tiku.Api.Contracts;

public sealed class LearningLimitQueryDto
{
    [Range(1, 500)]
    public int? Limit { get; set; }

    [StringLength(50)]
    public string? Status { get; set; }

    public Guid? UnitId { get; set; }

    public LearningLimitFilter ToFilter()
    {
        return new LearningLimitFilter(Limit, Status, UnitId);
    }
}

public sealed class PracticeSessionQueryDto
{
    public Guid? PracticeSessionId { get; set; }

    public Guid? BlueprintId { get; set; }

    [StringLength(50)]
    public string? Mode { get; set; }

    [StringLength(50)]
    public string? Status { get; set; }

    [Range(1, 200)]
    public int? Limit { get; set; }

    public PracticeSessionFilter ToFilter(Guid? routePracticeSessionId = null)
    {
        return new PracticeSessionFilter(
            routePracticeSessionId ?? PracticeSessionId,
            BlueprintId,
            Mode,
            Status,
            Limit);
    }
}

public sealed class CreatePracticeSessionDto
{
    [StringLength(50)]
    public string? Mode { get; set; }

    [StringLength(50)]
    public string? TargetType { get; set; }

    public Guid? TargetId { get; set; }

    public Guid? BlueprintId { get; set; }

    public Guid? CollectionId { get; set; }

    public Guid? EntryId { get; set; }

    public Guid? ContentNodeId { get; set; }

    [Range(1, 500)]
    public int? QuestionLimit { get; set; }

    [Range(1, 1440)]
    public int? DurationMinutes { get; set; }

    [Range(typeof(decimal), "0", "99999")]
    public decimal? TotalScore { get; set; }

    public JsonElement Metadata { get; set; } = JsonDefaults.Object();

    public PracticeSessionCommand ToCommand()
    {
        return new PracticeSessionCommand(
            Mode,
            TargetType,
            TargetId,
            BlueprintId,
            CollectionId,
            EntryId,
            ContentNodeId,
            QuestionLimit,
            DurationMinutes,
            TotalScore,
            Metadata);
    }
}

public sealed class SubmitPracticeSessionDto
{
    [Required]
    public Guid PracticeSessionId { get; set; }

    public PracticeSessionFilter ToFilter()
    {
        return new PracticeSessionFilter(PracticeSessionId);
    }
}

public sealed class SubmitAnswerDto
{
    [Required]
    public Guid SessionQuestionId { get; set; }

    public IReadOnlyCollection<string>? SelectedOptions { get; set; }

    [StringLength(10000)]
    public string? AnswerText { get; set; }

    public bool? SelfJudgedCorrect { get; set; }

    public SubmitAnswerCommand ToCommand()
    {
        return new SubmitAnswerCommand(
            SessionQuestionId,
            SelectedOptions,
            AnswerText,
            SelfJudgedCorrect);
    }
}

public sealed class QuestionActionDto
{
    [Required]
    public Guid QuestionId { get; set; }

    [Required]
    public QuestionSource Source { get; set; } = QuestionSource.Tenant;

    public bool? Favorite { get; set; }

    public QuestionActionCommand ToCommand()
    {
        return new QuestionActionCommand(new QuestionLocator(Source, QuestionId), Favorite);
    }
}

public sealed class WordProgressDto
{
    [Required]
    public Guid WordId { get; set; }

    [StringLength(50)]
    public string? Status { get; set; }

    [Range(0, 100)]
    public int? CorrectDelta { get; set; }

    [Range(0, 100)]
    public int? WrongDelta { get; set; }

    public DateTimeOffset? NextReviewAt { get; set; }

    public WordProgressCommand ToCommand()
    {
        return new WordProgressCommand(
            WordId,
            Status,
            CorrectDelta,
            WrongDelta,
            NextReviewAt);
    }
}

public sealed class FavoriteWordDto
{
    [Required]
    public Guid WordId { get; set; }

    public bool? Favorite { get; set; }

    [StringLength(1000)]
    public string? Note { get; set; }

    public FavoriteWordCommand ToCommand()
    {
        return new FavoriteWordCommand(WordId, Favorite, Note);
    }
}

public sealed class WordReviewDto
{
    [Required]
    public Guid WordId { get; set; }

    [StringLength(50)]
    public string? Result { get; set; }

    public DateTimeOffset? NextReviewAt { get; set; }

    public WordReviewCommand ToCommand()
    {
        return new WordReviewCommand(WordId, Result, NextReviewAt);
    }
}
