import { catalog, Model } from '@picsart/ai-sdk';
import policy from '../../shared/model-policy.json';
import {sdkVideoVariants} from '../../shared/sdk-video-options';
import {MODEL_CATALOG_REVISION,type SyncedModels} from '../../shared/model-sync';
import {supportedModels,modelSchema} from './supported-models';
export async function syncModels():Promise<SyncedModels>{
 // Refresh public rates as well as capabilities; account quotes remain authoritative.
 const supported=supportedModels();
 const visible=new Set<string>(catalog.find({}).map(m=>m.id));
 const result:SyncedModels={syncedAt:0,revision:MODEL_CATALOG_REVISION,configuredDefault:policy.defaultVideoModel,video:[],image:[],schemas:{},videoOptions:{}};
 for(const id of supported.video){
  if(!visible.has(id))continue;
  const m=Model(id as Parameters<typeof Model>[0]),raw=m.params().toSchema();
  const schema={model:id,schema:{properties:Object.fromEntries(Object.entries(raw).map(([k,v])=>[k,{...v,minimum:v.min,maximum:v.max}])),required:Object.entries(raw).filter(([,v])=>v.required).map(([k])=>k)}};
  const standard=sdkVideoVariants(schema),loop=sdkVideoVariants(schema,true);
  if(!standard.length)continue;
  result.video.push({id,name:m.name,provider:m.meta().provider.name});result.schemas[id]=schema;result.videoOptions[id]={standard,loop};
 }
 for(const id of supported.image){const m=Model(id);result.image!.push({id,name:m.name,provider:m.meta().provider.name});result.schemas[id]=modelSchema(id);}
 if(!result.video.length)throw new Error('No supported models returned');
 result.syncedAt=Date.now();return result;
}
