using Books.Api.EventFlow.ReadModels;
using Books.Api.Infrastructure.FileStorage;
using GraphQL.Types;
using Microsoft.Extensions.DependencyInjection;

namespace Books.Api.GraphQL.Types;

public class AttachmentGraphType : ObjectGraphType<AttachmentReadModelDto>
{
    public AttachmentGraphType()
    {
        Name = "Attachment";
        Description = "A document attachment (bilag) - required by Bogføringsloven § 6";

        Field(x => x.Id).Description("Unique identifier");
        Field(x => x.CompanyId).Description("Company this attachment belongs to");
        Field(x => x.FileName).Description("Stored filename");
        Field(x => x.OriginalFileName).Description("Original filename as uploaded");
        Field(x => x.ContentType).Description("MIME type (e.g., application/pdf)");
        Field(x => x.FileSize).Description("File size in bytes");
        Field(x => x.UploadedBy).Description("User who uploaded the attachment");
        Field(x => x.UploadedAt).Description("When the attachment was uploaded");
        Field(x => x.DraftId, nullable: true).Description("Reference to journal entry draft (before posting)");
        Field(x => x.TransactionId, nullable: true).Description("Reference to posted transaction (after posting)");
        Field(x => x.RetentionEndDate).Description("5-year retention period end date (Bogføringsloven § 6)");
        Field(x => x.IsDeleted).Description("Whether the attachment has been deleted");

        // Download URL is dynamically generated
        Field<NonNullGraphType<StringGraphType>>("url")
            .Description("URL to download the attachment")
            .Resolve(ctx =>
            {
                var fileStorage = ctx.RequestServices!.GetRequiredService<IFileStorageService>();
                return fileStorage.GetDownloadUrl(ctx.Source.StoragePath);
            });
    }
}
