import { describe, it, expect } from 'vitest';
import { parseBackupFilename, applyGfsRetention } from '@main/services/backupRotation.js';

describe('parseBackupFilename', () => {
  it('extracts ISO date from valid filename', () => {
    expect(parseBackupFilename('inkling-2026-04-26.sqlite')).toBe('2026-04-26');
  });

  it('returns null for non-matching filename', () => {
    expect(parseBackupFilename('something-else.sqlite')).toBeNull();
    expect(parseBackupFilename('inkling-2026-13-99.sqlite')).toBeNull();
    expect(parseBackupFilename('.last-snapshot')).toBeNull();
  });

  it('returns null for date that JS would silently coerce (roundtrip lock-in)', () => {
    // Without the toISOString roundtrip check, JS coerces 2026-02-30 to 2026-03-02.
    // This test locks in the roundtrip-validation contract.
    expect(parseBackupFilename('inkling-2026-02-30.sqlite')).toBeNull();
  });
});

describe('applyGfsRetention', () => {
  // KST-naive logic — caller passes UTC `now`. Filenames are KST date keys.
  const NOW = new Date('2026-04-26T12:00:00Z'); // 2026-04-26 21:00 KST

  function names(...dates: string[]): string[] {
    return dates.map((d) => `inkling-${d}.sqlite`);
  }

  it('keeps files within last 14 days (daily window)', () => {
    const files = names(
      '2026-04-26', '2026-04-25', '2026-04-20', '2026-04-13', '2026-04-12'
    );
    const r = applyGfsRetention(files, NOW);
    // 14 day window from 2026-04-26 reaches back to 2026-04-13 inclusive.
    expect(r.keep).toContain('inkling-2026-04-26.sqlite');
    expect(r.keep).toContain('inkling-2026-04-25.sqlite');
    expect(r.keep).toContain('inkling-2026-04-20.sqlite');
    expect(r.keep).toContain('inkling-2026-04-13.sqlite');
    expect(r.remove).toContain('inkling-2026-04-12.sqlite');
  });

  it('keeps last 4 Mondays beyond the 14 day window', () => {
    // Mondays in 2026: 04-13, 04-06, 03-30, 03-23, 03-16, 03-09
    const files = names(
      '2026-04-13', // within 14-day, also a Monday
      '2026-04-06', // outside 14-day, but a Monday in last 4 weeks
      '2026-03-30', // a Monday in last 4 weeks
      '2026-03-23', // a Monday in last 4 weeks
      '2026-03-16', // a Monday more than 4 weeks ago — REMOVE unless month-1
      '2026-03-09'  // a Monday more than 4 weeks ago — REMOVE
    );
    const r = applyGfsRetention(files, NOW);
    expect(r.keep).toContain('inkling-2026-04-06.sqlite');
    expect(r.keep).toContain('inkling-2026-03-30.sqlite');
    expect(r.keep).toContain('inkling-2026-03-23.sqlite');
    expect(r.remove).toContain('inkling-2026-03-16.sqlite');
    expect(r.remove).toContain('inkling-2026-03-09.sqlite');
  });

  it('weekly window is inclusive at oldest boundary', () => {
    // 2026-03-23 is exactly 4*7 days before anchor Monday 2026-04-20.
    // Locks in the boundary semantic explicitly.
    const r = applyGfsRetention(names('2026-03-23', '2026-03-16'), NOW);
    expect(r.keep).toContain('inkling-2026-03-23.sqlite');
    expect(r.remove).toContain('inkling-2026-03-16.sqlite');
  });

  it('keeps month-firsts within last 6 months', () => {
    // Last 6 month-firsts from 2026-04-26: 2026-04-01, 2026-03-01, 2026-02-01,
    // 2026-01-01, 2025-12-01, 2025-11-01
    const files = names(
      '2026-04-01', // within 14-day already
      '2026-03-01', // outside 14-day, outside 4-week-Monday — keep via month rule
      '2026-02-01',
      '2026-01-01',
      '2025-12-01',
      '2025-11-01',
      '2025-10-01'  // outside 6-month window — REMOVE
    );
    const r = applyGfsRetention(files, NOW);
    expect(r.keep).toContain('inkling-2026-03-01.sqlite');
    expect(r.keep).toContain('inkling-2026-02-01.sqlite');
    expect(r.keep).toContain('inkling-2025-11-01.sqlite');
    expect(r.remove).toContain('inkling-2025-10-01.sqlite');
  });

  it('ignores files that do not match backup pattern', () => {
    const files = ['random.sqlite', 'inkling.sqlite', '.last-snapshot', 'inkling-bad-date.sqlite'];
    const r = applyGfsRetention(files, NOW);
    expect(r.keep).toEqual([]);
    expect(r.remove).toEqual([]);
  });

  it('keeps future-dated files (clock skew safety)', () => {
    const files = names('2030-01-01');
    const r = applyGfsRetention(files, NOW);
    expect(r.keep).toContain('inkling-2030-01-01.sqlite');
    expect(r.remove).toEqual([]);
  });

  it('a file kept by any rule is in keep, never in both lists', () => {
    const files = names('2026-04-26', '2026-04-13', '2026-03-23', '2026-03-01');
    const r = applyGfsRetention(files, NOW);
    const intersection = r.keep.filter((f) => r.remove.includes(f));
    expect(intersection).toEqual([]);
  });
});
