using System;
using System.Text.Json.Serialization;

namespace Adaptation.FileHandlers.json.WorkItems;

public class WorkItem
{

#nullable enable

    [JsonConstructor]
    public WorkItem(DateTime? activatedDate,
                    string areaPath,
                    string? assignedTo,
                    long? businessValue,
                    DateTime changedDate,
                    DateTime? closedDate,
                    int commentCount,
                    DateTime createdDate,
                    string description,
                    long? effort,
                    int id,
                    string iterationPath,
                    int? parent,
                    int? priority,
                    Relation[]? relations,
                    string? requester,
                    DateTime? resolvedDate,
                    int revision,
                    long? riskReductionMinusOpportunityEnablement,
                    DateTime? startDate,
                    string state,
                    string tags,
                    DateTime? targetDate,
                    long? timeCriticality,
                    string title,
                    string? violation,
                    long? weightedShortestJobFirst,
                    string workItemType)
    {
        ActivatedDate = activatedDate;
        AreaPath = areaPath;
        AssignedTo = assignedTo;
        BusinessValue = businessValue;
        ChangedDate = changedDate;
        ClosedDate = closedDate;
        CommentCount = commentCount;
        CreatedDate = createdDate;
        Description = description;
        Effort = effort;
        Id = id;
        IterationPath = iterationPath;
        Parent = parent;
        Priority = priority;
        Relations = relations;
        Requester = requester;
        ResolvedDate = resolvedDate;
        Revision = revision;
        RiskReductionMinusOpportunityEnablement = riskReductionMinusOpportunityEnablement;
        StartDate = startDate;
        State = state;
        Tags = tags;
        TargetDate = targetDate;
        TimeCriticality = timeCriticality;
        Title = title;
        Violation = violation;
        WeightedShortestJobFirst = weightedShortestJobFirst;
        WorkItemType = workItemType;
    }

    public override string ToString() => $"{Id} - {WorkItemType} - {Title}";

    public static Record Get(Record record, string? violation)
    {
        Record result;
        WorkItem workItem = new(record.WorkItem.ActivatedDate,
                                record.WorkItem.AreaPath,
                                record.WorkItem.AssignedTo,
                                record.WorkItem.BusinessValue,
                                record.WorkItem.ChangedDate,
                                record.WorkItem.ClosedDate,
                                record.WorkItem.CommentCount,
                                record.WorkItem.CreatedDate,
                                record.WorkItem.Description,
                                record.WorkItem.Effort,
                                record.WorkItem.Id,
                                record.WorkItem.IterationPath,
                                record.WorkItem.Parent,
                                record.WorkItem.Priority,
                                record.WorkItem.Relations,
                                record.WorkItem.Requester,
                                record.WorkItem.ResolvedDate,
                                record.WorkItem.Revision,
                                record.WorkItem.RiskReductionMinusOpportunityEnablement,
                                record.WorkItem.StartDate,
                                record.WorkItem.State,
                                record.WorkItem.Tags,
                                record.WorkItem.TargetDate,
                                record.WorkItem.TimeCriticality,
                                record.WorkItem.Title,
                                record.WorkItem.Violation is null ? violation : record.WorkItem.Violation,
                                record.WorkItem.WeightedShortestJobFirst,
                                record.WorkItem.WorkItemType);
        result = new(workItem, record.Parent, Array.Empty<Record>());
        return result;
    }

    public DateTime? ActivatedDate { get; set; }
    public string AreaPath { get; set; }
    public string? AssignedTo { get; set; }
    public long? BusinessValue { get; set; }
    public DateTime ChangedDate { get; set; }
    public DateTime? ClosedDate { get; set; }
    public int CommentCount { get; set; }
    public DateTime CreatedDate { get; set; }
    public string Description { get; set; }
    public long? Effort { get; set; }
    public int Id { get; set; }
    public string IterationPath { get; set; }
    public int? Parent { get; set; }
    public int? Priority { get; set; }
    public Relation[]? Relations { get; set; }
    public string? Requester { get; set; }
    public DateTime? ResolvedDate { get; set; }
    public int Revision { get; set; }
    public long? RiskReductionMinusOpportunityEnablement { get; set; }
    public DateTime? StartDate { get; set; }
    public string State { get; set; }
    public string Tags { get; set; }
    public DateTime? TargetDate { get; set; }
    public long? TimeCriticality { get; set; }
    public string Title { get; set; }
    public string? Violation { get; set; }
    public string WorkItemType { get; set; }
    public long? WeightedShortestJobFirst { get; set; }

}