const {api} = require('../api/prisma')
const{} = require('@prisma/client').PrismaClient
const {request,response,query} =require('express')
const cleanDeep =require("clean-deep")

/**
 * 
 * @param {*} app 
 * @returns 
 */

const config =(app)=>({
    databaseConfig:{
      provider:null,
     url:null
    },
    state:{},
    jwt_key: process.env.SECRET_KEY_JWT || 'BITEV_JWT_KEY',
    app: app,
    debug: true,
    secret: "",
    port: 3003,
    prismaConfig:{
      modelsPath:'/prisma/models/json-schema.json',
    },
    routes: {
      admin:{
        enabled:true,
        extendAdmin:false,
        extendPath:"/admin",
        extendTarget:"http://localhost:8080"
      },
      api: "/v1/api",
      utils:"/utils" // Utils server
    },
    express: {
      corsOptions: {
        origin:"*"
      }, // cors config
      whiteListIps:[],
      blackListIps:[],
      limit:{
        jsonSize:'200mb',
        textSize:'200mb'
      }
    },
    plugins: [],
     services: [],
    modelviews:[],
    upload: {
      enableBase64:false,
      staticURL: "/upload",
      staticDir: "/upload",
      ftp:{
        host:"",
        user:"",
        password:"",
        secure:true,
        assetURL:"",
        keepLocalFile:false,
        remoteFolder:"",
        timeout:0

      }
    },
    utils:{
      hashModelPassword:false
   }
});

const paramsCollection ={
  activeTask:false,
  singleType:false,
  hidden:false,
  mode:"model",
  slug:null,
  label:null,
  timestamps:true,
  auth:{ // password field is always required
    enabled:false,
    by:'email' // Primary field for authentication
  },
  admin:{
    label:"",
    timestampsFormat:"YYYY-MM-DD HH:mm:ss"
  },
  fields:[{
    admin:{
      label:"",
      placeholder:null,
      suffix:"",
      prefix:"",
      prefix:null,
      format:"",
      align:"",
      sortable:false,
      readonly:false,
      hidden:false,
      required:false,
    },
    name:null,
    label:"",
    type:null, // relationship | composite | boolean | number | string | media | select | ...
    composite:null,
    unique:false,
    hasMany:false,
    relationTo:null,
    linked:true,
    defaultValue:null,
    required:false,
    options:[],
    relationType:'one-to-many',
    relationSideName:'',
    private:false
  }]
}


class Collection{
  constructor(params=paramsCollection
   ){

    if(!params.fields) params.fields =[]
    params ={
      ...paramsCollection,
      ...params,
      fields:params.fields.map(cf=>{
        return {
          ...paramsCollection.fields[0],
          ...cf,
          admin:{
            ...paramsCollection.fields[0].admin,
            ...cf.admin
          }
        }
      })
    }
   this.params = params
   return cleanDeep(this.params)
  }




}

const Context=({
    model:"", // Model on cllection
    input:{
      data:Object,
      where:Object,
      select:Object,
      include:Object,
      orderBy:Object,
      distinct:Object,
    },
    req:request,
    api :api,
    state:Object, // global state
    doc:Object|| Array, // Array or Object data
    services:Function || Array,
    many:Boolean,
    user:Object, // User session
    query:query,
  })

  

const beforeCreate= (ctx=Context)=>{

  


}


module.exports={
    config,
    Collection:(params=paramsCollection)=>new Collection(params),
    beforeCreate,
    Context,
    paramsCollection,
    paramsField:paramsCollection?.fields[0] ||null
   
}