using System.Text.Json;

namespace Shared.Excel;

public static class ImportStatuses
{
    public const string Created = "created";
    public const string Uploaded = "uploaded";
    public const string MappingRequired = "mapping-required";
    public const string Validating = "validating";
    public const string Validated = "validated";
    public const string Committing = "committing";
    public const string Completed = "completed";
    public const string PartiallyCompleted = "partially-completed";
    public const string Failed = "failed";
    public const string Cancelled = "cancelled";
}

public sealed record ImportFieldDefinition(
    string Key,
    string Label,
    IReadOnlyList<string> Aliases,
    string DataType,
    bool Required = false,
    int? MaxLength = null,
    int? Precision = null,
    int? Scale = null,
    string? LookupEntity = null,
    bool Importable = true,
    bool Exportable = true);

public sealed record ImportDefinition(
    string Id,
    string ScreenId,
    string Entity,
    string Title,
    IReadOnlyList<ImportFieldDefinition> Fields,
    bool AllowCreate,
    bool AllowUpdate,
    long MaxFileSizeBytes = 20 * 1024 * 1024,
    int MaxRows = 100_000);

public sealed record ImportMapping(
    string SourceColumn,
    string? TargetField,
    string Source,
    decimal? Confidence = null,
    string? Reason = null);

public sealed record ImportIssue(
    int RowNumber,
    string? Field,
    string? SourceColumn,
    string Code,
    string Message,
    string Severity);

public sealed record ImportProgressEvent(
    Guid SessionId,
    string Status,
    int ProgressPercent,
    int TotalRows,
    int ProcessedRows,
    int ValidRows,
    int InvalidRows,
    int WarningRows,
    string? Message = null);

public sealed record ImportSessionDto(
    Guid Id,
    string ImportType,
    string ScreenId,
    string FileName,
    string Status,
    int TotalRows,
    int ValidRows,
    int InvalidRows,
    int WarningRows,
    int CreatedRows,
    int UpdatedRows,
    int ProgressPercent,
    IReadOnlyList<string> SourceColumns,
    IReadOnlyList<ImportMapping> Mapping,
    IReadOnlyList<ImportIssue>? Errors,
    DateTimeOffset CreatedAt,
    DateTimeOffset? CompletedAt);

public sealed record ImportRawRow(int RowNumber, IReadOnlyDictionary<string, string?> Values);
public sealed record ImportNormalizedRow(int RowNumber, JsonDocument Data, string? DomainKey);

public sealed record ImportRowValidation(
    int RowNumber,
    IReadOnlyDictionary<string, string?> RawData,
    string? DomainKey,
    JsonDocument? NormalizedData,
    IReadOnlyList<ImportIssue> Errors,
    IReadOnlyList<ImportIssue> Warnings)
{
    public bool IsValid => Errors.Count == 0;
}

public sealed record ImportCommitResult(int Created, int Updated, int Failed);
