using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using ClaudeDo.Data;

namespace ClaudeDo.Installer.Core;

/// <summary>
/// Mirrors ClaudeDo.Worker.Config.WorkerConfig JSON shape.
/// Keep in sync with src/ClaudeDo.Worker/Config/WorkerConfig.cs.
/// </summary>
public sealed class InstallerWorkerConfig
{
    [JsonPropertyName("db_path")]
    public string DbPath { get; set; } = "~/.todo-app/todo.db";

    [JsonPropertyName("sandbox_root")]
    public string SandboxRoot { get; set; } = "~/.todo-app/sandbox";

    [JsonPropertyName("log_root")]
    public string LogRoot { get; set; } = "~/.todo-app/logs";

    [JsonPropertyName("worktree_root_strategy")]
    public string WorktreeRootStrategy { get; set; } = "sibling";

    [JsonPropertyName("central_worktree_root")]
    public string CentralWorktreeRoot { get; set; } = "~/.todo-app/worktrees";

    [JsonPropertyName("queue_backstop_interval_ms")]
    public int QueueBackstopIntervalMs { get; set; } = 30_000;

    [JsonPropertyName("signalr_port")]
    public int SignalRPort { get; set; } = 47_821;

    [JsonPropertyName("claude_bin")]
    public string ClaudeBin { get; set; } = "claude";

    private static readonly JsonSerializerOptions ReadOpts = new()
    {
        ReadCommentHandling = JsonCommentHandling.Skip,
        AllowTrailingCommas = true,
    };

    private static readonly JsonSerializerOptions WriteOpts = new()
    {
        WriteIndented = true,
    };

    public static InstallerWorkerConfig Load()
    {
        var path = Path.Combine(Paths.AppDataRoot(), "worker.config.json");
        if (!File.Exists(path)) return new();
        var json = File.ReadAllText(path);
        return JsonSerializer.Deserialize<InstallerWorkerConfig>(json, ReadOpts) ?? new();
    }

    public void Save()
    {
        var dir = Paths.AppDataRoot();
        Directory.CreateDirectory(dir);
        var path = Path.Combine(dir, "worker.config.json");
        var json = JsonSerializer.Serialize(this, WriteOpts);
        File.WriteAllText(path, json);
    }
}

/// <summary>
/// Mirrors ClaudeDo.Ui.AppSettings JSON shape.
/// Keep in sync with src/ClaudeDo.Ui/AppSettings.cs.
/// </summary>
public sealed class InstallerAppSettings
{
    public string DbPath { get; set; } = "~/.todo-app/todo.db";
    public string SignalRUrl { get; set; } = "http://127.0.0.1:47821/hub";

    private static readonly JsonSerializerOptions ReadOpts = new()
    {
        PropertyNameCaseInsensitive = true,
    };

    private static readonly JsonSerializerOptions WriteOpts = new()
    {
        WriteIndented = true,
    };

    public static InstallerAppSettings Load()
    {
        var path = Path.Combine(Paths.AppDataRoot(), "ui.config.json");
        if (!File.Exists(path)) return new();
        try
        {
            var json = File.ReadAllText(path);
            return JsonSerializer.Deserialize<InstallerAppSettings>(json, ReadOpts) ?? new();
        }
        catch
        {
            return new();
        }
    }

    public void Save()
    {
        var dir = Paths.AppDataRoot();
        Directory.CreateDirectory(dir);
        var path = Path.Combine(dir, "ui.config.json");
        var json = JsonSerializer.Serialize(this, WriteOpts);
        File.WriteAllText(path, json);
    }
}
