import { describe, it, expect } from 'vitest';

import { ExtractionTaskSchema, CreateTaskSchema, UpdateTaskSchema } from './types.js';

describe('ExtractionTaskSchema', () => {
  it('accepts valid task', () => {
    const result = ExtractionTaskSchema.safeParse({
      id: 'transcript-extraction',
      name: 'Transcript Extraction',
      prompt: 'Extract entities from transcripts.',
      classes: ['action_item', 'decision', 'person'],
      builtIn: true,
      productId: 'lysnrai',
    });
    expect(result.success).toBe(true);
  });

  it('rejects task without required fields', () => {
    const result = ExtractionTaskSchema.safeParse({
      id: 'test',
    });
    expect(result.success).toBe(false);
  });

  it('defaults builtIn to false', () => {
    const result = ExtractionTaskSchema.safeParse({
      id: 'custom-task',
      name: 'Custom',
      prompt: 'Extract stuff.',
      classes: ['thing'],
      productId: 'lysnrai',
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.builtIn).toBe(false);
    }
  });
});

describe('CreateTaskSchema', () => {
  it('accepts minimal create input', () => {
    const result = CreateTaskSchema.safeParse({
      id: 'my-task',
      name: 'My Task',
      prompt: 'Extract things.',
      classes: ['thing'],
    });
    expect(result.success).toBe(true);
  });

  it('accepts create with examples', () => {
    const result = CreateTaskSchema.safeParse({
      id: 'my-task',
      name: 'My Task',
      prompt: 'Extract things.',
      classes: ['thing'],
      examples: [
        {
          text: 'sample text',
          extractions: [{ extraction_class: 'thing', extraction_text: 'sample' }],
        },
      ],
    });
    expect(result.success).toBe(true);
  });
});

describe('UpdateTaskSchema', () => {
  it('accepts partial update', () => {
    const result = UpdateTaskSchema.safeParse({
      name: 'Updated Name',
    });
    expect(result.success).toBe(true);
  });

  it('accepts empty update', () => {
    const result = UpdateTaskSchema.safeParse({});
    expect(result.success).toBe(true);
  });

  it('rejects invalid classes', () => {
    const result = UpdateTaskSchema.safeParse({
      classes: [''],
    });
    expect(result.success).toBe(false);
  });
});
