using System.Text.Json;

namespace URLNotesGrabberCORE
{
    // Port of ThreeTxtFileHelper/UpdateBlogPaths.cs. Reads .tumblr / .tmblrpriv metadata
    // files from a root\Index folder and populates Blogs.TTFolderPath in TL.db.
    public static class UpdateBlogPathsRunner
    {
        public static int Run(string rootPath)
        {
            if (string.IsNullOrWhiteSpace(rootPath))
            {
                Console.WriteLine("UpdateBlogPaths: rootPath is required.");
                return 1;
            }

            DataAccess.EnsureTTFileHelperColumnsExist();

            string indexPath = Path.Combine(rootPath, "Index");
            if (!Directory.Exists(indexPath))
            {
                Console.WriteLine($"Index folder not found at: {indexPath}");
                return 1;
            }

            Console.WriteLine($"Scanning Index folder: {indexPath}");

            var blogFiles = Directory.GetFiles(indexPath, "*.tumblr")
                .Concat(Directory.GetFiles(indexPath, "*.tmblrpriv"))
                .ToList();

            Console.WriteLine($"Found {blogFiles.Count} blog metadata files");

            int updatedCount = 0;

            foreach (var blogFile in blogFiles)
            {
                try
                {
                    string blogName = Path.GetFileNameWithoutExtension(blogFile);
                    string jsonContent = File.ReadAllText(blogFile);
                    using JsonDocument doc = JsonDocument.Parse(jsonContent);
                    JsonElement root = doc.RootElement;

                    if (root.TryGetProperty("FileDownloadLocation", out JsonElement locationElement))
                    {
                        string? fileDownloadLocation = locationElement.GetString();
                        if (!string.IsNullOrWhiteSpace(fileDownloadLocation))
                        {
                            DataAccess.SetBlogTTFolderPath(blogName, fileDownloadLocation);
                            updatedCount++;
                            Console.WriteLine($"Updated {blogName}: {fileDownloadLocation}");
                        }
                    }
                    else
                    {
                        Console.WriteLine($"No FileDownloadLocation found in {blogFile}");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error processing {blogFile}: {ex.Message}");
                }
            }

            Console.WriteLine($"\nUpdated {updatedCount} blogs with TTFolderPath");
            return 0;
        }
    }
}
