/**
 * In-App Feedback module — unit tests.
 */

import { describe, it, expect } from 'vitest';
import { CreateFeedbackSchema, UpdateFeedbackSchema, QueryFeedbackSchema } from './types.js';

describe('CreateFeedbackSchema', () => {
  it('accepts valid bug report', () => {
    const result = CreateFeedbackSchema.parse({
      type: 'bug',
      title: 'App crashes on timer start',
      body: 'When I tap start, the app freezes',
    });
    expect(result.type).toBe('bug');
    expect(result.title).toBe('App crashes on timer start');
    expect(result.rating).toBeNull();
    expect(result.screen).toBeNull();
  });

  it('accepts feature request with rating', () => {
    const result = CreateFeedbackSchema.parse({
      type: 'feature',
      title: 'Dark mode',
      rating: 4,
      platform: 'ios',
    });
    expect(result.rating).toBe(4);
    expect(result.platform).toBe('ios');
  });

  it('accepts praise with screen info', () => {
    const result = CreateFeedbackSchema.parse({
      type: 'praise',
      title: 'Love the timer!',
      screen: '/dashboard',
      appVersion: '1.2.0',
    });
    expect(result.screen).toBe('/dashboard');
    expect(result.appVersion).toBe('1.2.0');
  });

  it('rejects empty title', () => {
    expect(() => CreateFeedbackSchema.parse({ type: 'bug', title: '' })).toThrow();
  });

  it('rejects invalid type', () => {
    expect(() => CreateFeedbackSchema.parse({ type: 'complaint', title: 'test' })).toThrow();
  });

  it('rejects rating out of range', () => {
    expect(() => CreateFeedbackSchema.parse({ type: 'praise', title: 'ok', rating: 0 })).toThrow();
    expect(() => CreateFeedbackSchema.parse({ type: 'praise', title: 'ok', rating: 6 })).toThrow();
  });

  it('rejects title over 200 chars', () => {
    expect(() => CreateFeedbackSchema.parse({ type: 'bug', title: 'x'.repeat(201) })).toThrow();
  });
});

describe('UpdateFeedbackSchema', () => {
  it('accepts status change', () => {
    const result = UpdateFeedbackSchema.parse({ status: 'reviewed' });
    expect(result.status).toBe('reviewed');
  });

  it('accepts admin notes', () => {
    const result = UpdateFeedbackSchema.parse({ adminNotes: 'Tracked in JIRA-123' });
    expect(result.adminNotes).toBe('Tracked in JIRA-123');
  });

  it('accepts null admin notes', () => {
    const result = UpdateFeedbackSchema.parse({ adminNotes: null });
    expect(result.adminNotes).toBeNull();
  });

  it('rejects invalid status', () => {
    expect(() => UpdateFeedbackSchema.parse({ status: 'closed' })).toThrow();
  });
});

describe('QueryFeedbackSchema', () => {
  it('applies defaults', () => {
    const result = QueryFeedbackSchema.parse({});
    expect(result.limit).toBe(50);
    expect(result.offset).toBe(0);
  });

  it('accepts all filters', () => {
    const result = QueryFeedbackSchema.parse({
      type: 'bug',
      status: 'new',
      limit: '20',
      offset: '10',
    });
    expect(result.type).toBe('bug');
    expect(result.status).toBe('new');
    expect(result.limit).toBe(20);
    expect(result.offset).toBe(10);
  });

  it('rejects limit over 100', () => {
    expect(() => QueryFeedbackSchema.parse({ limit: '101' })).toThrow();
  });
});

// ─────────────────────────────────────────────────────────────────────────────
// Screenshot Support Tests (Phase 1.5)
// ─────────────────────────────────────────────────────────────────────────────

describe('CreateFeedbackSchema with screenshot', () => {
  it('accepts feedback with screenshot metadata', () => {
    const result = CreateFeedbackSchema.parse({
      type: 'bug',
      title: 'Crash with screenshot',
      body: 'App crashes when I tap this button',
      screenshotBlobPath: 'feedback/test_product/fb_123/screenshot_abc.png',
      screenshotContentType: 'image/png',
      screenshotSizeBytes: 1024000,
      deviceContext: {
        osVersion: 'iOS 17.4',
        appVersion: '2.3.1',
        deviceModel: 'iPhone15,2',
        screenResolution: '393x852',
        locale: 'en-US',
      },
    });
    expect(result.screenshotBlobPath).toBe('feedback/test_product/fb_123/screenshot_abc.png');
    expect(result.screenshotContentType).toBe('image/png');
    expect(result.screenshotSizeBytes).toBe(1024000);
    expect(result.deviceContext?.osVersion).toBe('iOS 17.4');
  });

  it('accepts feedback without screenshot (backward compatible)', () => {
    const result = CreateFeedbackSchema.parse({
      type: 'feature',
      title: 'Add dark mode',
    });
    expect(result.screenshotBlobPath).toBeUndefined();
    expect(result.deviceContext).toBeUndefined();
  });

  it('rejects invalid screenshot content type', () => {
    expect(() =>
      CreateFeedbackSchema.parse({
        type: 'bug',
        title: 'test',
        screenshotContentType: 'image/gif', // not allowed
      })
    ).toThrow();
  });

  it('rejects screenshot over 10MB', () => {
    expect(() =>
      CreateFeedbackSchema.parse({
        type: 'bug',
        title: 'test',
        screenshotSizeBytes: 11 * 1024 * 1024, // 11MB
      })
    ).toThrow();
  });

  it('accepts webp screenshots', () => {
    const result = CreateFeedbackSchema.parse({
      type: 'bug',
      title: 'test',
      screenshotContentType: 'image/webp',
      screenshotSizeBytes: 500000,
    });
    expect(result.screenshotContentType).toBe('image/webp');
  });
});
