export type LoopStatus =
  | "IDLE"
  | "RUNNING"
  | "WAITING_DELAY"
  | "STOPPED"
  | "ERROR"
  | "COMPLETED";

export type EnforcementMode = "aggressive-soft";

export interface User {
  username: string;
  passwordHash: string;
  createdAt: string;
}

export interface Settings {
  pollIntervalMs: number;
  enforceIntervalMs: number;
  defaultDelayMs: number;
  maxLoopLimit: number;
}

export interface LoopJob {
  id: string;
  torrentHash: string;
  name: string;
  sizeBytes: number;
  magnet?: string;
  torrentFilePath?: string;
  allowIp: string;
  targetLoops: number;
  doneLoops: number;
  delayMs: number;
  profileName?: string;
  profileId?: string;
  deleteDataBetweenLoops: boolean;
  enforcementMode: EnforcementMode;
  status: LoopStatus;
  currentRun?: {
    startedAt: string;
    lastProgress: number;
    lastProgressAt?: string;
    stalledSince?: string;
    downloadedThisRunBytes: number;
    avgSpeed: number;
  };
  totals: {
    totalDownloadedBytes: number;
    totalTimeMs: number;
  };
  bans: {
    bannedIps: string[];
    lastBanAt?: string;
  };
  nextRunAt?: string;
  createdAt: string;
  updatedAt: string;
  lastError?: string;
}

export interface Profile {
  id: string;
  name: string;
  allowIp: string;
  delayMs: number;
  profileName?: string;
  profileId?: string;
  targetLoops: number;
  createdAt: string;
}

export interface TimerRule {
  id: string;
  tags: string[];
  seedLimitSeconds: number;
  createdAt: string;
}

export interface TimerLog {
  id: string;
  hash: string;
  name: string;
  sizeBytes: number;
  tracker?: string;
  tags: string[];
  category?: string;
  seedingTimeSeconds: number;
  uploadedBytes: number;
  deletedAt: string;
}

export interface TimerSummary {
  totalDeleted: number;
  totalSeededSeconds: number;
  totalUploadedBytes: number;
  updatedAt: string;
}

export interface AuditLog {
  id: string;
  level: "INFO" | "WARN" | "ERROR";
  event:
    | "JOB_STARTED"
    | "JOB_COMPLETED_LOOP"
    | "JOB_COMPLETED_ALL"
    | "JOB_RESTARTED"
    | "PEER_BANNED"
    | "QBIT_RELOGIN"
    | "ARCHIVE_SUCCESS"
    | "ARCHIVE_FAIL";
  message: string;
  createdAt: string;
}

export interface ArchiveStatus {
  hash: string;
  status: "PENDING" | "READY" | "FAILED" | "MISSING";
  torrentFilePath?: string;
  source?: "manual" | "generated" | "exported";
  lastError?: string;
  updatedAt: string;
}

export interface DbSchema {
  users: User[];
  settings: Settings;
  loopJobs: LoopJob[];
  profiles: Profile[];
  auditLogs: AuditLog[];
  archives: Record<string, ArchiveStatus>;
  timerRules?: TimerRule[];
  timerLogs?: TimerLog[];
  timerSummary?: TimerSummary;
}
