import { Type, type Static } from "@sinclair/typebox";

const isoTimestampPattern = "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}Z$";

export const ExternalServiceIdSchema = Type.Union([
  Type.Literal("resend_email"),
  Type.Literal("amap_web_service"),
], { $id: "ExternalServiceId" });

export const ExternalServicePeriodTypeSchema = Type.Union([
  Type.Literal("daily"),
  Type.Literal("monthly"),
], { $id: "ExternalServicePeriodType" });

export const ExternalServiceStatusSchema = Type.Union([
  Type.Literal("active"),
  Type.Literal("paused_quota"),
  Type.Literal("paused_provider"),
  Type.Literal("disabled"),
], { $id: "ExternalServiceStatus" });

export const ExternalServiceUsageSchema = Type.Object({
  service_id: Type.Ref(ExternalServiceIdSchema),
  period_type: Type.Ref(ExternalServicePeriodTypeSchema),
  period_start: Type.String({ pattern: isoTimestampPattern }),
  hard_limit: Type.Integer({ minimum: 1 }),
  used_count: Type.Integer({ minimum: 0 }),
  service_status: Type.Ref(ExternalServiceStatusSchema),
  pause_reason: Type.Union([Type.String({ maxLength: 120, pattern: "^[a-z0-9_.-]+$" }), Type.Null()]),
  updated_at: Type.String({ pattern: isoTimestampPattern }),
}, { additionalProperties: false, $id: "ExternalServiceUsage" });

export const AdminServicesResponseSchema = Type.Object({
  services: Type.Array(Type.Ref(ExternalServiceUsageSchema), { maxItems: 3 }),
}, { additionalProperties: false, $id: "AdminServicesResponse" });

export const AdminServiceLimitRequestSchema = Type.Object({
  period_type: Type.Ref(ExternalServicePeriodTypeSchema),
  hard_limit: Type.Integer({ minimum: 1 }),
}, { additionalProperties: false, $id: "AdminServiceLimitRequest" });

export const AdminServiceHealthCheckRequestSchema = Type.Object({
  available: Type.Boolean(),
  reason: Type.Optional(Type.String({ maxLength: 120, pattern: "^[a-zA-Z0-9_. -]+$" })),
}, { additionalProperties: false, $id: "AdminServiceHealthCheckRequest" });

export const AdminServiceRecoveryRequestSchema = Type.Object({
  check_id: Type.String({ maxLength: 64, minLength: 1, pattern: "^[0-9a-fA-F-]+$" }),
}, { additionalProperties: false, $id: "AdminServiceRecoveryRequest" });

export const AdminServiceParamsSchema = Type.Object({
  service_id: Type.Ref(ExternalServiceIdSchema),
}, { additionalProperties: false, $id: "AdminServiceParams" });

export type ExternalServiceId = Static<typeof ExternalServiceIdSchema>;
export type ExternalServicePeriodType = Static<typeof ExternalServicePeriodTypeSchema>;
export type ExternalServiceUsage = Static<typeof ExternalServiceUsageSchema>;
export type AdminServicesResponse = Static<typeof AdminServicesResponseSchema>;
export type AdminServiceLimitRequest = Static<typeof AdminServiceLimitRequestSchema>;
export type AdminServiceHealthCheckRequest = Static<typeof AdminServiceHealthCheckRequestSchema>;
export type AdminServiceRecoveryRequest = Static<typeof AdminServiceRecoveryRequestSchema>;
export type AdminServiceParams = Static<typeof AdminServiceParamsSchema>;
