import { describe, it, expect } from 'vitest';
import { CreateSessionSchema, RevokeSessionSchema } from './types.js';
import type { SessionDoc, SessionPlatform } from './types.js';

describe('CreateSessionSchema', () => {
  it('accepts valid session input with platform', () => {
    const result = CreateSessionSchema.safeParse({ platform: 'ios', deviceId: 'dev_123' });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.platform).toBe('ios');
      expect(result.data.deviceId).toBe('dev_123');
    }
  });

  it('defaults platform to unknown', () => {
    const result = CreateSessionSchema.safeParse({});
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.platform).toBe('unknown');
    }
  });

  it('accepts all valid platforms', () => {
    const platforms: SessionPlatform[] = ['ios', 'android', 'desktop', 'web', 'watch', 'unknown'];
    for (const platform of platforms) {
      const result = CreateSessionSchema.safeParse({ platform });
      expect(result.success).toBe(true);
    }
  });

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

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

  it('accepts reason string', () => {
    const result = RevokeSessionSchema.safeParse({ reason: 'Suspicious activity' });
    expect(result.success).toBe(true);
  });

  it('rejects reason over 200 chars', () => {
    const result = RevokeSessionSchema.safeParse({ reason: 'x'.repeat(201) });
    expect(result.success).toBe(false);
  });
});

describe('SessionDoc type coverage', () => {
  it('should allow building a valid session document', () => {
    const now = new Date().toISOString();
    const session: SessionDoc = {
      id: 'ses_123',
      productId: 'lysnrai',
      userId: 'usr_abc',
      platform: 'web',
      ipAddress: '192.168.1.1',
      userAgent: 'Mozilla/5.0',
      lastActiveAt: now,
      createdAt: now,
      expiresAt: new Date(Date.now() + 7 * 86400000).toISOString(),
    };
    expect(session.id).toBe('ses_123');
    expect(session.revokedAt).toBeUndefined();
  });

  it('should allow deviceId and revokedAt optional fields', () => {
    const now = new Date().toISOString();
    const session: SessionDoc = {
      id: 'ses_456',
      productId: 'chronomind',
      userId: 'usr_def',
      deviceId: 'dev_789',
      platform: 'ios',
      ipAddress: '10.0.0.1',
      userAgent: 'ChronoMind/1.0',
      lastActiveAt: now,
      createdAt: now,
      expiresAt: now,
      revokedAt: now,
    };
    expect(session.deviceId).toBe('dev_789');
    expect(session.revokedAt).toBe(now);
  });
});
