/**
 * Tests for themes module — Zod schemas + type validation.
 */

import { describe, it, expect } from 'vitest';
import { CreateThemeSchema, UpdateThemeSchema } from './types.js';
import type { ThemeDoc, PlatformTheme } from './types.js';

describe('CreateThemeSchema', () => {
  it('accepts valid theme with just name', () => {
    const result = CreateThemeSchema.safeParse({ name: 'Dark Mode' });
    expect(result.success).toBe(true);
  });

  it('accepts full input with platform themes', () => {
    const result = CreateThemeSchema.safeParse({
      name: 'Ocean Blue',
      description: 'A cool ocean theme',
      ios: { primary: '#0000ff', background: '#000022' },
      android: { primary: '#0000ff', accent: '#00ccff' },
      desktop: { primary: '#0000ff', idle: '#00ff00', listening: '#ff0000' },
      is_active: true,
      is_default: false,
    });
    expect(result.success).toBe(true);
  });

  it('rejects empty name', () => {
    const result = CreateThemeSchema.safeParse({ name: '' });
    expect(result.success).toBe(false);
  });

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

  it('accepts null description', () => {
    const result = CreateThemeSchema.safeParse({ name: 'Test', description: null });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.description).toBeNull();
    }
  });

  it('defaults optional booleans to undefined', () => {
    const result = CreateThemeSchema.safeParse({ name: 'Test' });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.is_active).toBeUndefined();
      expect(result.data.is_default).toBeUndefined();
    }
  });

  it('accepts partial platform theme (all fields optional)', () => {
    const result = CreateThemeSchema.safeParse({
      name: 'Partial',
      ios: { primary: '#fff' },
    });
    expect(result.success).toBe(true);
  });

  it('accepts desktop-specific fields (idle, listening, processing, offline)', () => {
    const result = CreateThemeSchema.safeParse({
      name: 'Desktop Theme',
      desktop: {
        idle: '#00ff00',
        listening: '#ff0000',
        processing: '#ffaa00',
        offline: '#999999',
      },
    });
    expect(result.success).toBe(true);
  });
});

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

  it('accepts empty object (no-op update)', () => {
    const result = UpdateThemeSchema.safeParse({});
    expect(result.success).toBe(true);
  });

  it('accepts is_active toggle', () => {
    const result = UpdateThemeSchema.safeParse({ is_active: true });
    expect(result.success).toBe(true);
  });

  it('accepts platform theme update', () => {
    const result = UpdateThemeSchema.safeParse({
      ios: { primary: '#ff0000', accent: '#cc0000' },
    });
    expect(result.success).toBe(true);
  });

  it('accepts description update to null', () => {
    const result = UpdateThemeSchema.safeParse({ description: null });
    expect(result.success).toBe(true);
  });

  it('rejects empty name string', () => {
    const result = UpdateThemeSchema.safeParse({ name: '' });
    expect(result.success).toBe(false);
  });
});

describe('ThemeDoc type shape', () => {
  it('has expected fields', () => {
    const doc: ThemeDoc = {
      id: 'theme-1',
      productId: 'lysnrai',
      name: 'Default Green',
      description: 'The default theme',
      ios: {
        primary: '#4caf50',
        secondary: '#2e7d32',
        accent: '#66bb6a',
        background: '#ffffff',
        surface: '#f5f5f5',
        error: '#f44336',
        warning: '#ff9800',
        success: '#4caf50',
      },
      android: {
        primary: '#4caf50',
        secondary: '#2e7d32',
        accent: '#66bb6a',
        background: '#ffffff',
        surface: '#f5f5f5',
        error: '#f44336',
        warning: '#ff9800',
        success: '#4caf50',
      },
      desktop: {
        primary: '#4caf50',
        secondary: '#2e7d32',
        accent: '#66bb6a',
        background: '#ffffff',
        surface: '#f5f5f5',
        error: '#f44336',
        warning: '#ff9800',
        success: '#4caf50',
        idle: '#4caf50',
        listening: '#e94560',
        processing: '#f5a623',
        offline: '#9e9e9e',
      },
      is_active: true,
      is_default: true,
      version: '1.0',
      created_at: '2026-01-01T00:00:00.000Z',
      updated_at: '2026-01-01T00:00:00.000Z',
      created_by: 'admin-user',
      type: 'theme',
    };

    expect(doc.type).toBe('theme');
    expect(doc.desktop.idle).toBe('#4caf50');
    expect(doc.ios.primary).toBe('#4caf50');
  });

  it('allows null description and created_by', () => {
    const doc: ThemeDoc = {
      id: 'theme-2',
      productId: 'mindlyst',
      name: 'Minimal',
      description: null,
      ios: {} as PlatformTheme,
      android: {} as PlatformTheme,
      desktop: {} as PlatformTheme,
      is_active: false,
      is_default: false,
      version: '1.0',
      created_at: '2026-01-01T00:00:00.000Z',
      updated_at: '2026-01-01T00:00:00.000Z',
      created_by: null,
      type: 'theme',
    };

    expect(doc.description).toBeNull();
    expect(doc.created_by).toBeNull();
  });
});
