namespace PSLauncher.Models;

/// <summary>
/// État persistant d'un téléchargement en cours, écrit régulièrement sur disque
/// pour permettre la reprise si le launcher est fermé / crashe pendant le DL.
///
/// Schema 2 : ajout de <see cref="Segments"/> pour le téléchargement parallèle multi-segments.
/// </summary>
public sealed class DownloadState
{
    public int SchemaVersion { get; set; } = 2;
    public string Version { get; set; } = string.Empty;
    public string Url { get; set; } = string.Empty;
    public long TotalBytes { get; set; }
    public long DownloadedBytes { get; set; }
    public string ExpectedSha256 { get; set; } = string.Empty;
    public string? Etag { get; set; }
    public string? LastModified { get; set; }
    public string PartialPath { get; set; } = string.Empty;
    public DateTime StartedAtUtc { get; set; }
    public DateTime LastChunkAtUtc { get; set; }

    /// <summary>
    /// Si non vide, indique un téléchargement parallèle multi-segments.
    /// Chaque segment couvre un range exclusif [Start, End] du fichier final.
    /// </summary>
    public List<DownloadSegment> Segments { get; set; } = new();
}

public sealed class DownloadSegment
{
    public int Index { get; set; }
    public long Start { get; set; }
    public long End { get; set; }   // inclusif
    public long DownloadedBytes { get; set; }
    public bool Completed { get; set; }

    public long Length => End - Start + 1;
    public long RemainingBytes => Length - DownloadedBytes;
}
