/**
 * Multi-Tenant module — types and schemas.
 * Manages tenant isolation, tenant-level settings, member management,
 * and cross-tenant resource sharing.
 */

import { z } from 'zod';

// ── Tenant Types ──────────────────────────────────────────────────

export type TenantStatus = 'active' | 'suspended' | 'deactivated';
export type TenantPlan = 'free' | 'starter' | 'pro' | 'enterprise';
export type MemberRole = 'owner' | 'admin' | 'member' | 'viewer';

export interface TenantDoc {
  id: string;
  productId: string;
  name: string;
  slug: string;
  status: TenantStatus;
  plan: TenantPlan;
  /** Owner userId */
  ownerId: string;
  /** Custom domain for white-label */
  customDomain: string | null;
  /** Tenant-level settings overrides */
  settings: Record<string, unknown>;
  /** Max members allowed by plan */
  maxMembers: number;
  /** Max storage in bytes */
  maxStorageBytes: number;
  /** Current storage used in bytes */
  storageUsedBytes: number;
  createdAt: string;
  updatedAt: string;
}

export interface TenantMemberDoc {
  id: string;
  tenantId: string;
  productId: string;
  userId: string;
  role: MemberRole;
  /** Display name within the tenant */
  displayName: string;
  invitedBy: string;
  joinedAt: string;
  updatedAt: string;
}

export interface TenantInviteDoc {
  id: string;
  tenantId: string;
  productId: string;
  email: string;
  role: MemberRole;
  invitedBy: string;
  /** SHA-256 hash of invite token */
  tokenHash: string;
  expiresAt: string;
  acceptedAt: string | null;
  createdAt: string;
}

// ── Schemas ──────────────────────────────────────────────────────

export const CreateTenantSchema = z.object({
  name: z.string().min(2).max(100),
  slug: z
    .string()
    .min(2)
    .max(64)
    .regex(/^[a-z0-9-]+$/, 'Slug must be lowercase alphanumeric with hyphens'),
  plan: z.enum(['free', 'starter', 'pro', 'enterprise']).default('free'),
  customDomain: z.string().max(253).nullable().optional(),
});

export const UpdateTenantSchema = z.object({
  name: z.string().min(2).max(100).optional(),
  status: z.enum(['active', 'suspended', 'deactivated']).optional(),
  plan: z.enum(['free', 'starter', 'pro', 'enterprise']).optional(),
  customDomain: z.string().max(253).nullable().optional(),
  settings: z.record(z.unknown()).optional(),
  maxMembers: z.number().int().min(1).max(10000).optional(),
  maxStorageBytes: z.number().int().min(0).optional(),
});

export const InviteMemberSchema = z.object({
  email: z.string().email(),
  role: z.enum(['admin', 'member', 'viewer']).default('member'),
  displayName: z.string().min(1).max(100).optional(),
});

export const UpdateMemberRoleSchema = z.object({
  role: z.enum(['admin', 'member', 'viewer']),
});

export type CreateTenantInput = z.infer<typeof CreateTenantSchema>;
export type UpdateTenantInput = z.infer<typeof UpdateTenantSchema>;
export type InviteMemberInput = z.infer<typeof InviteMemberSchema>;
export type UpdateMemberRoleInput = z.infer<typeof UpdateMemberRoleSchema>;

// ── Plan Limits ──────────────────────────────────────────────────

export const PLAN_LIMITS: Record<TenantPlan, { maxMembers: number; maxStorageBytes: number }> = {
  free: { maxMembers: 5, maxStorageBytes: 1 * 1024 * 1024 * 1024 }, // 1 GB
  starter: { maxMembers: 25, maxStorageBytes: 10 * 1024 * 1024 * 1024 }, // 10 GB
  pro: { maxMembers: 100, maxStorageBytes: 100 * 1024 * 1024 * 1024 }, // 100 GB
  enterprise: { maxMembers: 10000, maxStorageBytes: 1024 * 1024 * 1024 * 1024 }, // 1 TB
};
