using Efformance.Core.BlobStorage;
using Newtonsoft.Json;
using PropertyFuzeData;
using SattelitePkgTest;
using System.Data;
using static PropertyFuzeData.SendMail;


Tenants tenant = new();

//efformancetest azure storage
string blobServiceConnectionString = "DefaultEndpointsProtocol=https;AccountName=efformancetest;AccountKey=as1YNadJfHEbrILjveYFYz13slnhSubCIt9j6X8sYb+nkgY4WtsT+KTAX5tjMJHneMHpC3memvmp+ASt7BkN4Q==;EndpointSuffix=core.windows.net";
string rentManagerUrl = "https://efformance.api.rentmanager.com";
string apiToken = "80b4e701-27c9-4628-8906-c491512a1e73|1";
int tenantId = 242;
int vendorId = 480;
int mailingHistoryCategoryId = 3;

DataTable tenantDt = tenant.GetTenant(rentManagerUrl, apiToken, tenantId);

if (tenantDt != null && tenantDt.Rows.Count != 0)
{
    DataRow first = tenantDt.Rows[0];
    Console.WriteLine(JsonConvert.SerializeObject(first.ToDynamic(), Formatting.Indented));
}
else
{
    Console.WriteLine($"Tenant ID [{tenantId}] not found.", tenantId);
}


List<HistoryAttachment> historyAttachments = new();
IList<HistoryAttachmentResult> historyAttachmentsResults = null;
Guid uniqueIdForTask = Guid.NewGuid();
Guid uniqueIdForNote = Guid.NewGuid();
string blobPath = Path.Combine(uniqueIdForTask.ToString(), "testfile.txt");

AzureFileDefinition letterFileDefinition = new("working", blobPath, "testfile.txt", "text/plain");

historyAttachments.Add(new HistoryAttachment()
{
    HistoryCategoryID = mailingHistoryCategoryId,
    MetaTag = string.Concat(uniqueIdForNote.ToString(), "_letter"),
    CreateDate = DateTime.Now.Date,
    Note = string.Concat("A text note"),
    ParentID = tenantId,
    //HistoryAttachments = new List<HistoryAttachmentDetail>() //you must pass in an empty attachment list when you just want a note.
    HistoryAttachments = new List<HistoryAttachmentDetail>() {
                                              new HistoryAttachmentDetail() {
                                                  File = new HistoryAttachmentFile() {
                                                      AzureFile = letterFileDefinition,
                                                      MetaTag = string.Concat(uniqueIdForNote, "_letter_", 1)
                                                  }
                                              },
    //                                          //If you want a second attachment for the same note.
    //                                          //new HistoryAttachmentDetail() {
    //                                          //    File = new HistoryAttachmentFile() {
    //                                          //        AzureFile = letterFileDefinition,
    //                                          //        MetaTag = string.Concat(uniqueIdForNote, "_letter_", 2)
    //                                          //    }
    //                                          //}
                                          }
});

BlobService blobService = new(new Azure.Storage.Blobs.BlobServiceClient(blobServiceConnectionString));

await blobService.SaveAsync(new SaveTextBlobRequest("Hello, this is the content of the text file.", letterFileDefinition.Container, blobPath, letterFileDefinition.ContentType ?? default!), CancellationToken.None);

//Vendor vendor = new();
//historyAttachmentsResults = await vendor.AddVendorHistoryNotes(blobService, rentManagerUrl, apiToken, historyAttachments);

historyAttachmentsResults = await tenant.AddTenantHistoryNotes(blobService, rentManagerUrl, apiToken, historyAttachments);


Console.WriteLine("Starting Cleaning");

await blobService.DeleteAsync(new DeleteBlobRequest(letterFileDefinition), CancellationToken.None);

Console.WriteLine("Done");