import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { mkdtempSync, rmSync } from 'fs';
import { join } from 'path';
import { tmpdir } from 'os';
import { Repository } from './db/repository.js';
import type { PushSubscriptionRecord } from './db/repository.js';
import { VapidKeyStore } from './vapid-store.js';
import { buildPushPayload, PushService, type PushPayload } from './push-service.js';

const SUBJECT = 'https://aao.example/';

// We mock web-push at module level so individual tests can change its behavior.
vi.mock('web-push', () => {
  const sendNotification = vi.fn();
  return {
    default: {
      sendNotification,
      setVapidDetails: vi.fn(),
      generateVAPIDKeys: () => ({
        publicKey: 'BFakeFakeFakeFakeFakeFakeFakeFakeFakeFakeFakeFakeFakeFakeFakeFakeFakeFakeFakeFakeFakeFakeFakeFakeFakeFa',
        privateKey: 'fakefakefakefakefakefakefakefakefakefakefa',
      }),
    },
  };
});

import webPush from 'web-push';
const sendMock = webPush.sendNotification as unknown as ReturnType<typeof vi.fn>;

function makePushError(statusCode: number, message = 'mock'): Error & { statusCode: number } {
  const err = Object.assign(new Error(message), { statusCode });
  return err;
}

describe('buildPushPayload', () => {
  const base: PushPayload = {
    event: 'succeeded',
    taskId: 42,
    taskTitle: 'Long Important Task Title',
    pieceName: 'chat',
    ownerId: 'user-1',
  };

  it('includes title + pieceName when includeDetails=true and fits budget', () => {
    const json = buildPushPayload(base, true, 3072);
    const parsed = JSON.parse(json);
    expect(parsed.title).toContain('Long Important Task Title');
    expect(parsed.body).toBe('chat');
    expect(parsed.tag).toBe('task-42-succeeded');
    expect(parsed.data.taskId).toBe(42);
  });

  it('falls back to generic title when includeDetails=false', () => {
    const json = buildPushPayload(base, false, 3072);
    const parsed = JSON.parse(json);
    expect(parsed.title).not.toContain('Long Important Task Title');
    expect(parsed.title).toContain('#42');
    expect(parsed.body).not.toBe('chat');
  });

  it('falls back to generic when even trimmed detailed payload exceeds budget', () => {
    const huge = { ...base, taskTitle: 'x'.repeat(5_000), pieceName: 'y'.repeat(5_000) };
    // 100 bytes is too tight for any title-with-emoji + body — forces generic fallback
    const json = buildPushPayload(huge, true, 100);
    const parsed = JSON.parse(json);
    expect(parsed.title).toContain('#42');
    expect(parsed.body).not.toContain('y');
  });

  it('trims long detailed payload before falling back', () => {
    const huge = { ...base, taskTitle: 'x'.repeat(500), pieceName: 'y'.repeat(500) };
    // 300 bytes fits the trimmed branch (60-char title + 40-char body + boilerplate)
    const json = buildPushPayload(huge, true, 300);
    const parsed = JSON.parse(json);
    expect(parsed.title).toContain('xxxxx');
    expect(parsed.title.length).toBeLessThan(200);
  });

  it('uses correct emoji for each event', () => {
    expect(JSON.parse(buildPushPayload({ ...base, event: 'running' }, false, 3072)).title).toContain('🟢');
    expect(JSON.parse(buildPushPayload({ ...base, event: 'failed' }, false, 3072)).title).toContain('❌');
    expect(JSON.parse(buildPushPayload({ ...base, event: 'waiting_human' }, false, 3072)).title).toContain('❓');
  });
});

