using System.Text.Json;
using Tiku.Domain.Common;

namespace Tiku.Domain.Import;

public sealed class PocketBaseImportRun : Entity, ITenantOwned
{
    public Guid TenantId { get; set; }
    public string SourceName { get; set; } = string.Empty;
    public PocketBaseImportSourceKind SourceKind { get; set; } = PocketBaseImportSourceKind.Json;
    public PocketBaseImportRunStatus Status { get; set; } = PocketBaseImportRunStatus.Running;
    public JsonElement Stats { get; set; } = JsonDefaults.Object();
    public DateTimeOffset StartedAt { get; set; } = DateTimeOffset.UtcNow;
    public DateTimeOffset? FinishedAt { get; set; }
}

public sealed class PocketBaseRawRecord : ITenantOwned
{
    public Guid RunId { get; set; }
    public Guid TenantId { get; set; }
    public string CollectionName { get; set; } = string.Empty;
    public string LegacyId { get; set; } = string.Empty;
    public JsonElement Record { get; set; } = JsonDefaults.Object();
    public bool Normalized { get; set; }
    public JsonElement Errors { get; set; } = JsonDefaults.Array();
    public DateTimeOffset ImportedAt { get; set; } = DateTimeOffset.UtcNow;
}

public sealed class PocketBaseImportIssue : Entity, ITenantOwned
{
    public Guid? RunId { get; set; }
    public Guid TenantId { get; set; }
    public string CollectionName { get; set; } = string.Empty;
    public string? LegacyId { get; set; }
    public PocketBaseImportIssueSeverity Severity { get; set; } = PocketBaseImportIssueSeverity.Warning;
    public string IssueCode { get; set; } = string.Empty;
    public string Message { get; set; } = string.Empty;
    public string? FieldPath { get; set; }
    public string? RawValueSample { get; set; }
    public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
}

public enum PocketBaseImportSourceKind { Json, Sqlite, Api, Archive }
public enum PocketBaseImportRunStatus { Running, Completed, Failed }
public enum PocketBaseImportIssueSeverity { Info, Warning, Error, Critical }
