using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using e_suite.Database.Audit.Attributes;
using e_suite.Database.Core.Models;
using eSuite.Core.Enums;
using Microsoft.EntityFrameworkCore;

namespace e_suite.Database.Core.Tables.Activity;

[DisplayName("ActivityTask")]
[Table("ActivityTasks", Schema = "Activity")]
[Index(nameof(Guid), IsUnique = true)]
[Index(nameof(ActivityId), nameof(ActivityOrdinal), IsUnique = true)]
public class ActivityTask : IGeneralId, ISoftDeletable, IRuntimeOutcomes
{
    [Key]
    public long Id { get; set; }

    public Guid Guid { get; set; }

    public long? ParentId { get; set; }

    public long ActivityId { get; set; }

    public long ActivityOrdinal { get; set; }

    /// <summary>
    /// This is the guid of the task from the workflow template
    /// </summary>
    [Required]
    public Guid TaskGuid { get; set; }

    /// <summary>
    /// This is the fullname of the tasktype
    /// </summary>
    [Required]
    public string TaskType { get; set; }

    [AuditName]
    [Required]
    public string TaskName { get; set; }

    public ActivityState ActivityState { get; set; } = ActivityState.Pending;

    /// <summary>
    /// Contains the date and time when the task was changed from Pending to Active
    /// </summary>
    public DateTimeOffset? StartDateTime { get; set; }
    
    /// <summary>
    /// Contains the date and time when the task was changed from Active to Completed
    /// </summary>
    public DateTimeOffset? FinishDateTime { get; set; }

    public string Comments { get; set; } = string.Empty;
    public long? CompletedByUserId { get; set; }
    public string? CompletedByUserDisplayName { get; set; }

    [AuditSoftDelete(true)]
    [Required]
    [DefaultValue(false)]
    public bool Deleted { get; set; }

    [AuditLastUpdated]
    public DateTimeOffset LastUpdated { get; set; }


    [ForeignKey(nameof(ActivityId))]
    public virtual Activity Activity { get; set; } = null!;

    [ForeignKey(nameof(ParentId))]
    public virtual ActivityTask ParentTask { get; set; } = null!;

    public ICollection<ActivityAssignment> Assignments { get; set; } = [];

    public ICollection<ActivityTask> Tasks { get; set; } = [];

    public List<string> Outcomes { get; set; } = [];
}