describe('PushService', () => {
  let tempDir = '';
  let repo: Repository;
  let store: VapidKeyStore;
  let service: PushService;
  let userId = '';

  function seedSubscription(opts: { vapidKeyId?: string } = {}): PushSubscriptionRecord {
    const current = store.getCurrent();
    const { id } = repo.upsertPushSubscription({
      userId,
      endpoint: 'https://push.example/a',
      p256dh: 'pubkey',
      auth: 'authsec',
      vapidKeyId: opts.vapidKeyId ?? current.keyId,
    });
    return repo.getPushSubscriptionById(id)!;
  }

  beforeEach(() => {
    tempDir = mkdtempSync(join(tmpdir(), 'maestro-push-'));
    repo = new Repository(join(tempDir, 'db.sqlite'));
    store = new VapidKeyStore(join(tempDir, 'vapid.json'), join(tempDir, 'vapid-history'));
    store.loadOrGenerate(SUBJECT);
    service = new PushService(repo, store, { perSendTimeoutMs: 1_000 });
    const user = repo.createUser({ email: 'u@example.com', name: 'u', role: 'user', status: 'active' });
    userId = user.id;
    sendMock.mockReset();
  });

  afterEach(() => {
    repo.close();
    if (tempDir) {
      rmSync(tempDir, { recursive: true, force: true });
      tempDir = '';
    }
  });

  const payload = (): PushPayload => ({
    event: 'succeeded',
    taskId: 1,
    taskTitle: 'T',
    pieceName: 'chat',
    ownerId: userId,
  });

  it('skips silently when ownerId is null', async () => {
    await service.sendPushToUser({ ...payload(), ownerId: null });
    expect(sendMock).not.toHaveBeenCalled();
  });

  it('skips when master enabled=false', async () => {
    seedSubscription();
    repo.upsertUserNotificationPrefs(userId, { enabled: false });
    await service.sendPushToUser(payload());
    expect(sendMock).not.toHaveBeenCalled();
  });

  it('skips when the specific event is OFF', async () => {
    seedSubscription();
    repo.upsertUserNotificationPrefs(userId, { events: { succeeded: false } } as never);
    await service.sendPushToUser(payload());
    expect(sendMock).not.toHaveBeenCalled();
  });

  it('no-op when user has zero subscriptions', async () => {
    await service.sendPushToUser(payload());
    expect(sendMock).not.toHaveBeenCalled();
  });

  it('on 201 success: marks success and resets failure_count', async () => {
    const sub = seedSubscription();
    repo.markPushSubscriptionFailure(sub.id);
    sendMock.mockResolvedValueOnce({ statusCode: 201 });
    await service.sendPushToUser(payload());
    const after = repo.getPushSubscriptionById(sub.id)!;
    expect(after.failureCount).toBe(0);
    expect(after.lastSuccessAt).toBeTruthy();
  });

  it('on 410 Gone: deletes the subscription', async () => {
    const sub = seedSubscription();
    sendMock.mockRejectedValueOnce(makePushError(410));
    await service.sendPushToUser(payload());
    expect(repo.getPushSubscriptionById(sub.id)).toBeNull();
  });

  it('on 404: deletes the subscription', async () => {
    const sub = seedSubscription();
    sendMock.mockRejectedValueOnce(makePushError(404));
    await service.sendPushToUser(payload());
    expect(repo.getPushSubscriptionById(sub.id)).toBeNull();
  });

  it('on 413: keeps subscription, increments failure_count, no retry', async () => {
    const sub = seedSubscription();
    sendMock.mockRejectedValueOnce(makePushError(413));
    await service.sendPushToUser(payload());
    const after = repo.getPushSubscriptionById(sub.id)!;
    expect(after.failureCount).toBe(1);
    expect(sendMock).toHaveBeenCalledTimes(1);
  });

  it('on 401: keeps subscription (operator alert), no delete, no retry', async () => {
    const sub = seedSubscription();
    sendMock.mockRejectedValueOnce(makePushError(401));
    await service.sendPushToUser(payload());
    expect(repo.getPushSubscriptionById(sub.id)).not.toBeNull();
    expect(repo.getPushSubscriptionById(sub.id)!.failureCount).toBe(1);
    expect(sendMock).toHaveBeenCalledTimes(1);
  });

  it('on 403: same handling as 401', async () => {
    const sub = seedSubscription();
    sendMock.mockRejectedValueOnce(makePushError(403));
    await service.sendPushToUser(payload());
    expect(repo.getPushSubscriptionById(sub.id)).not.toBeNull();
  });

  it('on 429 then 200: retries and eventually succeeds', async () => {
    const sub = seedSubscription();
    sendMock
      .mockRejectedValueOnce(makePushError(429))
      .mockResolvedValueOnce({ statusCode: 200 });
    await service.sendPushToUser(payload());
    expect(sendMock).toHaveBeenCalledTimes(2);
    const after = repo.getPushSubscriptionById(sub.id)!;
    expect(after.failureCount).toBe(0);
    expect(after.lastSuccessAt).toBeTruthy();
  }, 10_000);

  it('on persistent 503: gives up after retries with failure_count', async () => {
    const sub = seedSubscription();
    sendMock.mockRejectedValue(makePushError(503));
    await service.sendPushToUser(payload());
    // initial + 2 retries = 3 sends
    expect(sendMock).toHaveBeenCalledTimes(3);
    const after = repo.getPushSubscriptionById(sub.id)!;
    expect(after).not.toBeNull();
    expect(after.failureCount).toBe(1);
  }, 15_000);

  it('on send timeout: marks failure', async () => {
    const sub = seedSubscription();
    sendMock.mockImplementation(() => new Promise(() => { /* never resolve */ }));
    const fastService = new PushService(repo, store, { perSendTimeoutMs: 50 });
    await fastService.sendPushToUser(payload());
    const after = repo.getPushSubscriptionById(sub.id)!;
    // non-retriable error code (undefined) → 1 attempt only
    expect(after.failureCount).toBe(1);
  });

  it('VAPID keyId not in current or history: marks failure, no send', async () => {
    seedSubscription({ vapidKeyId: 'nonexistent-id' });
    await service.sendPushToUser(payload());
    expect(sendMock).not.toHaveBeenCalled();
  });

  it('enqueue() is fire-and-forget (worker never awaits)', async () => {
    const sub = seedSubscription();
    sendMock.mockResolvedValue({ statusCode: 201 });
    service.enqueue(payload());
    await service.waitIdle();
    const after = repo.getPushSubscriptionById(sub.id)!;
    expect(after.lastSuccessAt).toBeTruthy();
  });

  it('enqueue() does not throw when ownerId is null', () => {
    expect(() => service.enqueue({ ...payload(), ownerId: null })).not.toThrow();
  });
});
