@implements IDisposable

<div class="veracity-score-card">
    <div class="score-circle @GetScoreColorClass()">
        <svg viewBox="0 0 36 36" class="score-chart">
            <path class="score-background"
                  d="M18 2.0845
                     a 15.9155 15.9155 0 0 1 0 31.831
                     a 15.9155 15.9155 0 0 1 0 -31.831" />
            <path class="score-progress"
                  stroke-dasharray="@_displayScore, 100"
                  d="M18 2.0845
                     a 15.9155 15.9155 0 0 1 0 31.831
                     a 15.9155 15.9155 0 0 1 0 -31.831" />
        </svg>
        <div class="score-value">
            <span class="score-number">@_displayScore</span>
            <span class="score-max">/100</span>
        </div>
    </div>
    <div class="score-label @GetScoreColorClass()">
        @(string.IsNullOrEmpty(Label) ? GetDefaultLabel() : Label)
    </div>
    <div class="score-description">
        @GetScoreDescription()
    </div>
</div>

<style>
    .veracity-score-card {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 1.5rem;
        background-color: #fff;
        border: 1px solid #dee2e6;
        border-radius: 0.5rem;
    }

    .score-circle {
        position: relative;
        width: 160px;
        height: 160px;
        margin-bottom: 1rem;
    }

    .score-chart {
        width: 100%;
        height: 100%;
        transform: rotate(-90deg);
    }

    .score-background {
        fill: none;
        stroke: #e9ecef;
        stroke-width: 3;
    }

    .score-progress {
        fill: none;
        stroke-width: 3;
        stroke-linecap: round;
        transition: stroke-dasharray 0.5s ease-out;
    }

    .score-circle.excellent .score-progress { stroke: #198754; }
    .score-circle.good .score-progress { stroke: #20c997; }
    .score-circle.moderate .score-progress { stroke: #ffc107; }
    .score-circle.poor .score-progress { stroke: #dc3545; }

    .score-value {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        text-align: center;
    }

    .score-number {
        font-size: 2.5rem;
        font-weight: 700;
        line-height: 1;
    }

    .score-circle.excellent .score-number { color: #198754; }
    .score-circle.good .score-number { color: #20c997; }
    .score-circle.moderate .score-number { color: #ffc107; }
    .score-circle.poor .score-number { color: #dc3545; }

    .score-max {
        font-size: 0.875rem;
        color: #6c757d;
    }

    .score-label {
        font-size: 1.25rem;
        font-weight: 600;
        margin-bottom: 0.25rem;
    }

    .score-label.excellent { color: #198754; }
    .score-label.good { color: #20c997; }
    .score-label.moderate { color: #ffc107; }
    .score-label.poor { color: #dc3545; }

    .score-description {
        font-size: 0.875rem;
        color: #6c757d;
        text-align: center;
    }
</style>

@code {
    private int _displayScore;
    private System.Threading.Timer? _animationTimer;
    private int _targetScore;

    [Parameter]
    public int Score { get; set; }

    [Parameter]
    public string? Label { get; set; }

    protected override void OnParametersSet()
    {
        _targetScore = Math.Clamp(Score, 0, 100);

        if (_displayScore != _targetScore)
        {
            StartAnimation();
        }
    }

    private void StartAnimation()
    {
        _animationTimer?.Dispose();

        var increment = _targetScore > _displayScore ? 1 : -1;
        var intervalMs = Math.Max(10, 500 / Math.Max(1, Math.Abs(_targetScore - _displayScore)));

        _animationTimer = new System.Threading.Timer(_ =>
        {
            if (_displayScore == _targetScore)
            {
                _animationTimer?.Dispose();
                return;
            }

            _displayScore += increment;
            InvokeAsync(StateHasChanged);
        }, null, 0, intervalMs);
    }

    private string GetScoreColorClass()
    {
        return _targetScore switch
        {
            >= 90 => "excellent",
            >= 70 => "good",
            >= 50 => "moderate",
            _ => "poor"
        };
    }

    private string GetDefaultLabel()
    {
        return _targetScore switch
        {
            >= 90 => "Excellent",
            >= 70 => "Good",
            >= 50 => "Moderate",
            _ => "Poor"
        };
    }

    private string GetScoreDescription()
    {
        return _targetScore switch
        {
            >= 90 => "This CV demonstrates high veracity with minimal concerns.",
            >= 70 => "This CV is generally trustworthy with some minor concerns.",
            >= 50 => "This CV has some inconsistencies that may need clarification.",
            _ => "This CV has significant concerns that require attention."
        };
    }

    public void Dispose()
    {
        _animationTimer?.Dispose();
    }
}
