using e_suite.Database.Core.Tables.Activity;
using eSuite.Core.Enums;

namespace e_suite.Workflow.Core.Extensions;

public static class ActivityAssignmentExtensions
{
    extension(ActivityAssignment task)
    {
        public void SetState(ActivityState newState)
        {
            if (newState == ActivityState.Active)
            {
                if (task.StartDateTime == null)
                    throw new InvalidOperationException(
                        $"Cannot set state to Active because StartDateTime is not set for task {task.Id}."
                    );
            }

            if (newState == ActivityState.ReadyToComplete)
            {
                if (task.Outcomes == null || task.Outcomes.Count == 0)
                    throw new InvalidOperationException(
                        $"Cannot set state to ReadyToComplete because no outcomes have been recorded for task {task.Id}."
                    );
            }

            if (newState == ActivityState.Completed)
            {
                if (task.FinishDateTime == null)
                    throw new InvalidOperationException(
                        $"Cannot set state to Completed because FinishDateTime is not set for task {task.Id}."
                    );
            }

            task.ActivityState = newState;
        }
    }
}
