import { z } from 'zod'

export const appStatusSchema = z.enum(['active', 'inactive'])
export type AppStatus = z.infer<typeof appStatusSchema>

export const billingMethodSchema = z.enum(['subscription', 'time', 'count'])
export type BillingMethod = z.infer<typeof billingMethodSchema>

export const operationModeSchema = z.enum(['free', 'paid'])
export type OperationMode = z.infer<typeof operationModeSchema>

export const deductionCycleSchema = z.enum(['per_use', 'per_minute', 'per_hour', 'per_day'])
export type DeductionCycle = z.infer<typeof deductionCycleSchema>

export const loginPolicySchema = z.enum(['loose', 'strict', 'hybrid'])
export type LoginPolicy = z.infer<typeof loginPolicySchema>

export const appSchema = z.object({
  id: z.union([z.string(), z.number()]),
  name: z.string(),
  description: z.string(),
  app_key: z.string(),
  billing_type: z.enum(['free', 'subscription', 'time', 'count']),
  login_policy: loginPolicySchema.default('loose'),
  encrypt_type: z.enum(['none', 'aes', 'rsa', 'rc4']),
  secret_key: z.string(),
  bind_type: z.enum(['device', 'account', 'none']),
  max_devices: z.number().default(1),
  change_limit: z.number().optional(),
  change_interval: z.number().optional(),
  multi_open: z.boolean().default(false),
  max_instances: z.number().optional(),
  enable_trial: z.boolean().default(false),
  trial_balance: z.number().default(0),
  enable_free_period: z.boolean().default(false),
  free_period_start: z.string().optional(),
  free_period_end: z.string().optional(),
  deduction_cycle: deductionCycleSchema.optional(),
  deduction_amount: z.number().optional(),
  heartbeat_interval: z.number().default(60),
  heartbeat_timeout: z.number().default(300),
  max_attempts: z.number().default(5),
  lock_duration: z.number().default(30),
  status: appStatusSchema,
  disabled_by_package: z.boolean().default(false),
  icon_url: z.string().optional(),
  users: z.number().optional(),
  verify_count: z.number().optional(),
  created_at: z.string(),
  updated_at: z.string().optional(),
})
export type App = z.infer<typeof appSchema>

export const appListSchema = z.array(appSchema)
