import { describe, it, expect } from 'vitest';
import { ROOM_TEMPLATES, templateToCreateRoomDto } from '../roomTemplates';

describe('ROOM_TEMPLATES', () => {
  it('has at least 5 templates', () => {
    expect(ROOM_TEMPLATES.length).toBeGreaterThanOrEqual(5);
  });

  it('all templates have required fields', () => {
    for (const template of ROOM_TEMPLATES) {
      expect(template.id).toBeTruthy();
      expect(template.name).toBeTruthy();
      expect(template.description).toBeTruthy();
      expect(template.defaultWidth).toBeGreaterThan(0);
      expect(template.defaultHeight).toBeGreaterThan(0);
      expect(template.wallHeight).toBeGreaterThan(0);
      expect(template.shape.length).toBeGreaterThanOrEqual(3);
    }
  });

  it('has unique template ids', () => {
    const ids = ROOM_TEMPLATES.map((t) => t.id);
    expect(new Set(ids).size).toBe(ids.length);
  });

  it('shapes form closed polygons with correct dimensions', () => {
    for (const template of ROOM_TEMPLATES) {
      const xs = template.shape.map((p) => p.x);
      const ys = template.shape.map((p) => p.y);
      const width = Math.max(...xs) - Math.min(...xs);
      const height = Math.max(...ys) - Math.min(...ys);
      expect(width).toBeCloseTo(template.defaultWidth);
      expect(height).toBeCloseTo(template.defaultHeight);
    }
  });
});

describe('templateToCreateRoomDto', () => {
  it('uses template name by default', () => {
    const dto = templateToCreateRoomDto(ROOM_TEMPLATES[0]);
    expect(dto.name).toBe(ROOM_TEMPLATES[0].name);
  });

  it('uses custom name when provided', () => {
    const dto = templateToCreateRoomDto(ROOM_TEMPLATES[0], 'Custom Name');
    expect(dto.name).toBe('Custom Name');
  });

  it('includes shape from template', () => {
    const template = ROOM_TEMPLATES[0];
    const dto = templateToCreateRoomDto(template);
    expect(dto.shape).toHaveLength(template.shape.length);
    expect(dto.width).toBe(template.defaultWidth);
    expect(dto.height).toBe(template.defaultHeight);
    expect(dto.wallHeight).toBe(template.wallHeight);
  });

  it('uses custom order', () => {
    const dto = templateToCreateRoomDto(ROOM_TEMPLATES[0], undefined, 5);
    expect(dto.order).toBe(5);
  });

  it('defaults order to 0', () => {
    const dto = templateToCreateRoomDto(ROOM_TEMPLATES[0]);
    expect(dto.order).toBe(0);
  });
});
