using System;
using System.IO;
using System.Collections.Generic;
using StellaOps.Feedser.Exporter.Json;

namespace StellaOps.Feedser.Exporter.TrivyDb;

public sealed class TrivyDbExportOptions
{
    public string OutputRoot { get; set; } = Path.Combine("exports", "trivy");

    public string ReferencePrefix { get; set; } = "feedser/trivy";

    public string TagFormat { get; set; } = "yyyyMMdd'T'HHmmss'Z'";

    public string DatabaseVersionFormat { get; set; } = "yyyyMMdd'T'HHmmss'Z'";

    public bool KeepWorkingTree { get; set; }

    public string? TargetRepository { get; set; }

    public JsonExportOptions Json { get; set; } = new()
    {
        OutputRoot = Path.Combine("exports", "trivy", "tree")
    };

    public TrivyDbBuilderOptions Builder { get; set; } = new();

    public TrivyDbOrasOptions Oras { get; set; } = new();

    public TrivyDbOfflineBundleOptions OfflineBundle { get; set; } = new();

    public string GetExportRoot(string exportId)
    {
        ArgumentException.ThrowIfNullOrEmpty(exportId);
        var root = Path.GetFullPath(OutputRoot);
        return Path.Combine(root, exportId);
    }
}

public sealed class TrivyDbBuilderOptions
{
    public string ExecutablePath { get; set; } = "trivy-db";

    public string? WorkingDirectory { get; set; }

    public TimeSpan UpdateInterval { get; set; } = TimeSpan.FromHours(24);

    public List<string> OnlyUpdateTargets { get; set; } = new();

    public Dictionary<string, string> Environment { get; set; } = new(StringComparer.OrdinalIgnoreCase);

    public bool InheritEnvironment { get; set; } = true;
}

public sealed class TrivyDbOrasOptions
{
    public bool Enabled { get; set; }

    public string ExecutablePath { get; set; } = "oras";

    public bool PublishFull { get; set; } = true;

    public bool PublishDelta { get; set; } = true;

    public string? WorkingDirectory { get; set; }

    public bool InheritEnvironment { get; set; } = true;

    public List<string> AdditionalArguments { get; set; } = new();

    public Dictionary<string, string> Environment { get; set; } = new(StringComparer.OrdinalIgnoreCase);

    public bool SkipTlsVerify { get; set; }

    public bool UseHttp { get; set; }
}

public sealed class TrivyDbOfflineBundleOptions
{
    public bool Enabled { get; set; }

    public bool IncludeFull { get; set; } = true;

    public bool IncludeDelta { get; set; } = true;

    public string? FileName { get; set; }
}
