/**
 * Tests for public roadmap schemas.
 */

import { describe, it, expect } from 'vitest';
import { PublicSubmitSchema, PublicVoteSchema, PublicRoadmapQuerySchema } from './types.js';

describe('PublicSubmitSchema', () => {
  it('accepts valid submission with all fields', () => {
    const result = PublicSubmitSchema.safeParse({
      type: 'feature',
      title: 'Add dark mode',
      description: 'Would love a dark theme for the dashboard',
      email: 'user@example.com',
      name: 'Jane Doe',
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.type).toBe('feature');
      expect(result.data.priority).toBe('medium');
      expect(result.data.email).toBe('user@example.com');
    }
  });

  it('defaults type to feature and priority to medium', () => {
    const result = PublicSubmitSchema.safeParse({
      title: 'Something cool',
      email: 'test@test.com',
      name: 'Test User',
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.type).toBe('feature');
      expect(result.data.priority).toBe('medium');
      expect(result.data.description).toBe('');
    }
  });

  it('rejects missing email', () => {
    const result = PublicSubmitSchema.safeParse({
      title: 'Missing email',
      name: 'Test',
    });
    expect(result.success).toBe(false);
  });

  it('rejects invalid email', () => {
    const result = PublicSubmitSchema.safeParse({
      title: 'Bad email',
      email: 'not-an-email',
      name: 'Test',
    });
    expect(result.success).toBe(false);
  });

  it('rejects missing name', () => {
    const result = PublicSubmitSchema.safeParse({
      title: 'Missing name',
      email: 'test@test.com',
    });
    expect(result.success).toBe(false);
  });

  it('rejects empty title', () => {
    const result = PublicSubmitSchema.safeParse({
      title: '',
      email: 'test@test.com',
      name: 'Test',
    });
    expect(result.success).toBe(false);
  });

  it('accepts bug type', () => {
    const result = PublicSubmitSchema.safeParse({
      type: 'bug',
      title: 'Button broken',
      email: 'test@test.com',
      name: 'Tester',
    });
    expect(result.success).toBe(true);
    if (result.success) expect(result.data.type).toBe('bug');
  });

  it('caps description at 5000 chars', () => {
    const result = PublicSubmitSchema.safeParse({
      title: 'Long desc',
      description: 'a'.repeat(5001),
      email: 'test@test.com',
      name: 'Test',
    });
    expect(result.success).toBe(false);
  });
});

describe('PublicVoteSchema', () => {
  it('accepts valid email', () => {
    const result = PublicVoteSchema.safeParse({ email: 'voter@example.com' });
    expect(result.success).toBe(true);
  });

  it('rejects missing email', () => {
    const result = PublicVoteSchema.safeParse({});
    expect(result.success).toBe(false);
  });

  it('rejects invalid email', () => {
    const result = PublicVoteSchema.safeParse({ email: 'bad' });
    expect(result.success).toBe(false);
  });
});

describe('PublicRoadmapQuerySchema', () => {
  it('applies defaults for empty query', () => {
    const result = PublicRoadmapQuerySchema.safeParse({});
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.sortBy).toBe('voteCount');
      expect(result.data.sortOrder).toBe('desc');
      expect(result.data.limit).toBe(50);
      expect(result.data.offset).toBe(0);
    }
  });

  it('accepts type filter', () => {
    const result = PublicRoadmapQuerySchema.safeParse({ type: 'feature' });
    expect(result.success).toBe(true);
  });

  it('restricts status to open/in_progress/done (no closed/wont_fix)', () => {
    expect(PublicRoadmapQuerySchema.safeParse({ status: 'open' }).success).toBe(true);
    expect(PublicRoadmapQuerySchema.safeParse({ status: 'in_progress' }).success).toBe(true);
    expect(PublicRoadmapQuerySchema.safeParse({ status: 'done' }).success).toBe(true);
    expect(PublicRoadmapQuerySchema.safeParse({ status: 'closed' }).success).toBe(false);
    expect(PublicRoadmapQuerySchema.safeParse({ status: 'wont_fix' }).success).toBe(false);
  });

  it('accepts search query', () => {
    const result = PublicRoadmapQuerySchema.safeParse({ q: 'dark mode' });
    expect(result.success).toBe(true);
  });

  it('coerces string limit/offset to numbers', () => {
    const result = PublicRoadmapQuerySchema.safeParse({ limit: '20', offset: '10' });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.limit).toBe(20);
      expect(result.data.offset).toBe(10);
    }
  });
});
