import { describe, expect, it, vi } from 'vitest';
import { configSchema } from '../config/schema.js';
import { buildPresetCronJobs } from './presets.js';

describe('buildPresetCronJobs', () => {
  it('creates a daily briefing preset cron job when enabled with output', () => {
    const config = configSchema.parse({
      telegram: { bot_token: 'token', allowed_chat_ids: [1] },
      models: { default: { provider: 'anthropic', model: 'claude-sonnet' } },
      automation: {
        daily_briefing: {
          enabled: true,
          schedule: '0 7 * * *',
          timezone: 'America/New_York',
          dedupe_per_local_day: true,
          output: { channel: 'telegram', peer: '1' },
          model_tier: 'fast',
          prompt: 'Daily briefing prompt',
        },
      },
    });

    const jobs = buildPresetCronJobs(config);
    expect(jobs).toHaveLength(1);
    expect(jobs[0]).toMatchObject({
      name: 'daily-briefing',
      schedule: '0 7 * * *',
      timezone: 'America/New_York',
      output: { channel: 'telegram', peer: '1' },
      model_tier: 'fast',
      message: 'Daily briefing prompt',
      once_per_local_day: true,
    });
  });

  it('can disable daily briefing per-local-day dedupe', () => {
    const config = configSchema.parse({
      telegram: { bot_token: 'token', allowed_chat_ids: [1] },
      models: { default: { provider: 'anthropic', model: 'claude-sonnet' } },
      automation: {
        daily_briefing: {
          enabled: true,
          dedupe_per_local_day: false,
          output: { channel: 'telegram', peer: '1' },
        },
      },
    });

    const jobs = buildPresetCronJobs(config);
    expect(jobs).toHaveLength(1);
    expect(jobs[0].once_per_local_day).toBe(false);
  });

  it('skips daily briefing job when output is missing', () => {
    const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
    const config = configSchema.parse({
      telegram: { bot_token: 'token', allowed_chat_ids: [1] },
      models: { default: { provider: 'anthropic', model: 'claude-sonnet' } },
      automation: {
        daily_briefing: {
          enabled: true,
        },
      },
    });

    const jobs = buildPresetCronJobs(config);
    expect(jobs).toHaveLength(0);
    expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('output is missing'));
    warnSpy.mockRestore();
  });

  it('skips preset when daily briefing name conflicts with user cron job', () => {
    const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
    const config = configSchema.parse({
      telegram: { bot_token: 'token', allowed_chat_ids: [1] },
      models: { default: { provider: 'anthropic', model: 'claude-sonnet' } },
      automation: {
        cron: [{
          name: 'daily-briefing',
          schedule: '0 9 * * *',
          message: 'manual job',
          output: { channel: 'telegram', peer: '1' },
        }],
        daily_briefing: {
          enabled: true,
          output: { channel: 'telegram', peer: '1' },
        },
      },
    });

    const jobs = buildPresetCronJobs(config);
    expect(jobs).toHaveLength(0);
    expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('conflicts with automation.cron'));
    warnSpy.mockRestore();
  });
});
