import { existsSync, mkdirSync, readFileSync, writeFileSync, readdirSync } from 'node:fs';
import { homedir } from 'node:os';
import { j as join } from './ccjk.bQ7Dh1g4.mjs';

class StatsStorage {
  baseDir;
  recordsDir;
  dailyDir;
  constructor(baseDir) {
    this.baseDir = baseDir || join(homedir(), ".ccjk", "stats");
    this.recordsDir = join(this.baseDir, "records");
    this.dailyDir = join(this.baseDir, "daily");
    this.ensureDirectories();
  }
  /**
   * Ensure storage directories exist
   */
  ensureDirectories() {
    for (const dir of [this.baseDir, this.recordsDir, this.dailyDir]) {
      if (!existsSync(dir)) {
        mkdirSync(dir, { recursive: true });
      }
    }
  }
  /**
   * Get file path for a specific date
   */
  getRecordFilePath(date) {
    return join(this.recordsDir, `${date}.json`);
  }
  /**
   * Get daily stats file path for a specific date
   */
  getDailyStatsFilePath(date) {
    return join(this.dailyDir, `${date}.json`);
  }
  /**
   * Format date as YYYY-MM-DD
   */
  formatDate(timestamp) {
    const date = new Date(timestamp);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, "0");
    const day = String(date.getDate()).padStart(2, "0");
    return `${year}-${month}-${day}`;
  }
  /**
   * Save a request record
   */
  saveRecord(record) {
    const date = this.formatDate(record.timestamp);
    const filePath = this.getRecordFilePath(date);
    let records = [];
    if (existsSync(filePath)) {
      try {
        const content = readFileSync(filePath, "utf-8");
        records = JSON.parse(content);
      } catch {
        records = [];
      }
    }
    records.push(record);
    writeFileSync(filePath, JSON.stringify(records, null, 2), "utf-8");
  }
  /**
   * Get records for a specific date
   */
  getRecordsByDate(date) {
    const filePath = this.getRecordFilePath(date);
    if (!existsSync(filePath)) {
      return [];
    }
    try {
      const content = readFileSync(filePath, "utf-8");
      return JSON.parse(content);
    } catch {
      return [];
    }
  }
  /**
   * Get records for a date range
   */
  getRecordsByDateRange(startDate, endDate) {
    const records = [];
    const start = new Date(startDate);
    const end = new Date(endDate);
    const d = new Date(start);
    while (d <= end) {
      const dateStr = this.formatDate(d.getTime());
      records.push(...this.getRecordsByDate(dateStr));
      d.setDate(d.getDate() + 1);
    }
    return records;
  }
  /**
   * Get all available record dates
   */
  getAvailableDates() {
    if (!existsSync(this.recordsDir)) {
      return [];
    }
    const files = readdirSync(this.recordsDir);
    return files.filter((f) => f.endsWith(".json")).map((f) => f.replace(".json", "")).sort();
  }
  /**
   * Save daily statistics
   */
  saveDailyStats(stats) {
    const filePath = this.getDailyStatsFilePath(stats.date);
    writeFileSync(filePath, JSON.stringify(stats, null, 2), "utf-8");
  }
  /**
   * Get daily statistics for a specific date
   */
  getDailyStats(date) {
    const filePath = this.getDailyStatsFilePath(date);
    if (!existsSync(filePath)) {
      return null;
    }
    try {
      const content = readFileSync(filePath, "utf-8");
      return JSON.parse(content);
    } catch {
      return null;
    }
  }
  /**
   * Get daily statistics for a date range
   */
  getDailyStatsByDateRange(startDate, endDate) {
    const stats = [];
    const start = new Date(startDate);
    const end = new Date(endDate);
    const d = new Date(start);
    while (d <= end) {
      const dateStr = this.formatDate(d.getTime());
      const dailyStats = this.getDailyStats(dateStr);
      if (dailyStats) {
        stats.push(dailyStats);
      }
      d.setDate(d.getDate() + 1);
    }
    return stats;
  }
  /**
   * Get all available daily stats dates
   */
  getAvailableDailyStatsDates() {
    if (!existsSync(this.dailyDir)) {
      return [];
    }
    const files = readdirSync(this.dailyDir);
    return files.filter((f) => f.endsWith(".json")).map((f) => f.replace(".json", "")).sort();
  }
  /**
   * Calculate date range for a period
   */
  getDateRangeForPeriod(period) {
    const now = /* @__PURE__ */ new Date();
    const endDate = this.formatDate(now.getTime());
    if (period === "all") {
      const dates = this.getAvailableDates();
      const startDate2 = dates.length > 0 ? dates[0] : endDate;
      return { startDate: startDate2, endDate };
    }
    const days = period === "1d" ? 1 : period === "7d" ? 7 : period === "30d" ? 30 : 90;
    const start = new Date(now);
    start.setDate(start.getDate() - days + 1);
    const startDate = this.formatDate(start.getTime());
    return { startDate, endDate };
  }
  /**
   * Clean up old records (older than specified days)
   */
  cleanupOldRecords(daysToKeep) {
    const cutoffDate = /* @__PURE__ */ new Date();
    cutoffDate.setDate(cutoffDate.getDate() - daysToKeep);
    const cutoffStr = this.formatDate(cutoffDate.getTime());
    const dates = this.getAvailableDates();
    let deletedCount = 0;
    for (const date of dates) {
      if (date < cutoffStr) {
        try {
          const recordFile = this.getRecordFilePath(date);
          const dailyFile = this.getDailyStatsFilePath(date);
          if (existsSync(recordFile)) {
            deletedCount++;
          }
          if (existsSync(dailyFile)) {
          }
        } catch {
        }
      }
    }
    return deletedCount;
  }
  /**
   * Get storage statistics
   */
  getStorageStats() {
    const recordDates = this.getAvailableDates();
    const dailyDates = this.getAvailableDailyStatsDates();
    let totalRecords = 0;
    for (const date of recordDates) {
      const records = this.getRecordsByDate(date);
      totalRecords += records.length;
    }
    return {
      totalRecordFiles: recordDates.length,
      totalDailyFiles: dailyDates.length,
      oldestDate: recordDates.length > 0 ? recordDates[0] : null,
      newestDate: recordDates.length > 0 ? recordDates[recordDates.length - 1] : null,
      totalRecords
    };
  }
}
let storageInstance = null;
function getStatsStorage() {
  if (!storageInstance) {
    storageInstance = new StatsStorage();
  }
  return storageInstance;
}

export { getStatsStorage as g };
