import Joi from 'joi';
import { MODULE_KEYS, type ModuleKey } from '../config/modules.js';
import type { UsageLimits } from './organization.validator.js';

// GET /plans, POST /plans, PATCH /plans/:id — blenaxis-super-admin's
// PlanInput = Pick<Plan, 'code'|'name'|'description'|'priceMonthly'|
// 'includedModuleKeys'|'limits'|'archived'> (currency is server-defaulted,
// not settable here).
export interface PlanBody {
  code: string;
  name: string;
  description?: string;
  priceMonthly: number;
  includedModuleKeys: ModuleKey[];
  limits: UsageLimits;
  archived?: boolean;
}

const limitsSchema = Joi.object<UsageLimits>({
  maxUsers: Joi.number().integer().min(1).required(),
  maxProjects: Joi.number().integer().min(1).required(),
  storageGb: Joi.number().integer().min(1).required(),
}).required();

export const createPlanSchema = Joi.object<PlanBody>({
  code: Joi.string().trim().uppercase().min(1).max(30).required(),
  name: Joi.string().trim().min(1).max(100).required(),
  description: Joi.string().trim().max(255).allow('').optional(),
  priceMonthly: Joi.number().integer().min(0).required(),
  includedModuleKeys: Joi.array().items(Joi.string().valid(...MODULE_KEYS)).required(),
  limits: limitsSchema,
  archived: Joi.boolean().optional(),
}).required();

export const updatePlanSchema = Joi.object<Partial<PlanBody>>({
  code: Joi.string().trim().uppercase().min(1).max(30),
  name: Joi.string().trim().min(1).max(100),
  description: Joi.string().trim().max(255).allow(''),
  priceMonthly: Joi.number().integer().min(0),
  includedModuleKeys: Joi.array().items(Joi.string().valid(...MODULE_KEYS)),
  limits: limitsSchema,
  archived: Joi.boolean(),
}).min(1).required();
