/**
 * Multi-Tenant module — unit tests.
 */

import { describe, it, expect } from 'vitest';
import {
  CreateTenantSchema,
  UpdateTenantSchema,
  InviteMemberSchema,
  UpdateMemberRoleSchema,
  PLAN_LIMITS,
} from './types.js';

describe('CreateTenantSchema', () => {
  it('validates minimal tenant', () => {
    const result = CreateTenantSchema.safeParse({
      name: 'Acme Corp',
      slug: 'acme-corp',
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.plan).toBe('free');
    }
  });

  it('validates with all fields', () => {
    const result = CreateTenantSchema.safeParse({
      name: 'Enterprise Inc',
      slug: 'enterprise-inc',
      plan: 'enterprise',
      customDomain: 'app.enterprise.com',
    });
    expect(result.success).toBe(true);
  });

  it('rejects invalid slug', () => {
    expect(CreateTenantSchema.safeParse({ name: 'Test', slug: 'Invalid Slug!' }).success).toBe(
      false
    );
  });

  it('rejects short name', () => {
    expect(CreateTenantSchema.safeParse({ name: 'A', slug: 'aa' }).success).toBe(false);
  });

  it('rejects invalid plan', () => {
    expect(
      CreateTenantSchema.safeParse({ name: 'Test', slug: 'test', plan: 'ultra' }).success
    ).toBe(false);
  });
});

describe('UpdateTenantSchema', () => {
  it('validates partial update', () => {
    const result = UpdateTenantSchema.safeParse({
      name: 'New Name',
      status: 'suspended',
    });
    expect(result.success).toBe(true);
  });

  it('validates settings update', () => {
    const result = UpdateTenantSchema.safeParse({
      settings: { feature_x: true, max_items: 100 },
    });
    expect(result.success).toBe(true);
  });

  it('rejects invalid status', () => {
    expect(UpdateTenantSchema.safeParse({ status: 'deleted' }).success).toBe(false);
  });

  it('validates maxMembers', () => {
    const result = UpdateTenantSchema.safeParse({ maxMembers: 500 });
    expect(result.success).toBe(true);
  });

  it('rejects negative maxMembers', () => {
    expect(UpdateTenantSchema.safeParse({ maxMembers: 0 }).success).toBe(false);
  });
});

describe('InviteMemberSchema', () => {
  it('validates minimal invite', () => {
    const result = InviteMemberSchema.safeParse({ email: 'user@example.com' });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.role).toBe('member');
    }
  });

  it('validates with role and displayName', () => {
    const result = InviteMemberSchema.safeParse({
      email: 'admin@example.com',
      role: 'admin',
      displayName: 'Jane Admin',
    });
    expect(result.success).toBe(true);
  });

  it('rejects invalid email', () => {
    expect(InviteMemberSchema.safeParse({ email: 'not-an-email' }).success).toBe(false);
  });

  it('rejects owner role (cannot invite as owner)', () => {
    expect(InviteMemberSchema.safeParse({ email: 'test@example.com', role: 'owner' }).success).toBe(
      false
    );
  });
});

describe('UpdateMemberRoleSchema', () => {
  it('validates role change', () => {
    expect(UpdateMemberRoleSchema.safeParse({ role: 'admin' }).success).toBe(true);
    expect(UpdateMemberRoleSchema.safeParse({ role: 'member' }).success).toBe(true);
    expect(UpdateMemberRoleSchema.safeParse({ role: 'viewer' }).success).toBe(true);
  });

  it('rejects owner role (cannot be assigned)', () => {
    expect(UpdateMemberRoleSchema.safeParse({ role: 'owner' }).success).toBe(false);
  });

  it('rejects invalid role', () => {
    expect(UpdateMemberRoleSchema.safeParse({ role: 'superadmin' }).success).toBe(false);
  });
});

describe('PLAN_LIMITS', () => {
  it('has correct free plan limits', () => {
    expect(PLAN_LIMITS.free.maxMembers).toBe(5);
    expect(PLAN_LIMITS.free.maxStorageBytes).toBe(1 * 1024 * 1024 * 1024);
  });

  it('enterprise has highest limits', () => {
    expect(PLAN_LIMITS.enterprise.maxMembers).toBe(10000);
    expect(PLAN_LIMITS.enterprise.maxStorageBytes).toBeGreaterThan(PLAN_LIMITS.pro.maxStorageBytes);
  });

  it('plans scale correctly', () => {
    expect(PLAN_LIMITS.starter.maxMembers).toBeGreaterThan(PLAN_LIMITS.free.maxMembers);
    expect(PLAN_LIMITS.pro.maxMembers).toBeGreaterThan(PLAN_LIMITS.starter.maxMembers);
    expect(PLAN_LIMITS.enterprise.maxMembers).toBeGreaterThan(PLAN_LIMITS.pro.maxMembers);
  });
});
