namespace ClaudeDo.Data.Models;

public sealed class AppSettingsEntity
{
    public const int SingletonId = 1;

    public int Id { get; set; } = SingletonId;

    // Persistent counter backing TaskNumberAllocator: the next task number to hand out. Lives on
    // the singleton row so allocation is a single atomic UPDATE...RETURNING in the task's own
    // insert transaction, never a MAX(number)+1 recompute.
    public int NextTaskNumber { get; set; } = 1;

    public string DefaultClaudeInstructions { get; set; } = string.Empty;
    public string DefaultModel { get; set; } = "sonnet";
    public int DefaultMaxTurns { get; set; } = 40;
    public string DefaultPermissionMode { get; set; } = "auto";

    // Hard ceiling every resolved max-turns value (task/list/global) is clamped to before a run
    // starts. Guards against runaway sessions regardless of what a task/list override requests.
    public int MaxTurnsCeiling { get; set; } = 80;

    public int MaxParallelExecutions { get; set; } = 1;

    public string WorktreeStrategy { get; set; } = "sibling";
    public string? CentralWorktreeRoot { get; set; }
    public bool WorktreeAutoCleanupEnabled { get; set; }
    public int WorktreeAutoCleanupDays { get; set; } = 7;

    // JSON array of parent folders remembered by the repo-import modal.
    public string? RepoImportFolders { get; set; }
    // JSON array of path prefixes whose sessions are excluded from the weekly report.
    public string? ReportExcludedPaths { get; set; }
    // DayOfWeek the standup happens on; default Wednesday. Drives the report's default range.
    public int StandupWeekday { get; set; } = (int)DayOfWeek.Wednesday;

    // Max number of open tasks the daily prep ("Prime Claude") may place in MyDay.
    public int DailyPrepMaxTasks { get; set; } = 5;

    // JSON array of session skill names applied by default to new tasks.
    public string? SessionSkills { get; set; }

    // JSON array of ModelPreset rows (model → effort + max turns). Supplies the global effort and
    // turn defaults per model; list-/task-level max-turns overrides still win. Null = ship defaults.
    public string? ModelPresets { get; set; }

    // Percentage of the 5h/7d Claude usage window at which the autonomous queue pauses. 0 = gate off.
    public int UsageGateFiveHourPct { get; set; } = 80;
    public int UsageGateSevenDayPct { get; set; } = 90;

    // Percentage at which the queue starts throttling parallelism ahead of the hard gate above.
    // Tracked per bucket, because the 5h and 7d windows fill at very different rates — soft caps
    // parallelism at 2 slots, hard at 1. 0 = that stage off for that bucket.
    public int UsageThrottleFiveHourSoftPct { get; set; } = 50;
    public int UsageThrottleFiveHourHardPct { get; set; } = 65;
    public int UsageThrottleSevenDaySoftPct { get; set; } = 50;
    public int UsageThrottleSevenDayHardPct { get; set; } = 65;

    // Off by default. When on: a task that failed on the Anthropic usage limit gets exactly one
    // automatic ContinueTask once the 5h window resets, and the gate-blocked queue wakes exactly
    // at that reset time instead of only on the 30s backstop.
    public bool AutoContinueOnUsageLimit { get; set; }
}
