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 ModelIdSchema = Type.String({ maxLength: 80, minLength: 1, pattern: "^[a-z0-9][a-z0-9.-]+$", $id: "ModelId" });
export const ModelContractValidationStatusSchema = Type.Union([
  Type.Literal("blocked"), Type.Literal("unverified"), Type.Literal("verified"),
], { $id: "ModelContractValidationStatus" });
export const ModelRuntimeReasonSchema = Type.Union([
  Type.Literal("available"), Type.Literal("configured_disabled"), Type.Literal("contract_unverified"),
  Type.Literal("contract_blocked"), Type.Literal("gateway_balance_insufficient"), Type.Literal("gateway_paused"),
  Type.Literal("worker_degraded"),
], { $id: "ModelRuntimeReason" });
export const ModelReferenceLimitsSchema = Type.Object({
  max_file_bytes: Type.Integer({ minimum: 1 }),
  max_files: Type.Integer({ minimum: 0 }),
  max_total_bytes: Type.Integer({ minimum: 1 }),
}, { additionalProperties: false, $id: "ModelReferenceLimits" });
export const ModelRuntimeAvailabilitySchema = Type.Object({
  available_for_new_jobs: Type.Boolean(),
  checked_at: Type.String({ pattern: isoTimestampPattern }),
  reason: Type.Ref(ModelRuntimeReasonSchema),
}, { additionalProperties: false, $id: "ModelRuntimeAvailability" });
export const ModelConfigSchema = Type.Object({
  config_version: Type.Integer({ minimum: 1 }),
  contract_evidence_ref: Type.Union([Type.String({ maxLength: 160, minLength: 1 }), Type.Null()]),
  contract_validation_status: Type.Ref(ModelContractValidationStatusSchema),
  credit_cost: Type.Integer({ minimum: 1 }),
  display_name: Type.String({ maxLength: 160, minLength: 1 }),
  enabled: Type.Boolean(),
  error_mapping_profile: Type.Record(Type.String(), Type.String()),
  gateway_account_ref: Type.String({ maxLength: 160, minLength: 1 }),
  is_default: Type.Boolean(),
  model_id: Type.Ref(ModelIdSchema),
  prompt_max_length: Type.Integer({ minimum: 1 }),
  recommendation_priority: Type.Integer({ minimum: 1 }),
  reference_limits: Type.Ref(ModelReferenceLimitsSchema),
  route_profile: Type.Record(Type.String(), Type.Unknown()),
  runtime_availability: Type.Ref(ModelRuntimeAvailabilitySchema),
  safety_source: Type.String({ maxLength: 160, minLength: 1 }),
  supported_ratios: Type.Array(Type.String({ maxLength: 20, minLength: 1 }), { minItems: 1, maxItems: 8 }),
}, { additionalProperties: false, $id: "ModelConfig" });
export const ModelConfigCandidateSchema = Type.Object({
  credit_cost: Type.Integer({ minimum: 1 }),
  display_name: Type.String({ maxLength: 160, minLength: 1 }),
  enabled: Type.Boolean(),
  error_mapping_profile: Type.Record(Type.String(), Type.String()),
  gateway_account_ref: Type.String({ maxLength: 160, minLength: 1 }),
  is_default: Type.Boolean(),
  model_id: Type.Ref(ModelIdSchema),
  prompt_max_length: Type.Integer({ minimum: 1 }),
  recommendation_priority: Type.Number(),
  reference_limits: Type.Ref(ModelReferenceLimitsSchema),
  route_profile: Type.Record(Type.String(), Type.Unknown()),
  safety_source: Type.String({ maxLength: 160, minLength: 1 }),
  supported_ratios: Type.Array(Type.String({ maxLength: 20, minLength: 1 }), { minItems: 1, maxItems: 8 }),
}, { additionalProperties: false, $id: "ModelConfigCandidate" });
export const ModelConfigurationResponseSchema = Type.Object({
  config_set_version: Type.Integer({ minimum: 1 }),
  configured_default_model_id: Type.Ref(ModelIdSchema),
  models: Type.Array(Type.Ref(ModelConfigSchema), { minItems: 3, maxItems: 3 }),
  recommended_model_id: Type.Union([Type.Ref(ModelIdSchema), Type.Null()]),
}, { additionalProperties: false, $id: "ModelConfigurationResponse" });
export const ModelConfigUpdateRequestSchema = Type.Object({
  expected_config_set_version: Type.Integer({ minimum: 1 }),
  models: Type.Array(Type.Ref(ModelConfigCandidateSchema), { minItems: 3, maxItems: 3 }),
}, { additionalProperties: false, $id: "ModelConfigUpdateRequest" });
export const ModelConfigUpdateHeadersSchema = Type.Object({
  "idempotency-key": Type.String({ maxLength: 200, minLength: 32, pattern: "^[A-Za-z0-9_-]+$" }),
  "x-csrf-token": Type.String({ maxLength: 64, minLength: 43, pattern: "^[A-Za-z0-9_-]+$" }),
}, { additionalProperties: true, $id: "ModelConfigUpdateHeaders" });
export const ModelParamsSchema = Type.Object({ model_id: Type.Ref(ModelIdSchema) }, { additionalProperties: false, $id: "ModelParams" });

export type ModelConfig = Static<typeof ModelConfigSchema>;
export type ModelConfigCandidate = Static<typeof ModelConfigCandidateSchema>;
export type ModelConfigurationResponse = Static<typeof ModelConfigurationResponseSchema>;
export type ModelConfigUpdateRequest = Static<typeof ModelConfigUpdateRequestSchema>;
export type ModelConfigUpdateHeaders = Static<typeof ModelConfigUpdateHeadersSchema>;
export type ModelParams = Static<typeof ModelParamsSchema>;
