using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace rgbc_sds.dal.DB
{
///
/// Entity representing an import session for shipment data
/// Maps to sdsImportSessions table
///
[Table("sdsImportSessions")]
public class ImportSession
{
///
/// Unique identifier for the import session
///
[Key]
[Required]
public Guid Id { get; set; }
///
/// User who initiated the import
///
[Required]
[StringLength(100)]
public string CreatedBy { get; set; } = string.Empty;
///
/// When the import session was created
///
[Required]
public DateTime CreatedAt { get; set; }
///
/// Current status of the import session
///
[Required]
[StringLength(20)]
public string Status { get; set; } = "Pending";
///
/// Total number of rows in the import file
///
[Required]
[Range(0, int.MaxValue)]
public int TotalRows { get; set; } = 0;
///
/// Number of rows that passed validation
///
[Required]
[Range(0, int.MaxValue)]
public int ValidRows { get; set; } = 0;
///
/// Number of rows with validation errors
///
[Required]
[Range(0, int.MaxValue)]
public int ErrorRows { get; set; } = 0;
///
/// Processing time in milliseconds
///
[Range(0, int.MaxValue)]
public int? ProcessingTime { get; set; }
///
/// Original filename that was imported
///
[StringLength(255)]
public string? FileName { get; set; }
///
/// Size of the imported file in bytes
///
[Range(0, long.MaxValue)]
public long? FileSize { get; set; }
///
/// Import strategy: UpdateExisting or AddNewOnly
///
[StringLength(20)]
public string? ImportMode { get; set; }
///
/// Additional notes about the import session
///
[StringLength(500)]
public string? Notes { get; set; }
///
/// When the import session was completed
///
public DateTime? CompletedAt { get; set; }
// Navigation Properties
///
/// Collection of staging shipments for this session
///
public virtual ICollection StagingShipments { get; set; } = new List();
///
/// Collection of error logs for this session
///
public virtual ICollection ImportErrorLogs { get; set; } = new List();
// Computed Properties
///
/// Success rate as a percentage
///
[NotMapped]
public decimal SuccessRate => TotalRows > 0 ? (decimal)ValidRows / TotalRows * 100 : 0;
///
/// Whether the session is currently active
///
[NotMapped]
public bool IsActive => Status == "Pending" || Status == "Processing";
///
/// Whether the session has completed (successfully or with errors)
///
[NotMapped]
public bool IsCompleted => Status == "Completed" || Status == "Failed";
// Constructor
public ImportSession()
{
Id = Guid.NewGuid();
CreatedAt = DateTime.UtcNow;
Status = "Pending";
TotalRows = 0;
ValidRows = 0;
ErrorRows = 0;
}
// Business Methods
///
/// Start processing the import session
///
public void StartProcessing()
{
if (Status != "Pending")
throw new InvalidOperationException("Can only start processing from Pending status");
Status = "Processing";
}
///
/// Complete the import session
///
/// Whether the import was successful
public void Complete(bool success)
{
if (Status != "Processing")
throw new InvalidOperationException("Can only complete from Processing status");
Status = success ? "Completed" : "Failed";
CompletedAt = DateTime.UtcNow;
}
///
/// Update row counts during processing
///
/// Number of valid rows
/// Number of error rows
public void UpdateRowCounts(int validCount, int errorCount)
{
ValidRows = validCount;
ErrorRows = errorCount;
if (ValidRows + ErrorRows > TotalRows)
throw new InvalidOperationException("Row counts exceed total rows");
}
}
}