
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using isn.abst;
using isnd.Data.Catalog;
using isnd.Data.Packages;
using isnd.Data.Packages.Catalog;
using Newtonsoft.Json;
using NuGet.Versioning;

namespace isnd.Data
{
  

    public class PackageVersion 
    {
        [Required]
        [ForeignKey("Package")]
        [StringLength(1024)]
        [JsonProperty("id")]
        public string PackageId { get; set; }

        [Required]
        public int Major { get; set; }

        [Required]
        public int Minor { get; set; }

        [Required]
        public int Patch { get; set; }

        public int Revision { get; set; }


        /// <summary>
        /// Full version string
        /// </summary>
        /// <value></value>
        [StringLength(256)]
        [Required]
        [Key]
        public string FullString { get; set; }
        public bool IsPrerelease { get; set; }

        [StringLength(256)]
        public string Type { get; set; }

        [JsonIgnore]
        public virtual Package Package { get; set; }

        [Required]
        [JsonIgnore]
        [ForeignKey("LatestCommit")]
        public long CommitNId { get; set; }

        [NotMapped]

        public string CommitId { get => CommitNId.ToString(); }

        public virtual Commit LatestCommit { get; set; }
        public string NugetLink => $"{Constants.PaquetFileEstension}/{PackageId}/{FullString}/{PackageId}-{FullString}."
         + Constants.PaquetFileEstension;
        public string NuspecLink => $"{Constants.SpecFileEstension}/{PackageId}/{FullString}/{PackageId}-{FullString}."
         + Constants.SpecFileEstension;

        public string SementicVersionString { get => $"{Major}.{Minor}.{Patch}"; }
        public NuGetVersion NugetVersion { get => new NuGetVersion(FullString); }

        public CatalogEntry ToLeave(string bid, string extUrl)
        {
            return new CatalogEntry(bid + "/" + this.PackageId + "/" + FullString + ".json")
            {
                Id = PackageId,
                Version = FullString,
                authors = $"{this.Package.Owner.FullName} <${Package.Owner.Email}>",
                packageContent = extUrl + this.NugetLink
            };
        }
    }
}