using CashLineERP.Domain.Common;

namespace CashLineERP.Domain.Parties;

public sealed class OnboardingReviewChecklistItem : Entity
{
    public long Id { get; private set; }
    public long WorkflowInstanceId { get; private set; }
    public string SubjectTypeCode { get; private set; } = string.Empty;
    public long SubjectProfileId { get; private set; }
    public string ChecklistItemCode { get; private set; } = string.Empty;
    public string DisplayName { get; private set; } = string.Empty;
    public string StatusCode { get; private set; } = string.Empty;
    public string CoverageCode { get; private set; } = string.Empty;
    public string? ReviewerNotesText { get; private set; }
    public DateTime ReviewedAtUtc { get; private set; } = DateTime.UtcNow;
    public long ReviewedByUserId { get; private set; }

    public OnboardingReviewChecklistItem(
        long workflowInstanceId,
        string subjectTypeCode,
        long subjectProfileId,
        string checklistItemCode,
        string displayName,
        string statusCode,
        string coverageCode,
        long reviewedByUserId,
        string? reviewerNotesText)
    {
        if (workflowInstanceId <= 0 || subjectProfileId <= 0 || reviewedByUserId <= 0)
        {
            throw new InvalidOperationException("Onboarding review checklist item requires valid workflow, profile, and reviewer identifiers.");
        }

        WorkflowInstanceId = workflowInstanceId;
        SubjectTypeCode = NormalizeRequired(subjectTypeCode, "subject type code");
        SubjectProfileId = subjectProfileId;
        ChecklistItemCode = NormalizeRequired(checklistItemCode, "checklist item code");
        DisplayName = NormalizeRequired(displayName, "display name");
        StatusCode = NormalizeRequired(statusCode, "status code");
        CoverageCode = NormalizeRequired(coverageCode, "coverage code");
        ReviewerNotesText = NormalizeOptional(reviewerNotesText);
        ReviewedByUserId = reviewedByUserId;
        CreatedByUserId = reviewedByUserId;
    }

    private OnboardingReviewChecklistItem()
    {
    }

    public void UpdateReview(string displayName, string statusCode, string coverageCode, long reviewedByUserId, string? reviewerNotesText)
    {
        if (reviewedByUserId <= 0)
        {
            throw new InvalidOperationException("Onboarding review checklist item requires a valid reviewer.");
        }

        DisplayName = NormalizeRequired(displayName, "display name");
        StatusCode = NormalizeRequired(statusCode, "status code");
        CoverageCode = NormalizeRequired(coverageCode, "coverage code");
        ReviewerNotesText = NormalizeOptional(reviewerNotesText);
        ReviewedAtUtc = DateTime.UtcNow;
        ReviewedByUserId = reviewedByUserId;
        Touch(reviewedByUserId);
    }

    private static string NormalizeRequired(string value, string fieldName)
    {
        var normalizedValue = NormalizeOptional(value);
        if (string.IsNullOrWhiteSpace(normalizedValue))
        {
            throw new InvalidOperationException($"Onboarding review checklist item {fieldName} is required.");
        }

        return normalizedValue;
    }

    private static string? NormalizeOptional(string? value) =>
        string.IsNullOrWhiteSpace(value) ? null : value.Trim();
}
