using Microsoft.EntityFrameworkCore;


/// <summary>
/// Seeds batch state for verification integration tests. Uses usernames from
/// <see cref="DataSeeder"/> (entry1, entry2, verifier1, etc.) — never hard-coded user IDs.
/// </summary>
public static class BatchSeedHelper
{
    public static async Task<Guid> UserIdAsync(AppDbContext db, string username) =>
        (await db.Users.AsNoTracking().FirstAsync(u => u.Username == username)).Id;

    /// <summary>
    /// Creates a batch in PendingVerification status with EnteredByUserId set,
    /// simulating a batch that has been through data entry and submission.
    /// </summary>
    public static async Task<DigitizationBatch> SeedBatchInPendingVerificationAsync(
        AppDbContext db,
        Guid enteredByUserId,
        BatchType batchType = BatchType.VitalsSheet)
    {
        var batchId = Guid.NewGuid();
        var batch = new DigitizationBatch
        {
            Id = batchId,
            Status = BatchStatus.PendingVerification,
            BatchType = batchType,
            Track = BatchTrack.Backfill,
            DocumentRef = $"scans/2026/01/{batchId}/abc123.pdf",
            DocumentSha256 = Guid.NewGuid().ToString("N"), // Fake SHA-256 for testing
            EnteredByUserId = enteredByUserId,
            CreatedAt = DateTimeOffset.UtcNow.AddMinutes(-30),
            UpdatedAt = DateTimeOffset.UtcNow.AddMinutes(-5)
        };

        db.DigitizationBatches.Add(batch);

        // Add the submission event
        db.DigitizationEvents.Add(new DigitizationEvent
        {
            Id = Guid.NewGuid(),
            BatchId = batchId,
            EventType = DigitizationEventType.SubmittedForVerification,
            ActorUserId = enteredByUserId,
            OccurredAt = DateTimeOffset.UtcNow.AddMinutes(-5)
        });

        // Add a ScannedDocument so FK constraints are satisfied
        db.ScannedDocuments.Add(new ScannedDocument
        {
            Id = Guid.NewGuid(),
            BatchId = batchId,
            ObjectKey = batch.DocumentRef,
            Sha256 = batch.DocumentSha256,
            ContentType = "application/pdf",
            FileSizeBytes = 1024,
            UploadedAt = DateTimeOffset.UtcNow.AddMinutes(-30)
        });

        await db.SaveChangesAsync();
        return batch;
    }

    /// <summary>
    /// Creates a batch in Rejected status for re-entry testing.
    /// </summary>
    public static async Task<DigitizationBatch> SeedBatchInRejectedAsync(
        AppDbContext db,
        Guid enteredByUserId,
        string rejectionReason = "Vital signs appear inconsistent with chart notes")
    {
        var batchId = Guid.NewGuid();
        var batch = new DigitizationBatch
        {
            Id = batchId,
            Status = BatchStatus.Rejected,
            BatchType = BatchType.VitalsSheet,
            Track = BatchTrack.Backfill,
            DocumentRef = $"scans/2026/01/{batchId}/abc123.pdf",
            DocumentSha256 = Guid.NewGuid().ToString("N"),
            EnteredByUserId = enteredByUserId,
            RejectionReason = rejectionReason,
            CreatedAt = DateTimeOffset.UtcNow.AddHours(-2),
            UpdatedAt = DateTimeOffset.UtcNow.AddMinutes(-10)
        };

        db.DigitizationBatches.Add(batch);

        db.ScannedDocuments.Add(new ScannedDocument
        {
            Id = Guid.NewGuid(),
            BatchId = batchId,
            ObjectKey = batch.DocumentRef,
            Sha256 = batch.DocumentSha256,
            ContentType = "application/pdf",
            FileSizeBytes = 1024,
            UploadedAt = DateTimeOffset.UtcNow.AddHours(-2)
        });

        await db.SaveChangesAsync();
        return batch;
    }
}