/**
 * Feature flags types — extends kill switch to full feature flags.
 * Ported from admin dashboard + desktop + mobile kill switch implementations.
 */

import { z } from 'zod';

export interface OsVersionRange {
  platform: string;
  minVersion?: string;
  maxVersion?: string;
}

export interface FeatureFlagDoc {
  id: string;
  productId: string;
  key: string;
  enabled: boolean;
  description: string;
  platforms: string[];
  regions: string[];
  osVersions: OsVersionRange[];
  segments: string[];
  percentage: number;
  createdAt: string;
  updatedAt: string;
}

export const OsVersionRangeSchema = z.object({
  platform: z.string().min(1),
  minVersion: z.string().optional(),
  maxVersion: z.string().optional(),
});

export const CreateFlagSchema = z.object({
  key: z
    .string()
    .min(1)
    .regex(/^[a-z0-9_]+$/),
  enabled: z.boolean().default(true),
  description: z.string().default(''),
  platforms: z.array(z.string()).default([]),
  regions: z.array(z.string()).default([]),
  osVersions: z.array(OsVersionRangeSchema).default([]),
  segments: z.array(z.string()).default([]),
  percentage: z.number().min(0).max(100).default(100),
});

export const UpdateFlagSchema = z.object({
  enabled: z.boolean().optional(),
  description: z.string().optional(),
  platforms: z.array(z.string()).optional(),
  regions: z.array(z.string()).optional(),
  osVersions: z.array(OsVersionRangeSchema).optional(),
  segments: z.array(z.string()).optional(),
  percentage: z.number().min(0).max(100).optional(),
});

export type CreateFlagInput = z.infer<typeof CreateFlagSchema>;
export type UpdateFlagInput = z.infer<typeof UpdateFlagSchema>;
