using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json;

namespace Database.Models
{
    public class BuildCommit : BaseEntity
    {
        public int BuildId { get; set; }
        
        [MaxLength(40)]
        public string? CommitHash { get; set; }
        
        public string? Comment { get; set; }
        
        [MaxLength(100)]
        public string? User { get; set; }
        
        public DateTime CommitDate { get; set; }
        
        [MaxLength(20)]
        public string? FogBugzCaseId { get; set; }
        
        public int? FogBugzCaseIdInt => int.TryParse(FogBugzCaseId, out int result) ? result : null;
        
        // JSON column for PostgreSQL
        [Column(TypeName = "jsonb")]
        public string? ModifiedFiles { get; set; }
        
        public string? ReleaseNote { get; set; }
        
        // Navigation properties
        [ForeignKey(nameof(BuildId))]
        public Build Build { get; set; } = null!;
        
        [ForeignKey(nameof(FogBugzCaseId))]
        public FogBugzCase? FogBugzCase { get; set; }
        
        // Helper methods for ModifiedFiles JSON
        public List<string> GetModifiedFiles()
        {
            if (string.IsNullOrEmpty(ModifiedFiles))
                return new List<string>();
                
            try
            {
                return JsonSerializer.Deserialize<List<string>>(ModifiedFiles) ?? new List<string>();
            }
            catch
            {
                return new List<string>();
            }
        }
        
        public void SetModifiedFiles(List<string> files)
        {
            ModifiedFiles = JsonSerializer.Serialize(files);
        }
    }
}